# How to add subscript in PlotLegends?

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"?

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.

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

• Using Row is simpler. Commented Apr 27, 2017 at 15:55
• @CarlWoll could you please show it? Commented Apr 27, 2017 at 15:56
• @CarlWoll very good idea too Commented Apr 27, 2017 at 15:57

You can convert a subscripted variable to a string.

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


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