## Font style in Mathematica and .tex file

14

8

I am using Mathematica 8. I want to label the axes of figure in "Times" font.

Plot[Sin[z], {z, -2 Pi, 2 Pi}, Frame -> True,
FrameLabel -> {"z", "a(z)"},
LabelStyle -> Directive[FontSize -> 20, FontFamily -> "Times"]]


And the plot is

However in Miktex, when I am using FontStyle, Times then z is different. So I want to replace by

the second one is the default z appearing pdf generarted from .tex file in math mode. So what extra information should be given to Mathematica, besides "Times".

The second Z is Times Italic. To get that in Mathematica add the option FontSlant-> Italic to your Plot function call. – Sjoerd C. de Vries – 2015-03-02T19:33:32.287

did not resolve the problem for me in Mathematica 8 – zenith – 2015-03-02T20:19:23.587

You mean you did not get the italic version of the font? – Sjoerd C. de Vries – 2015-03-02T20:56:36.047

@ Sjoerd Now its been resolved. I made a mistake previously as i added FontSlant-> Italic in Label style. So that it italicized all the tikz labels also. Moreover, the required font was not visible in Mathematica 8.0, but when I saved it in .eps format and seen in the .pdf generated through Miktex it was visible. Thank you – zenith – 2015-03-02T21:12:56.633

If you can wait a couple of days, I'll have an answer for this. It's an accident that I'm doing this at the same time when you asked the question. I didn't even see your question until now.

– Szabolcs – 2015-03-03T00:15:54.060

My approach is to use a non-$\LaTeX$ (installed system-wide) font in the .tex source and pick the same font in Mathematica. You can do the tex part either using pdflatex with suitable packages, or xetex, luatex. – Jens – 2015-03-06T16:57:57.817

21

You can match formatting between Mathematica figures and LaTeX using a small package I just wrote for using LaTeX-snippets in Mathematica: MaTeX.

Here's an example:

<<MaTeX

