Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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?

share|improve this question
    
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. –  Jens May 4 '12 at 15:52
add comment

5 Answers

up vote 9 down vote accepted

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

"2012-05-04_Plot_Export"

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

StringReplace["\\begin{figure}[!htb]\\centering
 \\includegraphics[width=1\\textwidth]{XXX}
 \\caption{Put your caption here.} 
 \\label{fig:XXX}
 \\end{figure}
 ", "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).

\begin{figure}[!htb]\centering
    \includegraphics[width=1\textwidth]{2012-05-04_Plot_Export}
    \caption{Put your caption here.} 
    \label{fig:2012-05-04_Plot_Export}
    \end{figure}
share|improve this answer
    
@Szabolcs thanks for getting rid of the TeX formatting ;-) –  Yves Klett May 4 '12 at 13:13
    
MathJax will try to interpret the LaTeX it finds, producing these funny boxes –  Szabolcs May 4 '12 at 13:19
add comment

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

Edit

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:

Map[SetOptions[#, 
    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.

share|improve this answer
    
What exactly do you paste into LyX? –  Yves Klett May 4 '12 at 15:50
    
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). –  Jens May 4 '12 at 15:53
    
Uh... agreed, that sure is straightforward! –  Yves Klett May 4 '12 at 15:59
add comment

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

project
project/figures

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

ResetDirectory[]

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.

share|improve this answer
1  
Plus, pdflatex also nicely works together with PNGs and JPGs without the need to add suffixes in the document source. –  Yves Klett May 4 '12 at 13:42
add comment

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.

share|improve this answer
add comment

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.

share|improve this answer
    
+1 We are very coherent this morning - 3 min difference - the irony ;-) –  Vitaliy Kaurov May 4 '12 at 12:58
    
@Vitaliy Fortunately, I'm going out for the day! –  Mark McClure May 4 '12 at 13:58
add comment

Your Answer

 
discard

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.