Export figure for LaTeX documents

High-precision tool to export a figure to pdf matching the font size and the text width of a LaTeX document.
Updated 29 Jun 2022

View License

The script "main.m" shows some examples of how to export a figure to pdf to precisely match the font size adopted in the LaTeX document "latex.m" (change file name to "latex.tex" when compiling).
It can be useful to export high-quality figures for scientific papers to match the style of the specific journal.
Each figure requires two parameters:
  • TextWidthRatio, defining the quantity (width of the figure)/(total width of the text in the LaTeX document). To exploit the entire width of the page, use TextWidthRatio=1.00 for a single figure, TextWidthRatio=0.49 for two subfigures placed next to each other, etc.
  • FigRatio, defining the quantity (height of the figure)/(width of the figure). In case the axes are "constrained" (for example by setting "axis equal"), this represents only an initial guess.
The function cropFigure() adopts an iterative procedure to gradually move the position of the axes in order to eliminate the white margins.
At each step, a lower-quality temporary png file is written and read making sure that no colored pixel (supposedly containing useful information) exits the margins of the figure.
Once the optimal position of the axes has been found, the resulting figure is exported to a high-quality pdf file, ready to be included in the LaTeX document via the \includegraphics command.
Simply run "main.m" in Matlab and compile "latex.tex" with your preferred LaTex compiler (for example TeXmaker) to see a few examples.
If you find this tool useful, please give feedback.

Cite As

Andrea La Spina (2024). Export figure for LaTeX documents (https://www.mathworks.com/matlabcentral/fileexchange/114350-export-figure-for-latex-documents), MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2019b
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
Version Published Release Notes