2
$\begingroup$

When I try to have a X_0=0 in the legend of the plot, I get a reversed order. Can anyone tell me why and how to fix it?

 Plot[x, {x, 0, 1}, PlotLegends -> {Subscript["X", 0] "=0"}]

One easy way to do this is using the Writing assistant rather than Subscript. However, I still don't see how to keep the order I desired, or even more important, how to include different functions/variables in the brackets of PlotLegends. For example, what if I wanted to recall a variable value rather than "=0"?

$\endgroup$
0

4 Answers 4

3
$\begingroup$

Your problem is that you are multiplying a subscript and a string. That is, your input is actually:

PlotLegends -> {Times[Subscript["X", 0], "=0"]}

and Times is Orderless, so Mathematica reorders the product into a canonical form. Instead you can use Row:

PlotLegends -> {Row[{Subscript["X", 0], "=0"}]}

However, I prefer using Equal instead, as in:

PlotLegends -> {Subscript["X", 0] == 0}

so that you rely on the spacing that Mathematica automatically gives typeset expressions.

$\endgroup$
2
$\begingroup$

You can't really convert a subscripted variable to a string, so you should use RowBox instead:

Plot[x, {x, 0, 1}, PlotLegends -> {DisplayForm@RowBox[{SubscriptBox["x", "0"], "=0"}]}]

You can also use a variable:

a = 0;
Plot[x, {x, 0, 1}, PlotLegends -> {DisplayForm@RowBox[{SubscriptBox["x", "0"], "=", a}]}]
$\endgroup$
3
  • $\begingroup$ Using Row is simpler. $\endgroup$
    – Carl Woll
    Commented Apr 27, 2017 at 15:55
  • $\begingroup$ @CarlWoll could you please show it? $\endgroup$
    – Andrea G
    Commented Apr 27, 2017 at 15:56
  • $\begingroup$ @CarlWoll very good idea too $\endgroup$
    – Stitch
    Commented Apr 27, 2017 at 15:57
1
$\begingroup$

You can convert a subscripted variable to a string.

legends = {StringForm["````", Subscript[X, 0]  , "=0"]}
Plot[x, {x, 0, 1}, PlotLegends -> legends]
$\endgroup$
1
$\begingroup$

You can also use MaTeX package to include LaTeX typesetting to your plot.

<< MaTeX`

texStyle = {FontFamily -> "Latin Modern Roman", FontSize -> 12};

Plot[x, {x, 0, 1}, PlotLegends -> MaTeX /@ {"x_0=0"}, BaseStyle -> texStyle]

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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