Skip to main content
3 of 13
deleted 22 characters in body
Alexey Popkov
  • 62.3k
  • 7
  • 154
  • 375

###Summary of the whole answer

In the old days space-like characters (spaces, newlines, tabs) inside strings were not interpreted literally, for example single newlines followed by spaces or tabs were converted to a single space. The \<\> syntax was introduced in order to allow literal interpretation of such characters.

When developing the 1996-year Mathematica notebook format (used before Mathematica version 6) the \<\> syntax was standardized. The kernel simply ignores the \<\> delimiters when you use Get on a notebook file or when you use string-manipulation functions.

Starting from version 6 the default behavior of strings was changed and the \<\> syntax is no longer needed for preserving space-like characters.


###My 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! The reason is that the backslash at the end of line is interpreted as an indicator that there is no actual linebreak and the parser should continue reading the expression from the new line.

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}]

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 and the next whitespace are absent. I think that the absence of the backslash with the space can be related to the series of bugs I discuss in this answer. 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:

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

In the first case here the newline character \n is escaped by the backslash and is present in the output literally. In the second case the backslash is absent again but the space at the first line is preserved.


###An old discussion on MathGroup

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.

Alexey Popkov
  • 62.3k
  • 7
  • 154
  • 375