When the "|" character appears inside of a string containing linear box syntax, SyntaxQ returns False and FrontEnd often fails to display the string correctly. Consider:

  1. A string with an alphabetical character embedded in linear box syntax, it's OK:

    "\!\(\*StyleBox[\"a\"]\)" // SyntaxQ
  2. When we put the "|" character (which is ASCII!), something goes wrong:

    "\!\(\*StyleBox[\"|\"]\)" // SyntaxQ
  3. Well, let us take what Mathematica suggests:

    str = ToString[Style["|"], StandardForm];
    str // SyntaxQ
    str // InputForm
    "\!\(\*StyleBox[\"\\\"|\\\"\", Rule[StripOnInput, False]]\)"

Now it looks OK, but I wish to have additional "|" in the string, so I try:

str <> "|" // SyntaxQ

Again something goes wrong (but why???), while with the alphabetical characters it's OK:

str <> "a" // SyntaxQ

StringJoin of str with itself is also OK, but I don't wish to blow up my file having every "|" character wrapped by the linear syntax. The above is just a minimal working example, of course. Actually I'm constructing a string containing both Bold and Plain (the default style) symbols "|".

I'm working with large strings and wish to keep them as small in size as possible and I'm also trying to make the script as efficient as possible, hence I wish to avoid excessive linear syntax inside of the strings where it isn't strictly necessary.

What is the correct way to compose a large string with differently styled "|" characters? The string have to be as small as possible, with default formatting applied to unstyled characters.

  • $\begingroup$ You're attempt #2 corresponds to Style[|] which is not valid syntax. Your attempt #3 with the | at the end corresponds to Style["|"]| which is again not valid syntax. Can you just use "\"|\"" instead of "|"? $\endgroup$
    – Carl Woll
    Commented Dec 29, 2017 at 18:37

1 Answer 1


The basic issue is that | corresponds to an operator, and not a letter. A simpler example:




SyntaxQ means that the string can be parsed into a Mathematica expression (e.g., ToExpression). You can't expect a string to be syntactically correct when you add an operator in a random place. Can you just use "\"|\"" instead of "|"?

In order to have SyntaxQ strings that can be string joined, you probably need to create linear syntax for each string. So, something like:

plain = "\!\(\"|\"\)"
bold = "\!\(\*StyleBox[\"\\\"|\\\"\", Bold]\)"
bold <> plain

plain //SyntaxQ
bold //SyntaxQ
bold <> plain //SyntaxQ

Here's a pic:

enter image description here

  • $\begingroup$ I'm trying but it doesn't work: "\!\(\*StyleBox[\"\\\"|\\\"\",Bold]\)\"\\\"|\\\"\"". My goal is to get something like "| |" as the output (with the ability to StringJoint it with similar strings). How can I achieve this? $\endgroup$ Commented Dec 29, 2017 at 19:23
  • $\begingroup$ @AlexeyPopkov Could you use a different unicode character? For example, FromCharacterCode[10072]? $\endgroup$
    – Carl Woll
    Commented Dec 29, 2017 at 19:44
  • $\begingroup$ I have to use "|" because other characters including FromCharacterCode[10072] are rendered in a different way. Your updated solution works, thank you. $\endgroup$ Commented Dec 30, 2017 at 6:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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