1

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
    
    True
    
  2. When we put the "|" character (which is ASCII!), something goes wrong:

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

    str = ToString[Style["|"], StandardForm];
    str // SyntaxQ
    str // InputForm
    
    True
    

    "!(*StyleBox["\"|\"", Rule[StripOnInput, False]])"

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

str <> "|" // SyntaxQ
False

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

str <> "a" // SyntaxQ
True

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.

Alexey Popkov
  • 61,809
  • 7
  • 149
  • 368
  • 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 "|"? – Carl Woll Dec 29 '17 at 18:37

1 Answers1

8

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

SyntaxQ["a"]
SyntaxQ["|"]

True

False

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

Carl Woll
  • 130,679
  • 6
  • 243
  • 355
  • 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? – Alexey Popkov Dec 29 '17 at 19:23
  • @AlexeyPopkov Could you use a different unicode character? For example, FromCharacterCode[10072]? – Carl Woll Dec 29 '17 at 19:44
  • I have to use "|" because other characters including FromCharacterCode[10072] are rendered in a different way. Your updated solution works, thank you. – Alexey Popkov Dec 30 '17 at 06:12