What is the quickest and best way to export a plot image from Mathematica to a $\LaTeX$ Editor? Is there something quicker than exporting it to an image and then including it in the $\LaTeX$ document?

  • $\begingroup$ Exporting from Mathematica is not the fastest way. Copying and pasting is. You just have to use a LaTeX editor that supports pasting graphics. I have been using LyX for that for years because it does that automatically. I can't even imagine not having that copy-paste functionality. $\endgroup$ – Jens May 4 '12 at 15:52
  • $\begingroup$ does not work with WinEdt $\endgroup$ – Xavier_B May 12 '17 at 8:40

I usually Export to hi-res PNG bitmaps for ease of use (there are a number of discussions on how best to export high-quality images on this forum. Take a peek at the right column of this page under "Related"). Personally I like notebooks that do not need any mouse-clicking or any other user interaction to produce output which makes reruns that much more comfortable and and most of all reproducible.

For ease of use when dealing with many different graphics I prepare a notebook for each of these and also generate the LaTeX code for insertion in my document. The graphics get their name from the notebook so you can easily spawn different versions by renaming the notebook.

plot = Plot[Sin[x], {x, 0, 10}]

Mathematica graphics

(*gfxname = StringTake[FileNameSplit[NotebookFileName[]][[-1]], {1, -4}]*)

EDIT: more concise and portable:

gfxname = FileBaseName[NotebookFileName[]]


Export[gfxname <> ".png", plot];

 \\caption{Put your caption here.} 
 ", "XXX" -> gfxname]

The resulting output can be copied as plaintext or you might even splice it directly into your document (although this is a bit much for casual use).

\caption{Put your caption here.} 
  • $\begingroup$ @Szabolcs thanks for getting rid of the TeX formatting ;-) $\endgroup$ – Yves Klett May 4 '12 at 13:13
  • $\begingroup$ MathJax will try to interpret the LaTeX it finds, producing these funny boxes $\endgroup$ – Szabolcs May 4 '12 at 13:19

The absolutely fastest way I know to get high-quality latex output is:

  • Make sure you use LyX as the editor (http://www.lyx.org/)
  • In MMA, highlight the plot, select "Copy as ... PDF" from the Edit menu
  • In Lyx, paste wherever you want, that's it.
  • The PDF will be saved under a default numbered file name (you can optionally rename it)
  • Run pdflatex from the View menu


Copying as PDF may not be available on all platforms - but you asked for the fastest way to export a plot image to a $\LaTeX$ editor, and this is it. My platform is Mac OS X. On other platforms, LyX is also available, but the "Copy As..." function may not offer PDF. Then you have to choose whatever other format that platform offers.

Edit 2: copying complex graphics: 3D plots, contour plots, etc.

As has been noted in this post on $\LaTeX$ and Mathematica, you can no longer rely on EPS as an acceptable file format for exporting graphics, because it can't handle opacity and can't be used in pdflatex workflows.

Unfortunately, this means that answers based on Save As ... LaTeX document are simply not ready for production use at this point, because that method relies on EPS files.

On the other hand, copying as PDF can also have one problem: the large file size created especially when attempting to convert Graphics3D objects to PDF.

Fortunately, that can be avoided by making sure that Mathematica automatically rasterizes 3D graphics upon export to PDF. There is an export option "AllowRasterization"->True, but I found a method that produces better quality rasterization without setting that option. The reason it's relevant here is that this approach works with copy-and-paste, too. It's described here and here, and I'll just give the recipe below: start the notebook with this initialization:

    Prolog -> {{EdgeForm[], Texture[{{{0, 0, 0, 0}}}], 
       Polygon[#, VertexTextureCoordinates -> #] &[{{0, 0}, {1, 
          0}, {1, 1}}]}}] &, {Graphics3D, ContourPlot3D, 
   ListContourPlot3D, ListPlot3D, Plot3D, ListSurfacePlot3D, 
   ListVectorPlot3D, ParametricPlot3D, RegionPlot3D, RevolutionPlot3D,
    SphericalPlot3D, VectorPlot3D}];

Now all exported and copied PDF of 3D graphics will be a wrapper for a print-quality high resolution image instead, and pasting it into LyX works in the blink of an eye.

