Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I quite often would like to draw graphics in my $\LaTeX$ documents using Mathematica. I have encountered three problems. I would like to know if there are any workarounds to these problems

I would like to make my graphics homogeneous with my document. That means that I would like to use the same font in the graphics (labels for axis etc) as the main text. Mathematica does not support Computer Modern. I found a workaround using PSFrag, saving graphics as EPS. It is possible using PSfrag to rename the text in the graphic into $\LaTeX$ code. A big downside is that this method does not allow me to use pdflatex. Many other packages (hyperlink) therefore do not work.

Graphics3D objects are extremely big. If I save it using a bitmap, the picture usually becomes horrible.

I often would like to use transparency. If I use Opacity to make some part of the graphic transparent, the exported file in Mathematica is horrible.

share|improve this question
Theoretically you can get Mathematica to use Computer Modern, but it's a lot of trouble for little gain an imperfect results. You could start by downloading Latin Modern in OpenType format. Mathematica exports to PDF 1.4, a PDF specification too old to be able to embed OpenType fonts, so you'd need to convert them to TrueType. Then install the appropriate font (e.g. "LM Roman 10" is upright, optimized for 10 pt viewing), and make the plot with BaseStyle -> {FontFamily -> "LM Roman 10", FontSize -> 10}. – Szabolcs Jan 26 '12 at 9:22
This is not a satisfactory answer I know, but I thought it would be good to write it down. Even if you do this, mathematical text (formulae) won't appear in the same style as when typeset by LaTeX, you unless all you need is numbers for the axes, it's probably not worth the trouble. – Szabolcs Jan 26 '12 at 9:24
For Graphics3D objects, there is the Fortran code Polygone that reduces the size of postscript graphics by merging unnecessary polygons. – Simon Jan 26 '12 at 12:39
@Simon that could have been a good answer for my question here:… I encourage you to post it. – Szabolcs Jan 26 '12 at 14:25
@Szabolcs: problem is, I've never used Polygone - only heard about others using it. If you get it working and post an answer, I promise to upvote it! – Simon Jan 27 '12 at 1:47
up vote 34 down vote accepted

There are a few different parts to your question. I'll just answer the part about using psfragand pdflatex.

There's a package called pstool that automates the whole process of using psfrag with pdflatex.

For example, here's a graphics created in Mathematica 8

plot = Plot[Sin[Exp[x]], {x, -Pi, Pi}, AxesLabel -> {"e", "s"}]
Export[NotebookDirectory[] <> "plot.eps", plot]

da plot

Note the use of the single character names for the axes. This was discussed in the stackexchange question Mathematica 8.0 and psfrag.

You can use psfrag on this image and compile straight to pdf using the following latex file


Compile it using pdflatex --shell-escape filename.tex. You can optionally include a file plot.tex in the same directory which can contain all the psfrag code for plot.eps so that your main .tex file is tidier and the plot is more portable.
Here's a screenshot of the graphics in the pdf file:

share|improve this answer
I don't use LaTeX so this is solely out of curiosity but I notice that the conversion also converted the axes labels. Presumably that is to do with default fonts or something? – Mike Honeychurch Jan 26 '12 at 22:25
@MikeHoneychurch : The conversion happens with \psfrag{e}{$\epsilon$} in the LaTeX file. – Henrik Hansen Jun 21 '14 at 10:38
It is probably worth pointing out that pstools and psfrag do not work in MMA10. – Alexey Bobrick Oct 13 '14 at 14:30
@AlexeyBobrick If you look at the generated eps you can see that the fonts are by default outlined and not represented as text. This is probably a bug and will hopefully be fixed. See this comment by Szabolcs – Simon Oct 15 '14 at 3:30
@Thankyouforyourcooperation - It looks like Mathematica is still converting text to outlines (as mentioned in a comment above). Until they fix this psfrag won't work. I just submitted feedback on this issue, asking that the option TextOutlines works for EPS like it does for PDF export. – Simon Oct 24 '15 at 11:10

Exporting graphics with consistent font sizes

I'll show you my preferred way of exporting figures for use with $\LaTeX$.

I prefer to use consistent font sizes in figures. This means that I need to export PDF figures at the final print size and avoid scaling them within LaTeX. (Note that PDF files contain information about the physical print size of the document.)

