File Exchange

## fig2texPS - Export Matlab Figure to LaTeX

version 1.6 (299 KB) by

V053 - Conversion of a Matlab figure to a LaTeX file with PSTricks compatible macros

4.58333
14 Ratings

Updated

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.

http://fig2texps.peter-scholz.net

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

Kiran Sagar Kollepara

### Kiran Sagar Kollepara (view profile)

Any equivalent versions of the same?

Marcin Kawka

### Marcin Kawka (view profile)

Looks like it's no longer compatible with MATLAB versions above 2014. See this article for explanation: https://www.mathworks.com/help/matlab/graphics_transition/graphics-handles-are-now-objects-not-doubles.html

Konstantinos Margaris

AP

### AP (view profile)

Excellent job

Christopher Jones

### Christopher Jones (view profile)

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: http://pastebin.com/xK6KxxBT

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: http://pastebin.com/cHGqD9a5

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

### Peter Scholz (view profile)

@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.

Antoine Haarscher

### Antoine Haarscher (view profile)

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

OB

### OB (view profile)

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.

OB

### OB (view profile)

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);
plot(x,y,'bo-');
legend('\exp(x)');
fig2texPS;

OB

Jeff Tuhtan

### Jeff Tuhtan (view profile)

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

Peter Scholz

### Peter Scholz (view profile)

@ Bertrand Scherrer: Thank you for your help. I have integrated your code to the latest version 0.5.2 that can be found here:
http://fig2texps.peter-scholz.net
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 :-)

Bertrand Scherrer

### Bertrand Scherrer (view profile)

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: http://www.music.mcgill.ca/~scherrer/fig2texPS_wTextBoxes

Pia

### Pia (view profile)

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

### Peter Scholz (view profile)

@ 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

Pia

### Pia (view profile)

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

errorbar(1:10,sin(1:10),0.1*sin(1:10))

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.. :(

Petter

### Petter (view profile)

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.

Alan Kennedy

### Alan Kennedy (view profile)

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.

Carlos

Peter Scholz

### Peter Scholz (view profile)

@ Petter: You have to set
set(0,'DefaultFigureWindowStyle','normal')
I have updated the code so that there will be given a warning in your case.

Petter

### Petter (view profile)

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.

Petter

### Petter (view profile)

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
set(0,'DefaultFigureWindowStyle','docked');

 3 Nov 2010 1.6 Changes: * 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 5 May 2010 1.5 New features implemented 5 May 2010 1.4 Enhanced Features implemented 11 Dec 2009 1.3 Link updated: http://fig2texps.peter-scholz.net 10 Dec 2009 1.2 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 15 Sep 2009 1.1 Warning, if the 'DefaultFigureWindowStyle' is 'docked'
##### MATLAB Release
MATLAB 7.10 (R2010a)