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

I am writing a student thesis in LaTeX and need to import several figures created in Matlab. I found some programs to do this task:

  • matfig2pgf
  • matlab2tikz
  • fig2texPS

The problem is that using one of the first two I get a problem that the tex capacity is exceeded. The figures have quite lots of data points which could be the reason why tex gives up. Using the latter program I cannot export subplots; I have to create a image for each. Also I am not sure, if I get the single images arranged in latex in a nice fashion (not yet tasted). Also I have to do it using dvi/ps/pdf for each file separately.

My question is: What is a good way to include the images from matlab in latex?

I ask because I do not want to have to create all the files twice to more because any limitation of any program is reached.

share|improve this question
    
An MWE (Minimal (non)Working Example) would be helpful here –  Boris Nov 10 '11 at 18:37
1  
2  
    
@CountZero No, but interesting in general. The problem comes up for even 1 single figure as it is quite large. I am not even able to import one figure. –  Christian Wolf Nov 10 '11 at 18:57
    
There is fig2tex [mathworks.de/matlabcentral/fileexchange/7884] that could be considered as well. The beauty of using the tools you mentioned is that you have to think about what you really want to keep in the figure: are a zillion points necessary? You may also be interested in compiling each figure separately by using the externalization feature of Tikz. –  pluton Nov 10 '11 at 20:37

6 Answers 6

up vote 11 down vote accepted

Graphics with lots of points are always a challenge for TeX-based processors.

However, I am convinced that both memory and time limitations can be tackled to "reasonable" degree (i.e. to reduced pain).

There are two solutions which should both be considered:

  1. to increase TeX's memory (or to circumvent the limitations of pdflatex).

  2. to reduce the number of times the graphic is being processed by TeX (compile once, use often).

While the comments to your question already indicate some solutions concerning (2.), you may need more input for (1.). In fact, I believe that (1.) is the more pressing issue which cannot easily be solved by (2.).

Concerning (1.), I know that one solution works pretty well: to increase the limits. The pgfplots manual contains details instructions for both windows and linux how to enlarge the memory limits. I consider that to be a mandatory step for you - and invite you to follow the link above and read chapter "6 Memory and Speed Considerations" in the pgfplots manual. The chapter contains readily deployable configuration examples. It might be that switching to lualatex instead of the conventional tools (pdflatex or latex/dvips) might also solve the memory problem (I do not know).

Concerning (2.), you can use the standalone package (this site contains a lot of examples). This should work with any of your packages. However, if you use matlab2tikz, I find the TikZ library external very useful here - I tailored it to convert each figure to a separate pdf without changing the original document. Note that matlab2tikz uses pgfplots, so the link mentioned above might be very useful (it also contains a brief description of this automatic image externalization).

I believe that the steps above should help.

But there are always cases where one might also want to know about alternatives.

Here are some of them. I did not post them directly because I have the impression that you may already have an existing workflow and they may not fit - but perhaps you are interested in my experiences anyway:

a) you could try to implement (selected) figures directly in TeX. I did so by means of pgfplots which is quite powerful. I like the fact that I could define document-wide consistent styles and that the single documents are, well, often easier to read than autogenerated code. In fact, once I started using pgfplots instead of matlab, I found that both simpler to maintain (.tex files instead of .m files) and prettier. I dropped all of my matlab scripts eventually and used only pgfplots in the end.

b) if your vector graphics are too large, you may want to consider using bitmap graphics and use TeX to overlay axis descriptions over the bitmap. pgfplots comes with its \addplot graphics and \addplot3 graphics commands to streamline the process. You can also post feature requests to Nico Schloemer (author of matlab2tikz) - perhaps he is willing to add automatic bitmap conversion with overlay axes. Details for such an approach can be found in the aforementioned pgfplots manual (including application examples). Bitmap graphics have the advantage that they render much faster in all viewers - and for surface plots, it does not matter anyway.

share|improve this answer
1  
Also: 3) Use LuaTeX instead of pdfTeX as LuaTeX can dynamically allocate as much memory as it needs. –  Sharpie Feb 3 '12 at 15:11
    
@Sharpie that's good to know, thanks for the hint. –  Christian Feuersänger Feb 4 '12 at 16:10

From the command line in MATLAB I usually use

print -dpsc myfigure.eps

You can then include the graphic in your TeX file using \includegraphics from the graphicx package (as demonstrated below).

Rather than using subplots directly in MATLAB, I would usually export each picture separately, and then use the subfigure environment from the subcaption package- a complete MWE example follows.

screenshot

\documentclass{article}
\usepackage{graphicx}   % for including graphics
\usepackage{caption}    % for captions
\usepackage{subcaption} % for sub-captions

\begin{document}

\begin{figure}
 \centering
\includegraphics[scale=0.5]{myfigure}
\end{figure}

\begin{figure}
 \centering
 \begin{subfigure}{0.49\textwidth}
    \includegraphics[width=\textwidth]{myfigure}
    \caption{Sub caption}
 \end{subfigure}
 \begin{subfigure}{0.49\textwidth}
    \includegraphics[width=\textwidth]{myfigure}
    \caption{Sub caption}
 \end{subfigure}
 \caption{Main caption}
\end{figure}

\end{document}

edit

