2
$\begingroup$

I understand why Mathematica doesn't the support the [ character in symbol names, but it should be escapable in some way, like non-printable characters are in strings using \. A bigger problem is that a large number of Unicode characters are also not allowed in symbol names, and some of them for no obvious reason.

Is there a good way to escape such characters? Ideally, it should be broadly usable in arbitrary Mathematica code without requiring further special handling of the escaped symbol names.

I'm thinking something like the following:

Replace[a$multiply$[b[c, d]], 
 head_[x___] :> $[
        StringReplace[ToString@head, {"$multiply$" :> "*"}]][x], {0, 
  Infinity}]
$\endgroup$
  • $\begingroup$ I'm voting to close this question as off-topic because this is not a question but merely a rant about how the OP would like to change the behavior of Mathematica $\endgroup$ – m_goldberg Sep 10 '15 at 2:57
  • $\begingroup$ @m_goldberg But what if I can patch it to something like a posted used the dollarsign character? I'll let you close it, but I will probably repost it once I get a somewhat working solution. $\endgroup$ – William Sep 10 '15 at 3:13
  • $\begingroup$ @M_goldberg If I get one more downvote I'm probably going to delete it for now. $\endgroup$ – William Sep 10 '15 at 3:18
  • 3
    $\begingroup$ While allowing arbitrary characters in symbols is perhaps not a good idea (could lead to ambiguities in interpretation), I for one do not see any "rant" here, so +1 to offset down-votes I do not think warranted. $\endgroup$ – ciao Sep 10 '15 at 3:47
  • $\begingroup$ I edited your question to try to make it more accessible, since I had to read carefully to discover what you were really getting at and understand that it is a valid and useful thing to want. If you do not like the edits, please roll back as appropriate, but in my opinion it is a more idiomatic question when posed this way. You also have my upvote, not for offsetting reasons but because it is a good question in its essence. $\endgroup$ – Oleksandr R. Sep 10 '15 at 23:00
1
$\begingroup$

The following is in the right direction if you include the answer to invalid Symbol characters also.

opts = {"$divide$" :> "/", "$backslash$" :> "\\", "$period$" :> ".", 
   "$tilde$" :> "~", "$backtick$" :> "`", "$exclaim$" :> "!", 
   "$at$" :> "@", "$number$" :> "#", "$dollar$" :> "$", 
   "$percent$" :> "%", "$caret$" :> "^", "$amp$" :> "&", 
   "$multiply$" :> "*", "$parenleft$" :> "(", "$parenright$" :> ")", 
   "$underscore$" :> "_", "$dash$" :> "-", "$plus$" :> "+", 
   "$equal$" :> "=", "$barleft$" :> "[", "$barright$" :> "]", 
   "$bracketleft$" :> "{", "$bracketright$" :> "}", "$colon$" :> ":", 
   "$semicolon$" :> ";", "$singlequote$" :> "'", 
   "$doublequote$" :> "\"", "$lessthan$" :> "<", "$comma$" :> ",", 
   "$period$" :> ".", "$slash$" :> "/", "$question$" :> "?", 
   "$space$" :> " ", "$n$" :> "\n", "$r$" :> "\r"};
opts = Join[
   Drop[Quiet[
     Cases[Symbol[FromCharacterCode[#]] & /@ Range[0, 16^4 - 1], 
      Symbol[z__] -> (StringJoin@
          Flatten@Join[{"$", IntegerString[ToCharacterCode[z], 16, 4],
              "$"}] -> z)]], 1], opts];
Replace[a$multiply$[b$underscore$[c, d]], 
 head_[x___] :> $[StringReplace[ToString@head, opts]][x], {0, 
  Infinity}]

If you would like to display it in a pretty form you can do the following.

ReplaceRepeated[
 ToBoxes[FullForm@Ti$at$mes[{a}, Rational[1, -2], "test"]][[1, 
   1]], {RowBox[{b_String, "[", x___String, "]"}] :> 
   StringJoin[{StringReplace[b, opts], "[", x, "]"}], 
  RowBox[{b___String}] :> StringJoin[{b}]}]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.