Let's say we want a 10 cm wide figure that uses 10 point type. Taking an example figure from the documentation,

g = ContourPlot3D[
  x^4 + y^4 + z^4 - (x^2 + y^2 + z^2)^2 + 3 (x^2 + y^2 + z^2) == 
   3, {x, -2, 2}, {y, -2, 2}, {z, -2, 2}, Mesh -> None, 
  ContourStyle -> 
   Directive[Orange, Opacity[0.8], Specularity[White, 30]], 
  PlotPoints -> 30, MaxRecursion -> 5,

  BaseStyle -> {FontSize -> 10}] (* <-- specify text size in points here *)

Mathematica graphics

I increased the PlotPoints and MaxRecursion options, otherwise the raggedness of the surface will be noticeable at the high resolutions we will be using here.

I prefer to work in centimetres (and not printer's point, the default unit of Mathematica):

cm = 72/2.54;

Let's turn on the ruler (Window -> Show Ruler) and verify that the following is really 10 cm wide (you may also need to go to Edit -> Preferences and set the ruler units to centimetres):

Show[g, ImageSize -> 10 cm]

Mathematica graphics

As you noticed, exporting 3D objects as vector graphics is not ideal, so let's rasterize this figure at the correct size:

image = Rasterize[Show[g, ImageSize -> 10 cm], "Image", ImageResolution -> 600];

(Unfortunately Mathematica has trouble with scaling tick marks when rasterizing, so you may want to use an explicit tick specification if this is important.)

A resolution of 600 dpi ensures that it will look excellent in print, but rasterization may take a while.

Finally, export the figure to a PDF of the correct size:

Export["figure.pdf", Show[image, ImageSize -> 10 cm]]

(When using PDF, it is necessary to specify ImageSize within Show and not Export to avoid some problems.)

You can open the produced PDF, maybe even print it out, and you'll see that all the text is precisely at 10 point size.

The same principles can be applied to 2D graphics that export well as vector data:

g = Plot[Sin[x], {x, 0, 10}, BaseStyle -> {FontSize -> 10}]
Export["figure.pdf", Show[g, ImageSize -> 10 cm]]

Related reading:

share|improve this answer
While I have been using this workflow for a while, and I am sure there have been problems with exporting to size in some previous versions (6 or 7?), I recently realized that this convoluted workflow is not usually necessary in version 8. I don't have time right now to figure out if a more direct method of exporting to a given image size and resolution would work fine in all cases, so I am leaving this here as is. I'll update when I get the time to test. – Szabolcs Mar 19 '12 at 14:18

I would like to make my graphics homogeneous with my (LaTeX) document. That means that I would like to use the same font in the graphics (labels for axis etc) as the main text.

I recently wrote a package, called MaTeX, to solve this exact problem. MaTeX makes it easy to compile short LaTeX snippets and embed them in Mathematica notebooks or graphics.

Here's a quick demo:

<< MaTeX`

MaTeX["\\sin^2 \\varphi + \\cos^2 \\varphi = 1", Magnification -> 2]

MaTeX[Sin[x] + O[x]^8, Magnification -> 2]

enter image description here

funs = {Sin[x], Normal[Sin[x] + O[x]^8]};

Plot[Evaluate[funs], {x, 0, 2 Pi}, PlotLegends -> MaTeX /@ funs, 
 BaseStyle -> {FontFamily -> "Latin Modern Roman"}, 
 Frame -> True, FrameStyle -> BlackFrame]

enter image description here

I use it in conjunction with the Latin Modern fonts to achieve a consistent visual style.

This package grew out of my own needs so the functionality is fairly rudimentary at the moment (March 2015, MaTeX version 0.2). Feel free to send me suggestions, or just drop me a note if you find it useful.

I found a workaround using PSFrag, saving graphics as EPS. It is possible using PSfrag to rename the text in the graphic into LATEX code. A big downside is that this method does not allow me to use pdflatex.

You can still use pdflatex, but it comes with inconveniences. MaTeX solves this problem completely.

Another Mathematica package I learned about just after releasing MaTeX is MathPSfrag, which relies on PSfrag and should be able to create PDF output. However, I have never used it.

share|improve this answer
@Artem Please email instead, this comment thread is not suitable for troubleshooting. Include a notebook with you email showing what the FindFile command returns when applied to that file or its directory. – Szabolcs Jan 14 at 17:18

You can use Mathematica-generated PDF graphics in LaTeX, using the pdflatex engine. I have been doing this for years. You have several options

  1. Use a font such as Times that will embed properly in the PDF, and a LaTeX package that uses matching fonts, such as mathptmx, txfonts or tex-gyre Termes. (There are actually many different font options in LaTeX beyond Computer Modern. If the packages that come with your TeX distribution don't appeal, I have published some more, including for fonts like New Baskerville.)
  2. If you have a Mac, right-click on the graph and choose "Print Graphic" from the pop-up menu, and then "Save as PDF" from the resulting dialog. The PDF that results is a higher version that will embed the Latin Modern fonts. You might need to crop the resulting PDF in Preview before inserting it in your LaTeX file.

PDF format handles opacity properly, so this will also solve some of the other issues you mention.

share|improve this answer
I second this. Have done this many times as well. I usually had to play with resizing though - used convert under Linux to change image resolution etc (this was a few years ago, M5 - M6. Perhaps now this is unnecessary). +1. – Leonid Shifrin Jan 27 '12 at 10:51

About the size of 3d graphics, have you tried the ImageSize option in functions like Plot3D? For instance,

   Sin[x^2 + y^2]/Sqrt[x^2 + y^2],
   {x, -Pi, Pi},
   {y, -Pi, Pi}
  ImageSize -> 1000

exports a 527KB pdf file here, which looks OK on screen (without rasterizing, you get a 14.4MB file). You can increase the ImageSize if this is not enough.

EDIT: Actually, as Szabolcs points out in a comment, for this purpose ImageResolution is the right tool:

   Sin[x^2 + y^2]/Sqrt[x^2 + y^2],
   {x, -Pi, Pi},
   {y, -Pi, Pi}
  ImageResolution -> 300

as it takes care of font sizes etc.

share|improve this answer
I'd recommend ImageResolution instead which makes sure that every element of the image scales proportionally, including text and line widths. – Szabolcs Jan 26 '12 at 9:59
@Szabolcs you're right, I added it to the answer (if you don't mind). I am making some composite images for a paper and have been using ImageSize all the time, so I kept thinking of that. Thanks for the correction. – acl Jan 26 '12 at 10:09
When I was doing this a lot (2004-2009), I found that both ImageSize and ImageResolution are useful in combination, since one is not reduced to the other. May be it is different now, I have not checked. +1. – Leonid Shifrin Jan 27 '12 at 10:54
Here we may observe two of the top experts in this community -- people who have each given crucial help to countless users, answering thousands of questions -- both musing on the possible influence of ImageSize and ImageResolution on the scale and granularity of printed output. Is it not ludicrous that Mathematica should still, after all these years, require undocumented tricks and incantations simply to print something at a definite size? Then to top it off: "May be it is different now." As usual, whoever knows such details is keeping mum. – Ralph Dratman Dec 12 '14 at 23:55

I usually draw only the non-textual part in Mathematica export it as PDF (if the graphic is simple) and do all text typesetting in LaTeX to get a consistent use of fonts across the document. This is mainly manual labor. On the LaTeX side I use TikZ. This entry on TeX.SE might be a starting point.

There is a solution for SVGs comming from Inkscape. I have not tried it with Mathematica however.

Regarding the size of the Graphics Objects there is no automatic solution I know of, if you don't want to rasterize the pictures already in Mathematica. In computer graphics you would use back-face culling to remove all the triangles not visible. If you export for example one of the mathematica spikeys into a PDF and open it. The sheer number of triangles exported slows the viewer down so much that you can see the graphic building up slowly. And you can see how many triangles are overpainted. I once tried to reduce the number of triangles however my knowledge about the graphic objects within Mathematica is not good enough for this task. So I usually rasterize them in Mathematica and export them as PNG.

EDIT: Over at TeX.SE there is a similar question, where tools for an EPS toolchain are mentioned. Never tried them myself however. Gnuplot and LaTeX play well together when EPS is used, perhaps this work for Mathematica EPS too?

EDIT2: This entry in Tex.SE explains the wrapping of a picture with axis and labels typeset with the TeX fonts in TikZ by an example EPS scatterplot.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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