# How to plot using MaTeX for LaTeX typesetting in Mathematica

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!

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


to LaTeX style

LatexPlot[p, 20]


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.

• 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! Feb 16 '17 at 23:15
• 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. Feb 16 '17 at 23:23
• Alright, thanks! Feb 16 '17 at 23:29