3
$\begingroup$

This might be a silly question, but actually I lost a bit of time to figure out what I was doing wrong and now I want to understand better. Moreover, I'm not sure if the behaviour I see is a bug or not.

I was experimenting with the "new" PlotLabels functionality and I created a label which is a string with a superscript, i.e. I naively employed the usual superscript notation (ctrl+6) inside quotation marks. This is what I usually do for labels, copy-pasting the code it reads:

Plot[x^2, {x, -3, 3},PlotLabels -> "\!\(\*SuperscriptBox[\(x\), \(2\)]\)"]

The result is a weirdly placed label that looks broken (Mathematica 11.3 on macOS). I lost some times to figure out what was the problem and it turns out it was the superscript: by writing the input "by hand" as "\!\(x\^2\)" everything works nicely. See this picture to understand what I mean.
PlotLabels strange behaviour Now, my question is: have I always done something fundamentally wrong by using superscript notation inside quotation marks for my labels? Is this a bug of PlotLabels?

Note that PlotLegends works as expected in both cases: enter image description here

$\endgroup$
5
$\begingroup$

This might be a bug. However, I think linear syntax is evil, and would recommend that you avoid it like the plague. Instead, just use an expression:

Plot[x^2, {x, -3, 3}, PlotLabels -> x^2]

enter image description here

Addendum

Question: What is linear syntax?

Answer: It is something like "!(x\^2)". This is what happens to a string when you perform 2D formatting inside of a string.

Question: Why do you think this is evil?

Answer: These are the main reasons:

  1. It creates unintelligible code when displayed outside of the Front End (which happens all the time here on MSE).
  2. It is extremely difficult to convert into a readable form that can be usefully displayed outside the Front End.
  3. It mixes expressions and strings in a hidden way.

Something like:

"\!\(x\^2\) vs x"

is basically equivalent to:

StringForm["`` vs x", HoldForm[x^2]]

(It's a bit more complicated, but this gives the gist). The x^2 part is an expression, while the x part is a string. Now, Mathematica has the option SingleLetterItalics, which controls the font slant of single letter "symbols". So, in the following, notice that the x in x^2 gets italicized, while the string x does not:

Style["\!\(x\^2\) vs x", SingleLetterItalics->True]

enter image description here

This hidden difference between symbols and strings causes endless confusion for people who use linear syntax in plots, where labels are by default displayed in TraditionalForm, which has the option SingleLetterItalics set to True.

$\endgroup$
  • $\begingroup$ Thank you for your answer! What do you mean by "linear syntax is evil"? Anyways, now I notice that even my second plot has a weird spacing between the leader and the text! Do you think I should file a bug report? $\endgroup$ – justmyfault Aug 31 '18 at 6:59
  • 1
    $\begingroup$ @justmyfault See update $\endgroup$ – Carl Woll Aug 31 '18 at 18:07
  • $\begingroup$ Thanks a lot, I will probably resort to using HoldForm as much as possible! $\endgroup$ – justmyfault Sep 1 '18 at 15:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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