File Exchange

image thumbnail

fig2texPS - Export Matlab Figure to LaTeX

version (299 KB) by Peter Scholz
V053 - Conversion of a Matlab figure to a LaTeX file with PSTricks compatible macros


Updated 03 Nov 2010

View License

The aim of this function is to create a tex-code picture of a Matlab figure in PSTricks syntax. The tex-file picture can still be adapted after creation and may be either a standalone tex-file or a file to be included in a larger document. The LaTeX packages pstricks, pst-node, pst-plot, pst-circ and moredefs are required. An minimal example latex code is generated at each time this script is run. The matlab code is based on the fig2tex file by Ercan Solak in 2005.

You can also download the latest version on

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) are supported
* GUI, where the fontsize, the plotsize and the export file 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
* Standalone Option for creating high quality cropped pdf-files

Cite As

Peter Scholz (2020). fig2texPS - Export Matlab Figure to LaTeX (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (25)

Any equivalent versions of the same?

Looks like it's no longer compatible with MATLAB versions above 2014. See this article for explanation:


Excellent job

awesome, been using fit2tex for a while, this just adds a lot of extra bling bling :)

thank you very much!


I fixed it, I think. And also add some other things. If someone is interested look in here:


Hi Peter, I made some changes to your script regarding the use of pdflatex, now the files created use ifpdf package to compile for pdflatex and latex. I didn't know where to put it, so you can find it here:

Also, I found a bug when you have points outside of the axis size, in which those points are removed from the plot.

I'm not sure if my approach is correct or not, but did the job for my test scenarios. Also, it may help others.

Peter Scholz

@adin: I can't provide any support and/or changes on the script no longer. I have never tested the \usepackage[pdf]{pstricks} version of pstricks but it could work.
Regarding the one/two column document: I recommend to export two versions of the figure, one for each version of the document. The best way is to write the default configuration to the "globals" struct and run the script with "fig2texPS(fignum,globals)".


Hi, I love your script. It work really good. However, I'm struggling with a couple of things.
First, it would be great if you add an option for pdflatex. You can compile it using \usepackage[pdf]{pstricks} instead of the normal pstricks header that you already have. However, this only works for the stand alone version. Which creates my second issue.

The thing is that if you have a two column document, the size of the document is fixed, is there a way to make it change with the \linewidth. So it will be relative to one or two column documents.

Thanks for your work. :)

Hi, I cannot run this program for a pcolor figure. Is it my mistake or it is simply impossible?
Thank you


The marker legend problem is solved. It was not the script. (fig2texPS is a great script!!) It was XeLaTeX !! Compiled with LaTeX and converted with dvips and epstopdf, I obtained a perfect PDF graphic file. Wondeful.


This is a great script!! I love this.

By the way, when markers present, the legend does not display markers. Could you fix this?

Maybe you test with this:

x = linspace(0,2,10);
y = exp(x);


Jeff Tuhtan

Worked well, thank you for sharing this script. The stand alone option is a great feature!

Peter Scholz

@ Bertrand Scherrer: Thank you for your help. I have integrated your code to the latest version 0.5.2 that can be found here:
The borders of the text boxes are now also shown. But there is still some finetuning to implement, e.g. an automatic linebreak for longer text. I will do that when I'll find some time for that :-)

Dear Peter,

Thank you for making this script available. It is very useful.

I have slightly modified your script to include text box annotations in the exported figures. I posted the file and a little test file at:


Ah, it was the version number that was the problem - of course I downloaded the one from this page instead of going to your page to get the most recent one - doh!

Thank you very much for the help - you're a life saver with this script! :)

Peter Scholz

@ Pia: I have tested your given errorbar with the newest version V.0.5.1 and it works fine without errors. Please test again with the same version and send me your tex-file if it still not works. Peter


I have trouble exporting errorbar plots. Do the package support these? A simple plot like


exports fine (apparently, not errors at least), but after compiling, my DVI viewer simply stalls with weird errors. If I try to convert the dvi document to a pdf, my GhostScript simply tells me there was an unrecoverable error, and that it stopped.. :(


As I've said before: it would be really nice if there was an option to export to a stand alone tex file with correct paper sizes that then could be converted to a PDF via ps2pdf. This way, users of pdflatex can also use this package.

Great submission! very much what I was looking for - am having problems though adding text to the figure itself (using Matlab's text() command) - and ignore my last rating, that was an accident! 5 stars.


Peter Scholz

@ Petter: You have to set
I have updated the code so that there will be given a warning in your case.


Couldn't you create a standalone .tex file (with paper sizes etc.)which could be compiled using latex/ps2pdf into a separate pdf file containg the figure.

Then we pdfLatex users could include this file into our documents.


I cannot run this file

??? Error using ==> figure
Error using ==> figure
Cannot set DockControls to 'off' while WindowStyle is 'docked'

Error in ==> fig2texPS at 891
fig.figh=figure('Name','Figure that will be converted to TeX',...

Might be a conflict with


* dataplot -> psline replaced, option linejoin=1 introduced
* marker more bold
* enhancement for patches which are cut at the axes limits
* Colorbar enhancement
* Support of TextBoxes
* Small bugfixes

New features implemented

Enhanced Features implemented

Link updated:

Changes to Version 0.42:
* More possible locations for the legends
* Linewidths of the global struct can now also be strings
* Linestyles adapted
* Enhancements for TextArrows
* Questiondialog, if the tex file already exists
* Small bugfixes

Warning, if the 'DefaultFigureWindowStyle' is 'docked'

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

Inspired by: fig2tex, Matfig2PGF