Skip to main content
2 of 13
added 4 characters in body
Alexey Popkov
  • 62.3k
  • 7
  • 154
  • 375

A closely related (but not identical) question was asked by user kj and answered by John Fultz in the official newsgroup in 2010, so I'll cite here both the question and the answer completely:

The question:

When I examine the source code of Mathematica notebooks, I often see Cell objects whose first argument is preceded by

"\<\

and followed by

\
\>"

E.g.

Cell["\<\
Yet another undocumented item.\
\>", "Text"...

What do these delimiters mean? And how do they differ from the TextData symbol? (The latter is also undocumented, but at least I've seen "cameo appearances" of it in the documentation.)

TIA!
~kj

The answer:

Cell[_String] is equivalent to Cell[TextData[{_String}]] as far as the front end is concerned. The former is just an abbreviation of the latter syntax, which is necessarily used when the contents are more complex (e.g., contain styles or buttons).

The \<\> business dates back to a hoarier day (in my opinion) in the design of strings in Mathematica. Once upon a time, Mathematica had an odd interpretation of newlines which I probably made more sense in the days of 80 character terminal usage. You'll have to forgive me...my memory is fuzzy here, and I don't have an old Mathematica text to double-check this from my current location...but I believe the issue was something like this...

"abc\
         d"

...with the newlines and spaces as shown, being equivalent to "abc d" (with just the single space). The \<\> business forced everything inside the string to really be interpreted literally, with backslash at the end of a line meaning to simply escape the newline, and do nothing special to leading spaces. Since the Mathematica notebook format generally wraps at about 70 characters, the non-literal interpretation would have made the representation of more than 70 consecutive spaces difficult.

The modern Mathematica notebook format (introduced in 1996) was always made to be interpreted properly as a Mathematica expression should you call Get[] on it from the kernel. So this syntax was standardized, and is still used today. Now, the kernel simply ignores the \<\> delimiters as you can see below:

In[1]:== StringLength["\<x\>"]

Out[1]== 1

Sincerely,

John Fultz
jfultz at wolfram.com
User Interface Group
Wolfram Research, Inc.


###Summary of the relevant info from the answer by John Fultz

In the old days the strings at the low level representation of notebooks were not interpreted literally, for example after line-ending backslash all the spaces at the start of the next line were interpreted as only one space. This old behavior made difficult to represent more than 70 consecutive spaces. The \<\> syntax was introduced to force "everything inside the string to really be interpreted literally".

When developing the modern Mathematica notebook format (introduced in 1996) the \<\> syntax was accepted as the standard for internal representation of strings and is used by default since that time. The kernel simply ignores the \<\> delimiters when you use Get on a notebook file or when you use string-manipulation functions.


###My own experiments with Mathematica 11.0.0

Let us consider the example given by John Fultz (please copy the code exactly as written!):

CellPrint@Cell["abc\
           d"]
abc         d

It is easy to check that there are 9 spaces between abc and d in the printed cell (and so are before d in the code). But the new line and the backslash are absent! After pressing Shift+Ctrl+E we can see the code of the printed cell:

Cell["abc         d",
 GeneratedCell->True,
 CellAutoOverwrite->True,
 CellChangeTimes->{3.680677305471143*^9}]

Let us add the \<\> delimiters:

CellPrint@Cell["\<abc\
             d\>"]
abc         d

Nothing is changed and even the code of the printed cell is the same! A possible explanation is that the delimiters were eaten by the kernel. But pasting the code of the cell directly into a notebook produces the same, as well as editing the code of the cell! It seems the delimiters \<\> change nothing in this case.

Now let us add a space after the backslash (it isn't visible, but it is there!):

CellPrint@Cell["abc\ 
           d"]
abc 
          d

Now we got our newline but the backslash is absent. The code of the printed cell:

Cell["\<\
abc 
         d\
\>",
 GeneratedCell->True,
 CellAutoOverwrite->True,
 CellChangeTimes->{3.6806776850508537`*^9}]

Note that similar things happen when we write \n instead of entering the newline what suggests that \n is implicitly present in the above cases and is escaped by the backslash (but somehow isn't showed what looks like a bug potentially related to this one):

CellPrint@Cell["abc\\n         d"]
CellPrint@Cell["abc\ \n         d"]
abc\n         d
abc 
          d
Alexey Popkov
  • 62.3k
  • 7
  • 154
  • 375