# MaTeX, font size, and PlotLegends

I want to create plots in Mathematica where all labels have font sizes which match those in a LaTeX document. I start by defining

Needs["MaTeX"];
texStyle = {FontFamily -> "Latin Modern Roman", FontSize -> 12}


Now, if I export a simple plot

Export["test1.pdf",
Plot[{Sin[x], Cos[x]}, {x, 0, 2 Pi},
BaseStyle -> texStyle,
ImageSize -> 400]]


the font sizes match those in my document. However, if I include a plot legend

Export["test2.pdf",
Plot[{Sin[x], Cos[x]}, {x, 0, 2 Pi},
BaseStyle -> texStyle,
ImageSize -> 400,
PlotLegends -> Placed[MaTeX[{"\\sin(x)", "\\cos(x)"}], {0.6, 0.8}]]]


the font size in the plot is too small. This might have to do with the fact that Mathematica makes the second plot smaller (independent of MaTeX).

• Minor note: MaTeX does use a 12 pt font size by default, but instead of relying on that (it may change in the future!) I would set it explicitly too. – Szabolcs Mar 26 at 19:11

This has nothing to do with MaTeX and is essentially a duplicate of

(But can't be marked as such because that question is on StackOverflow, and it was asked before the creation of Mathematica.SE)

However, MaTeX does come with a nice tutorial on preparing figures to size, which explains this issue. Enter MaTeX/tutorial/PreparingFiguresToSize in the address bar of the documentation browser.

In short, when an expression with head Graphics or Graphics3D is exported, it retains the sizes. However, when any other visual expression is exported to PDF, it will use the styles from the PrintingStyleEnvironment. The default is "Printout", which downscales everything to 80%. This applies to all sorts of expressions that format content in notebooks, such as Row, Grid, Labeled, etc. It also applies to Legended, which is the head of any graphics containing a legend. This is because Legended uses the same underlying boxes as Grid, Column, Labeled and several others: GridBox.

Yes, this is very annoying and very hard to figure out if you are not aware of it.

The workaround is to set the PrintingStyleEnvironment to be the same as the style environment used for on-screen display:

SetOptions[$FrontEndSession, PrintingStyleEnvironment -> "Working"]  This setting will persist until you restart the Front End. If you use $FrontEnd instead of \$FrontEndSession then it persists even across restarts. I do not recommend doing this though, as there is a good reason for the "Printout" environment downscaling to 80%: this environment is used when printing notebooks or converting notebooks to multi-page PDFs. Without the downscaling very little content fits on a single page.

• I see, thank you very much. The computer I am using is locked down very much and therefore I am using an old version of MaTeX. I could not find the tutorial in the documentation. – kalix Mar 28 at 12:19
• @kalix If you can use Mathematica, then very likely you can install MaTeX. If you have M11.2 or later, just evaluate PacletInstall["https://github.com/szhorvat/MaTeX/releases/download/v1.7.4/MaTeX-1.7.4.paclet"]`. Otherwise use the install script on MaTeX's GitHub page – Szabolcs Mar 28 at 12:51
• Thanks, that works. I was afraid to update because recently other users with the same privileges as I did have problems installing MaTeX (I don't remember the exact error and the admin was not able to fix it). Installing worked without problems for me some time ago so I was afraid to trigger the same problem when updating. Debugging these problems is sometimes annoying without admin privileges. – kalix Mar 28 at 14:26
• @kalix You can suggest to the admin to contact me if he has trouble with the package. I can help him install it system-wide and advise on compatibility with various Mathematica versions. – Szabolcs Mar 28 at 14:35