Plot[Sin[z], {z, -2 Pi, 2 Pi},
Frame -> True, FrameStyle -> BlackFrame,
FrameLabel -> (MaTeX[#, Magnification -> 20/12] &) /@ {"z", "a(z) = \\sin z"},
BaseStyle -> {FontFamily -> "Latin Modern Roman", FontSize -> 20}]


Additionally, I used the Latin Modern font for tick labels to fully match the "LaTeX look", and set the frame to be black instead of gray.

If you prefer the Times font, we can do that too:

SetOptions[MaTeX, "Preamble" -> {"\\usepackage{amsmath,amssymb}", "\\usepackage{txfonts}"}];

Plot[Sin[z], {z, -2 Pi, 2 Pi}, Frame -> True,
FrameStyle -> BlackFrame,
FrameLabel -> (MaTeX[#, Magnification -> 20/12] &) /@ {"z", "a(z) = \\sin z"},
BaseStyle -> {FontFamily -> "Times", FontSize -> 20}]


1Today I also wanted to ask a similar question, but just when I had almost given up hope that a nice, clean solution would exist, I came across your answer and your easy-to-use package "MaTeX." Many thanks for developing such a convenient package (and for clearly documenting the steps needed to make it work)! – TMM – 2015-03-30T23:35:47.497

@TMM I'm glad you find it useful. Feel free to email me if you have any comments or suggestions. – Szabolcs – 2015-03-31T02:04:47.450

Actually there's one question I had, which may also be useful to readers of this website: is it possible to automate the MaTeXing further? i.e. Write something like MaTeX[Plot[x, {x,0,1}]] and it automatically TeXifies the tick labels on the axes? Alternatively, is there a convenient way to TeXify automatically generated ticks on axes? Many thanks again! – TMM – 2015-04-01T02:05:15.537

@TMM For ticks, I would recommend simply using the Latin Modern font instead of MaTeX. While theoretically it should be possible to auto-process tick labels, there are many obstacle ... Option 1: Use FullGraphics to render the axes and ticks as graphics objects, then go from there. FullGraphics has stayed neglected since Mma6 and it's practically unusable in v10. Option 2: Get the tick labels using AbsoluteOptions, then process those. This works better, but not perfectly. Often, what AbsoluteOptions returns is quite a bit different from the automatically rendered ticks. – Szabolcs – 2015-04-01T18:15:57.023

@TMM Maybe like this. Note how AbsoluteOptions changes the ticks though ...

– Szabolcs – 2015-04-01T18:20:35.303

That example doesn't work on my pc (Mma 10.0.1), with errors of the form Axes::axes: "{{False,False},{False,False}} is not a valid axis specification." (and similar complaints about Ticks). Something simple like AbsoluteOptions[Plot[Sin[x], {x, 0, 10}, Ticks -> Automatic], Ticks] returns the same errors; I guess AbsoluteOptions is a bit buggy. – TMM – 2015-04-02T13:37:47.627

@TMM Apparently this is why (it's a bug), and it's fixed in 10.0.2 ... yes, the main reason why this is difficult is because AbsoluteOptions is so unreliable and FullGraphics is near useless ...

– Szabolcs – 2015-04-02T13:41:50.447

Thanks for the link, that indeed seems to be the problem. I upgraded to Mma 10.1 now, and although it works with Ticks, the same idea does not seem to work with "FrameTicks" for which the same bug still seems present. – TMM – 2015-04-03T20:50:49.153

I am still not able to get it to work with FrameTicks, and typing in all the ticks for each figure is too much work as I am using this to style tons of different figures used in my thesis. Also specifying ticks manually seems to have the unwanted side-effect of removing the smaller ticks inbetween the larger ticks. For now I'll just keep the ticks the way they were, and use your package for styling the frame labels. – TMM – 2015-04-04T00:02:06.380

@TMM I use the CustomTicks package from here when I need more control over ticks.

– Szabolcs – 2015-04-04T00:51:52.873

I just figured out how to combine CustomTicks with MaTeX: MapAt[Function[x, MaTeX[x]], LinTicks[-1, 1], {All, 2}] will take the tics generated by LinTicks and apply MaTex to the actually printed labels, i.e., to the second column of the output of LinTicks. This even solves the problem of the ugly 1. being printed instead of 1 or 1.0. – Felix – 2016-11-08T20:51:44.377

@Felix Try this for a speedup: Transpose@MapAt[MaTeX, Transpose@LinTicks[-1, 1], 2]. Applying MaTeX to a whole list of expressions will call LaTeX only once. This works in version 1.6 or later. Personally I don't often use MaTeX for ticks. I just set the font to Latin Modern instead. I use MaTeX only when I have something more complicated than decimal numbers in the ticks, e.g. for $\frac{\pi}{3}$. – Szabolcs – 2016-11-09T07:48:28.167

MaTeX looks very nice. However, when I try to install it, I get

In[83]:= Needs["PacletManager"] PacletInstall["C:\Users\iosif\Documents\MaTeX-1.7.3"]

During evaluation of In[83]:= PacletInstall::notavail: No paclet named C:\Users\iosif\Documents\MaTeX-1.7.3 is available for download from any currently enabled paclet sites.

Out[84]= \$Failed

How could this be fixed? Thank you. – Iosif Pinelis – 2018-01-22T21:21:16.223

@IosifPinelis Try PacletInstall["C:\Users\iosif\Documents\MaTeX-1.7.3.paclet"] (not the added extension), and make sure that file actually exists. – Szabolcs – 2018-01-22T21:49:42.487

@Szabolcs : Thank you very much for your hint, and sorry for missing the paclet extension. However, now I have a nice looking font for the tick labels with the Times font, but not nearly as nice as in your picture with Latin Modern. The notebook is at the link https://my.pcloud.com/publink/show?code=XZ8NnH7Zmwf0OT6s3TJhdhvBzNYByRyJAyWX . Could you please help me with this as well?

– Iosif Pinelis – 2018-01-23T02:40:29.277

@IosifPinelis Probably this will help: https://mathematica.stackexchange.com/a/161530/12

– Szabolcs – 2018-01-23T10:08:40.243

@Szabolcs : Thank you very much again -- for such a great product as MaTeX and your responses. It works great with CMU fonts. – Iosif Pinelis – 2018-01-23T15:20:27.707

Can I use that package in mathematica online? – Rodrigo – 2019-04-12T12:29:48.263

@Rodrigo Only if you could install a full TeX system in Mathematica Online (i.e. no). – Szabolcs – 2019-04-12T12:39:25.043

Note for newbies, to leave math mode, it will look like this: {"\textrm{Frequency}", "a(z) = \sin z"} – axsvl77 – 2019-07-12T14:54:06.490

1@axsvl77 Just use \text, not \textrm. This is shown in several examples in the documentation. Suggestions on how to improve the documentation are always welcome, but keep in mind that just adding more information and making it longer is not going to help—people already don't read it. – Szabolcs – 2019-07-12T16:53:21.987

@Szabolcs Guilty! I didn't read it once I got it working. Maybe I should say "Note for newbies, read the documentation!" – axsvl77 – 2019-07-13T00:57:56.050

Works great! Thank you! – Rainer Glüge – 2019-11-19T10:40:33.940