Following the comment, see Why can't pdflatex print EPS figures? for details of how to run pdflatex with .eps images- it worked by default without any tweaking on my TeXlive 2011 distribution.

share|improve this answer
    
@ChristianWolf See my edit –  cmhughes Nov 10 '11 at 20:19
1  
The problem here is that matlab creates much too large images. Scaling in LaTeX also scales the text sizes. Also the font is not the usual latex font. Ideas? Main problem is that latex document is in A5 and the eps is larger. –  Christian Wolf Nov 10 '11 at 20:34
1  
The range of Matlabs print command output formats covers pdf, eps, emf, tiff, jpg, png,... –  matth Nov 10 '11 at 21:36

It is possible to wrap an image with pgfplots to get the goodness of LaTeX for text, but without having to process the image itself. The following is a CWE using a png generated in Matlab which had the axes turned off, and was then trimmed with Imagemagick.

\documentclass[10pt]{article}
\usepackage{graphicx}
\usepackage[hang,small,bf,width=.90\textwidth]{caption}
\usepackage{textcomp}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.3}
\pgfplotsset{colormap={jet}{rgb255(0)=(0,0,143);rgb255(8)=(0,0,255);rgb255(24)=(0,255,255);rgb255(40)=(255,255,0);rgb255(56)=(255,0,0);rgb255(64)=(128,0,0)}}

\begin{document}
\thispagestyle{empty}
\begin{figure}[ht]
\centering
\begin{tikzpicture}
    \begin{axis} [
        enlargelimits=false,
        axis on top,
        xlabel=$A/\Omega$,
        ylabel=$\epsilon_0/\Omega$,
        colorbar,
    ]
        \addplot graphics [
            xmin=0,
            xmax=0.5,
            ymin=-3,
            ymax=3,
        ] {example};
    \end{axis}
\end{tikzpicture}
\caption{$\Omega=1.2$\,GHz, $A/B=1.5\%$, $\Delta=350$\,MHz, $\Gamma=0.02$\,MHz, $t=1$\,\textmu s}
\end{figure}
\end{document}

This used the following file (example.png): Matlab figure with axes removed, and trimmed.

to generate:

pgfplots figure using a separate canvas.

I suggest doing something like this for very heavy plots. In this case it makes sense because a pseudo-colour plot is made of patches, which are more efficiently represented with a bitmap. If you're doing something in 3D, it may also be a better idea.

share|improve this answer
    
on my web browser, you image is completely screwed up... I would expect something like that for corrupted images (perhaps scanlines being interpreted in a wrong way?). Are you sure the image is correct? –  Christian Feuersänger Nov 12 '11 at 11:02
    
It certainly is screwed up. You should be seeing mainly blue with oscillating bands of colour from left to right. This was an accident due to setting the tolerance on ode45 too low, but that's not the point of this answer, and I didn't want to upset my co-authors on a paper we haven't submitted yet. ;) –  qubyte Nov 12 '11 at 11:32
    
ah ok - in this case I see what you wanted to show. Thanks for the explanation! –  Christian Feuersänger Nov 12 '11 at 12:41
    
No worries. To be honest, I pulled this from the junk pile. I probably should have taken the time to generate some demo data, but it's not really necessary now. –  qubyte Nov 12 '11 at 12:56

I agree with @cmhughes, in that i really like the print command. To avoid margin and text resizing problems: I really like to take a lot of control when exporting my figures by pre-defining the margins and paper-size to be my desired figure size. This makes fonts size correctly, and lets you choose the margins you want.

First I define my figure width and height (these are in metric, but you can use US units also).

FigW=13.49414;
FigH=21.08462*.3;

Next, I set my figure properties. First I define the text interpreter to be latex, and then I set the paper size. You can define margins in the paper position parameter, but i prefer to do that later. if you want to use inches, that's fine here. Finally the 'Position' option just makes the figure on screen look identical to what will be printed to PDF. The first two parameters in 'Position' are the screen position and the second are the width and height of the figure.

Figure1=figure(1);clf;
    set(Figure1,'defaulttextinterpreter','latex',...
                'PaperUnits','centimeters','PaperSize',[FigW FigH],...
                'PaperPosition',[0,0,FigW,FigH],'Units','centimeters',...
                'Position',[1,10,FigW,FigH]);

I then use the subaxis package from the mathworks file exchange to take control of my margins, padding and spacing of all my subplots. For one plot this might look like:

subaxis(1,1,1,'Margin',.1);

Finally, I export to PDF using the print command in matlab:

print('-dpdf','-r500','filename.pdf')
share|improve this answer

a simpler approach is to :

  1. access the "print preview" in the Figure's "File" menu,
  2. select "landscape",
  3. adjust : (margins, width, Height, paper size,...etc),
  4. print it using "PDF Creator" or other pdf printer software.
share|improve this answer
1  
Welcome to TeX.SX! –  Papiro Jun 30 '13 at 22:44

If you're willing to try an open-source alternative to MATLAB, and lucky enough that your MATLAB scripts run on GNU Octave too, you can try its print command. It can export to a LOT of formats, including:

  • PS, EPS, PDF + supplementary LaTeX files to \input
  • PGF or TikZ

Also check out Problems with including a tikz tex file generated by Octave (MatLab clone).

share|improve this answer

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.