1
$\begingroup$

Let this code

f[x_]:=Piecewise[{{Sin[x],x>0},{Exp[x], x<=0}}];
Plot[f[x],{x,-5,5}, PlotLegends-> {"f(x)=" TraditionalForm[f[x]]}]

Then the part of the TraditionalForm in the legend appear braced, as it would be an array. How I can quit the braces? Morover: there is a way that the legend appear with more beautiful fonts (as it would be LaTeX)?

$\endgroup$

1 Answer 1

1
$\begingroup$
Plot[f[x], {x, -5, 5}, PlotLegends-> {Row[{"f(x) = ", TraditionalForm[f[x]]}]}]

enter image description here

Note: To see why you get TraditionalForm[f[x]] parenthesized check

 "f(x)=" TraditionalForm[f[x]] // FullForm

Times["f(x)=", TraditionalForm[Piecewise[List[List[Sin[x], Greater[x, 0]], List[Power[E, x], LessEqual[x, 0]]], 0]]]

that is, "f(x)=" is multiplied by TraditionalForm[f[x]].

$\endgroup$
2
  • $\begingroup$ one more question: when I evaluate just TraditionalForm[f[x]] the output is written using more beautiful fonts than when I did the same inside the legend of a plot. There is a way to put the same fonts in the legend? $\endgroup$
    – user44169
    Commented Jul 29, 2019 at 12:15
  • 1
    $\begingroup$ @Masacroso, If you know the font family you can use Style[TraditionalForm[f[x]], FontFamily -> fontfamily] to get the same style in the legend. For best labeling you might want to try Szabolcs's MaTeX package $\endgroup$
    – kglr
    Commented Jul 29, 2019 at 12:43

Your Answer

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