fig2texPS - Export Matlab Figure to LaTeX
Date: 03.11.2010 Author: Peter Scholz Email: contact@peter-scholz.net Version: 0.5.3
This MATLAB function converts a figure to a LaTeX file with PSTricks compatible macros. The packages pstricks, pst-node, pst-plot, pst-circ and moredefs are required. A minimal example latex code is generated at each time this script is run. The tex file can also be a stand-alone file so that the paper size matches the figure outline.
Contents
Motivation
The conversion tool has been written for the following reasons (compared to eps with psfrag):
- With eps with psfrag it is cumbersome to replace all tick labels
- When replacing text in a legend with eps with psfrag, the size of the legend's box is NOT adapted -> this is different in this tool because PSTricks macros are used and the box size is adapted to its content
- When resizing an eps-Figure, the widths of all lines are scaled as well -> With this tool, all linewidths are equal independent on the size of the figure. This guarantees a neat appearance in documents with many different figures
- eps-Figures, created by Matlab have a surrounding box that may be unwanted sometimes, especially in two-column documents. -> Here, the box size can be chosen to be either minimal or fixed
- The output of this tool is a tex file that is easy to read. For instance, each grid line is one line in the TeX document. This offers the possibility to finetune the figure with any of the powerful PSTricks macros
Properties
The aim of this function is to create a tex-code picture of a Matlab figure in PSTricks syntax that can still be adapted after creation.
Supported Features:
- 2D plots with arbitrary number of lines and patches
- Linear and logarithmic axes and second y-axis
- Grids, subgrids, ticks and subticks
- Arbitrary colors, linestyles and markers
- Labels, title, legend and colorbar
- Annotations (TextArrows and TextBoxes) are supported
- GUI, where the fontsize, plotsize and the export file location can be set
- Lines with many datapoints can be compressed in order to reduce file size and compilation time (copied from Matfig2PGF by Paul Wagenaars in 2008)
- Convergence Triangles can be shown for logarithmic plots
- Support of additional code in PSTricks syntax
Changes to Version 0.52 from 10.06.2010:
- small bugfixes
- dataplot -> psline replaced, option linejoin=1 introduced
- marker more bold
- fixed a bug for vertical lines that produced NaN values when cut
- enhancement for patches which are cut at the axes limits
- Colorbar enhancement, if the tight inset of the right side is larger, this value is used instead
- fixed a bug for axes without tick labels
NOT Supported in this version:
- 3D Plots or contour plots
- Subfigures (if a figure with multiple axes is chosen, a dialog box appears that allows to activate a certain axes)
- Other annotations than TextArrows, and TextBoxes
- Reverse Axes
The matlab code is based on the fig2tex file by Ercan Solak in 2005. The key modifications are that the ratios can be set, implementation of grids and logarithmic scale. The algorithm to reduce the number of large data sets is copied from Matfig2PGF by Paul Wagenaars in 2008. Bertrand Scherrer added the support of textboxes on 09.06.2010
Bugs and ToDo's
- 26.02.09: The annotations (TextArrows) are not shown in the visualized figure, this has to be implemented
- 20.05.10: If patches are cut, the algorithm works in principle but the patches are divided into multiple patches according to the algorithm used for lines. This is wrong, instead a merging must be enforced in order to draw a straight line at the border...
- 29.06.10: Bug if a legend is added for patches
Syntax
fig2texPS() fig2texPS(fignum) fig2texPS(fignum,fname) fig2texPS(fignum,globals)
Description
fig2texPS() exports either the activated figure (gcf) or a test function, if no figure is present.
fig2texPS(fignum) exports the figure with the number fignum.
fig2texPS(fignum,fname) exports the figure with the number fignum and sets its name to fname. The name can be changed in the Figure Export Properties later on.
fig2texPS(fignum,globals) exports the figure with the number fignum. The struct globals can be used in order to set different settings from outside this function. See the fieldnames for further explanation.
Figure Export Properties
The Figure Export Properties GUI is opened together with a replot of the figure for finetuning the export behavior. Each time a paramter is changed, the replot is updated automatically. The GUI can be used to set the following paramters:
- Width / cm The width of either the figure or the axes can be set in centimeters. This parameter can be changed in the tex-file as well. This feature allows all figures in a document to have the same size and style.
- Figure or Axes This checkbox allows to set if the chosen width belongs to the axes or the total figure, in case of Figure, the size of the axes is chosen automatically, in case of Axes the space for all 'outer' text is added.
- Height / Width The height of the axes can be set by the height factor relatively to the axes width. This allows for using the same aspect ratios of all figures in a latex document.
- Axis equal Alternatively, the height can be set in such a way that the data of the x-axis and the y-axis are in the same aspect ratio.
- Square In Addition, the height can be set in such a way being equal to the width.
- Auto Slider This slider allows to adjust the surrounding space by increasing or decreasing the size of the figure's text. Note, that the fontsize has NO influence on the fontsize in the LaTeX document.
- Center axes horizontally Here it can be decided whether the axes should be centered horizontally or not. If set, extra space is added either left or right of the axes in order to keep the axes centered.
- Manual Space Setting The four fields Left, Bottom, Right and Top allow to set the surrounding space manually. This is important, when using subfigures in the TeX document. Hereby, it is guaranteed that all figures are aligned correctly.
- Draw figure outline If activated, a box is plotted around the latex figure in order to visualize the real figure size. If deactivated, no box is drawn. However, the box can still be activated in the tex-file by uncommenting the coresponding line.
- Stand-alone file If activated, a stand-alone tex-file is created that can be compiled with dvi -> ps -> pdf. The borders are matched with the size of the figure. Using this method, a pdf file can be generated that can be used later on for pdftex documents.
- Reduce data points The number of data points can be reduced for lines without any markers if this radiobutton is enabled. The reduction factor can be set. If the factor gets smaller, the reduction becomes less. A zero factor equals no reduction.
- Legend style The style of the legend can be set. Currently, a rectangular box, a box with rounded corners, a shadowbox and no box are supported.
- Export to file The filename can be set or changed.
Example file
The following example picture shows the functionality of the script: