matlab figures with latex fonts

I realize this question might have been asked already, but I'm relatively new to LaTeX and matlab so I need some help.

I'm trying to export a figure for inclusion in my tex document (to be compiled using pdflatex). The figure contains axes labels with LaTeX fonts (xlabel(...,'interpreter',latex)).

I've looked extensevely on the web but have not found a satisfactory solution. My best solution so far is to produce a png file by using the savefig.m function, but the quality is not ideal

I'd be very grateful for any hints.

-
Welcome to TeX.sx! Please add a minimal working example (MWE) that illustrates your problem. A tip: You can use backticks  to mark your inline code as I did in my edit. –  Kurt Oct 21 '12 at 0:34
Matlab allows you to save figures in eps format, which maybe preferable over png format. I am not sure what you use in savefig.m, but saving figures can be done by saveas() command. –  mythealias Oct 21 '12 at 2:41
Possible duplicate: Matlab to pdflatex with text properties –  Qrrbrbirlbel Oct 21 '12 at 18:20

These are the possibilities for you (I use the first two for my work):

Way out 1

You may try matlab2tikz and export your figures into tikz code.

Way out 2

Try matfig2pgf and get pgf code for your figure. matfig2pgf also provides a nice menu in the matlab figure window, thus some what more friendly.

Way out 3

If you a a PSTricks fan try fig2texps and get pstricks code.

Way out 4

Try plot2svg and get svg diagrams. But this is somewhat over kill as you have to again find ways to insert svg figures into latex.

Way out 5

If you have surface plots, you may look at surf2latex.

You can search for other applications in mathworks matlabcentral/fileexchange and look for various (numerous) ways. Also use different key words like pgf, tikz, PSTricks, latex etc in your search on that page. Hope this is useful.

How to get some peace of mind

For all plotting needs you may start using pgfplots. Use matlab to get the datapoints from your program, and use pgfplots to plot. For details, see pgfplots documentation. pgfplots can also be used with gnuplot seemlessly. Hence we can use the computing capabilities of gnuplot also.

An example:

\documentclass{article}
\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}[scale=1.0]
\begin{axis}[xlabel=$x-$ label and some text,
ylabel=$y-$ label
]
%
\addplot[domain=-10000:10000, no markers,green,ultra thick] gnuplot{14*x - x^2 + x^3 - x^4};
%
\end{axis}
\end{tikzpicture}

Some text to show the similarity of fonts in figures and main body.

\end{document}


You need to have gnuplot installed for this to work and enable --shell-escape option for pdflatex.

-
Or leave all these MATLAB begging methods and use pgfplots directly :) –  percusse Oct 21 '12 at 1:00
@percusse: How did I forget that? :( Adding immediately to the answer :) –  Harish Kumar Oct 21 '12 at 1:13

I can suggest some other options but they are not completely straightforward. You may give it a try.

Besides savefig.m there are popular tools at mathworks central that can export matlab figure such as export_fig,matlabfrag and laprint.

Out of them matlabfrag and laprint split the matlab figure into graphic stream as.eps and text stream as '.tex'. During latex->dvips->ps2pdf compilation the PDF of the figure with latex font embedded is done using psfrag package .

For a simple demonstration of laprint and matlabfragsee the link below Matlab graphics with LaTeX and PDF document here. You may need pstool package to make psfrag package work during pdflatex compilation.

For examples of export_fig can be found here

Search with some keywords and tags as shown in this link http://tex.stackexchange.com/search?q=matlab+pdflatex

One of the related questions to this topic What is the best way to include Matlab graphics?

PS: I use export_fig and matlabfrag mostly.

UPDATE: with mlf2pdf.m we can run the 'latex-dvi-ps-pdf compilation with psfrag commands' inside matlab. we dont need pstool or psfrag in latex document. here is the very less popular unknown magic function. http://www.mathworks.com/matlabcentral/fileexchange/28545-matlabfrag-to-pdf

-

I recently learned of another approach over at MATLAB Answers which involves overloading the way MATLAB handles TeX and LaTeX labels. Basically when you create a text label in a figure that has its interpreter set to TeX or LaTeX, MATLAB processes that string with a function called tex.m. This function creates a MATLAB string that is a fairly typical .tex file. It has a \documentclass and a \begin{document} and an \end{document}. The document class is mwarticle which is open source and is essentially identical to the article class with a few spacing and page layout changes. The MATLAB string is then passed to a closed source .mex file which does the processing and creates an image. It seems like the .mex file essentially calls some sort of tex program. This understanding presents two solutions.

The first is for the string to add packages. This is a little difficult since you need to include the path to the package. It is also not clear to what extent the program called by the .mex file is fully tex compliant. The string that MATLAB creates includes \nofiles and it is not clear how multiple passes are handles.

The second, and fun solution in my mind, is to overload tex.m to call your own TeX distribution. In this way you could do whatever you want with your MATLAB text labels. You could even include cross references and pgfplots` and whatever else you want.

-