The list of 3D plot functions in the command above can be extended to include some notoriously unwieldy 2D plot functions as well, e.g., ContourPlot, DensityPlot etc. They have a lot in common with 3D graphics in that they tend to produce huge numbers of polygons to create colored regions or gradients. For my own use, I didn't include these functions in the list above because I prefer to employ a couple of homemade substitutes for these functions that produce rasterized color gradients in their own special way.

  • $\begingroup$ What exactly do you paste into LyX? $\endgroup$ – Yves Klett May 4 '12 at 15:50
  • 3
    $\begingroup$ The PDF from the clipboard, as I said. It is automatically saved by LyX as a PDF with the LaTeX document. What happens when you press the paste key is that LyX recognizes when the pasteboard is able to provide a PDF representation of the object to be pasted, and then it writes that representation to a file (it also creates the \includegraphics code, of course). $\endgroup$ – Jens May 4 '12 at 15:53
  • $\begingroup$ Uh... agreed, that sure is straightforward! $\endgroup$ – Yves Klett May 4 '12 at 15:59
  • $\begingroup$ When exporting a 3D graph after initializing the notebook with the command described above, the shading of the 3D plot is lost: instead of gray shading, I get a completely black envelope, useless for a scientific paper... $\endgroup$ – Xavier_B May 12 '17 at 8:51
  • $\begingroup$ @Xavier_B Can you give a simple example? Maybe you just have to add another of the 3D commands to the list run through SetOptions... $\endgroup$ – Jens May 13 '17 at 17:28

If you create a notebook with some plots and then save it as LaTeX, not only will the images all be generated but so will LaTeX code that you can use to include those images.

  • $\begingroup$ +1 We are very coherent this morning - 3 min difference - the irony ;-) $\endgroup$ – Vitaliy Kaurov May 4 '12 at 12:58
  • $\begingroup$ @Vitaliy Fortunately, I'm going out for the day! $\endgroup$ – Mark McClure May 4 '12 at 13:58

One way to speed up the workflow is to batch process all your images and even formulas if you have many. Basically do all your work in the Mathematica notebook placing distinct graphics, formulas and text in separate cells. Then choose File > Save As... and pick LaTeX Document (*.tex) from the drop down menu of popped up window. This will produce a set of .EPS files for each of your graphics stored in separate cells and a coherent .TEX file with text, formulas and calls to the .EPS images.


The way I do this is by creating a directory structure like the following:


The LaTeX file is in project, the figures are in figures. I put the notebook(s) used to generate the figures in project as well, but you can make a separate directory for them.

The notebooks start with

SetDirectory@FileNameJoin[{NotebookDirectory[], "figures"}]

and ends with


The figures are then Exported to PDF files. Some tips on how to do this effectively are in this thread:

The key point is that I export all figures to the correct final size, ensuring that the font sizes will be consistent (e.g. in all figures the fonts will be precisely 8 pt big). (Re-)evaluating the notebook will produce all figures and save them directly into the figure directory.

For the LaTeX part, I use the graphicx package and compile everything with pdflatex. (pdflatex directly supports PDF, PNG and JPEG images.) In the preamble I include \graphicspath{{./figures/}}, so I can avoid specifying the full figure path. I include the figures using \includegraphics{figname}, without writing the file extension. This way, if later it turns out that everything needs to be converted to EPS from PDF (figname.pdf --> figname.eps), I won't need to modify the LaTeX source, I can simply switch from compiling with pdflatex to compiling with latex.

If some figures need to be post-processed manually using some illustration software, I export them to a separate directory, then copy the final result manually. I keep all figures under version control (in addition to the figure notebook), in case re-evaluating the figure code will overwrite and break something.

  • 1
    $\begingroup$ Plus, pdflatex also nicely works together with PNGs and JPGs without the need to add suffixes in the document source. $\endgroup$ – Yves Klett May 4 '12 at 13:42

I just figured out the following workaround imitating the psfrag convenience while avoiding intense programming or additional software:

  1. Create a new PlotTheme as described here. It should contain the Latex fonts i.e. FontFamily->"CMU Serif", the font size of the document to be created, i.e. FontSize->11 and the final size of the plot via ImageSize->425. The latter is necessary for reproducible font sizes. My result looks like this:

    DefaultPlotStyle->Thread@Directive[ColorData[97,"ColorList"], Thick],
    Frame ->True, Background -> None, LabelStyle -> {FontSize->11,           
    FontFamily -> "CMU Serif"}, ImageSize -> 425, PlotMarkers -> {Automatic, Small}];
  2. Use the new PlotTheme on the figure like so: Plot[...,PlotTheme->"MyTheme"] and export as EPS

  3. Import into Latex with size of 100%.

I ended up creating a small version with 212pt Image Size and a larger for full-page plots of 425pt. Also, I made a package to have them ready at every session. This imitates psfrag in the way that if the document font size or -style changes, the plots can be redrawn quickly by simply changing the custom themes and exporting again.

This is not a proper solution to the problem, but an easy workaround, which should be sufficient for most cases. It might not be enough for a journal article, though:

In general it is a shame that Mathematica cannot separate graphics and text upon export. Matlab and most other Software packages have much better latex export functionalities. Maybe it is just a marketing thing - making it hard to avoid the Mathematica style. In any case a much-needed feature for v11!


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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