3
$\begingroup$

I installed MaTeX and I tested for a while and I like it very much.

I have the following code that I want to use MaTeX for displaying the Expresions as it would be in LaTeX.

 Plot[{Sin[x], Cos[x], x}, {x, 0, 2 π},
 Background -> None,
 PlotRange -> Full,
 LabelStyle -> {FontFamily -> "Times New Roman", FontSize -> 12},
 Axes -> False,
 Frame -> True,
 PlotTheme -> "Monochrome",
 PlotLegends -> 
 Placed[LineLegend["Expressions", 
 LegendFunction -> Framed], {{0.16, 0.79}}]]

I have tried to put MaTeX[] in the whole code, in the "Expressions" field or inside the LineLegend command but nothing worked properly. Any ideas? Thanks in advance!

$\endgroup$
0

1 Answer 1

7
$\begingroup$

I think you want something like this to convert your plot labels to LaTeX (note that I'm also using CustomTicks):

Needs["MaTeX`"];
Needs["CustomTicks`"];
LatexPlot[plot_, fontsize_] := Module[{xmin, xmax, ymin, ymax, al},
  {xmin, xmax, ymin, ymax} = 
   Flatten[PlotRange /. First[AbsoluteOptions[plot, PlotRange]]];
  al = AxesLabel /. First[AbsoluteOptions[plot, AxesLabel]];
  Show[plot /. {Thickness[x_] -> Thickness[0.01], 
     AbsoluteThickness[x_] -> Thickness[0.01], AxesLabel -> None},
   Frame -> True,
   FrameLabel -> MaTeX[al, Magnification -> fontsize/12],
   FrameTicks -> {{MapAt[
       Function[x, MaTeX[x, Magnification -> fontsize/12]], 
       LinTicks[ymin, ymax], {{All, 2}}], 
      StripTickLabels[LinTicks]}, {MapAt[
       Function[x, MaTeX[x, Magnification -> fontsize/12]], 
       LinTicks[xmin, xmax], {{All, 2}}], 
      StripTickLabels[LinTicks]}},
   PlotRange -> {{xmin, xmax}, {ymin, ymax}}
   ]]

You can use it to convert regular plots:

p = Plot[{Sin[x], Cos[x], x}, {x, 0, 2 \[Pi]}, PlotRange -> Full, 
  PlotTheme -> "Monochrome", AxesLabel -> {"x", "y"}, 
  PlotLegends -> 
   Placed[LineLegend["Expressions", 
     LegendFunction -> Framed], {{0.16, 0.79}}]]

enter image description here

to LaTeX style

LatexPlot[p, 20]

enter image description here

I am not yet sure how to convert the legend, but I would think that you can use a similar approach.

Update:

For the legends, I think a manual approach is appropriate given that you have only three items:

p = Plot[{Sin[x], Cos[x], x}, {x, 0, 2 \[Pi]}, PlotRange -> Full, 
  PlotTheme -> "Monochrome", AxesLabel -> {"x", "y"}, 
  PlotLegends -> 
   Placed[LineLegend[
     MaTeX[#, Magnification -> 20/12] & /@ {"\\sin(x)", "\\cos(x)", 
       "x"}, LegendFunction -> Framed], {{0.21, 0.71}}]]
LatexPlot[p, 20]

You might want to manually adjust the plot range to create some free space for the legend.

enter image description here

$\endgroup$
3
  • $\begingroup$ It worked perfect, I found another way to have to have the LaTeX typesetting in legends, but your way include the ticks to be the same too. Thank you for your help! Is there another way to achieve the ticks to be in the typesetting but without the LaTeXPlot, (lets say the FrameTicks command)? Thanks again! $\endgroup$
    – Nikos K.
    Feb 16, 2017 at 23:15
  • $\begingroup$ I don't think it is possible in combination with automatic ticks. If you are willing to define your ticks manually, you can of course set their labels in the same way how I converted the legend. $\endgroup$
    – Felix
    Feb 16, 2017 at 23:23
  • $\begingroup$ Alright, thanks! $\endgroup$
    – Nikos K.
    Feb 16, 2017 at 23:29

Your Answer

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

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