fig2tex for MATLAB

fig2tex is a MATLAB function that produces a LaTeX file to replicate MATLAB figures using PSTricks macros. (the final LaTeX file requires the PSTricks packages pst-plot and pst-node, in particular).

I wrote fig2tex function out of frustration with the low quality of graphics produced from MATLAB figures using the standard print command. Basically, some of my problems were that

  • The label formatting did not match the surrounding text
  • Scaling figures distorted everything from dash lengths to fontsize
  • MATLAB -> figure -> eps > LaTeX cycle was too time consuming for small style changes to the figure, and these changes require you to have MATLAB.

With fig2tex, you have a LaTeX file that produces your figure in LaTeX + PSTricks. Hence, text style remains uniform throughout your document. Same thing goes for scaling. Moreover, small changes (like changing the label text) can directly be made on the LaTeX file.

Download

  • fig2tex.m (ver. 0.7 Date: 2005/6/27)
  • fig2tex.m (ver. 0.71 Date: 2005/7/20): This is a backward compatibility version for people who use MATLAB 6.5. It completely changes how the multiline strings are represented. However, I will continue the development from version 0.7, not from 0.71 as it makes life a lot easier to work with a cell of strings.

This script is free software; you may redistribute it and/or modify it under the conditions of the LaTeX Project Public License, version 1.3 or later.

Install

Put fig2tex.m in a directory in MATLAB's search path.

Example

Here is a figure in MATLAB.

As you see, all the labels are entered as LaTeX strings. You can turn on the built in LaTeX interpreter of MATLAB to get an idea of how these will look like when typeset. This has no effect on fig2tex.

Then you run

fig2tex(1,'fig1.tex',8);
        

in MATLAB. This produces a LaTeX + PSTricks file fig1.tex of a picture having a plot area width of 8cm.

You can then include this file in the LaTeX figure environment where you would otherwise include an EPS file.

\begin{figure}[h]
\centerline{
\input{fig1.tex}}
\end{figure}

Here are the figure, the resulting fig1.tex and the main file. The final PDF file looks like this.

Limitations and things to do

  • Markers on plots are not implemented
  • Positioning of multi line axis labels and plot titles may require a bit of trial and error. For now, a good rule of thumb: If you can see the label in the MATLAB figure and you leave some margin between the label and the figure boundary, nothing will be clipped in the final output.

Notes

  • You might see some weird positioning of labels if you preview the DVI file. This is usually a problem with the DVI viewer. Convert to PS and check again with GSView.
  • DVI -> PDF does not perform well. Instead convert DVI to PS and then to PDF.

ChangeLog and Contributors

  • 0.71 (2005/7/20)
    • Changed the way multiline strings are represented for backward compatibility for MATLAB 6.5. This will not be maintained in future versions, though.
  • 0.7 (2005/6/27)
    • Added support for dashdot style (Andrea Gallivanoni).
  • 0.6 (2005/6/27)
    • Combined the two functions into one .m file.
    • Added support for line colors (Andrea Gallivanoni).
    • Fixed the bug that caused string concatenation errors (Radu Serban, Andrea Gallivanoni).
  • 0.51 (2005/6/23 )
    • Included existence check for legends (Radu Serban, Andrea Gallivanoni).
    • Included type checking for lineseries objects (Radu Serban, Andrea Gallivanoni).
    • Now the license is LPPL.

 

 

you can send bug reports and feature requests to
ercan at isikun dot edu dot tr