2
$\begingroup$

What exactly do \> and \< do in Mathematica strings (e.g. "ab\>c")?

They sure exhibit some weird behavior! If you evaluate a string containing either of these, it completely disappears from the string: "ab\>c" evaluates to "abc".

And this isn't some formatting trickery: StringLength["ab\>c"] evaluates to 3; indeed, "a\<b\>c" == "abc" (and even "a\<b\>c" === "abc") evaluates to True.

Viewing the box structure of the input and output with CmdShiftE, in addition to reconfirming that the outputs are exactly the same, perhaps shows us a clue: evaluating "abc" and viewing the box structure of the output yields

Cell[BoxData["\<\"abc\"\>"], "Output", ...]

(Oddly enough, viewing the box structure for the input cell containing "abc" yields Cell[BoxData["\"\<abc\>\""], "Input", ...]—the escaped quotes, \", are now outside the \<, \> "brackets".)

Notice that these seem to be exclusively "string-enclosing brackets": viewing the box structure of 3 yields Cell[BoxData["3"], ...]

But what actually is their purpose? When does Mathematica use them, and maybe more importantly, why? Neither the documentation for BoxData nor String seem to offer any clues—searching the documentation for \> doesn't yield any results, either.

$\endgroup$
4
  • 1
    $\begingroup$ Those are reserved characters used to build box expression. If you need a "\>" literal, you need to escape them: "ab\\>c" or "ab\\<c" work as you would expect. $\endgroup$
    – MarcoB
    Commented Jun 21, 2020 at 2:57
  • $\begingroup$ I figured they had something to do with boxes. But I'm curious as to how exactly they're used to build box expressions—they don't appear anywhere in the documentation, that I can see. $\endgroup$
    – thorimur
    Commented Jun 21, 2020 at 2:59
  • 1
    $\begingroup$ I have marked this as a duplicate. Please review that question (linked in the banner at the top of yours), and let me know if you agree. $\endgroup$
    – Mr.Wizard
    Commented Jun 21, 2020 at 4:25
  • $\begingroup$ Ah, yes, thank you! I tried searching stackexchange for "\>" and "\<", but I expect the search function itself excludes these characters, as nothing came up. :) $\endgroup$
    – thorimur
    Commented Jun 21, 2020 at 19:55

0

Browse other questions tagged or ask your own question.