
High quality graphics for Latex documents: The Matlab route - JohnHammersley
https://www.io.ac.nz/blog/matlab-to-latex/
======
leephillips
Gnuplot is designed to work with LaTeX. If you want a unified appearance, with
textual elements on the graph typeset by TeX, then that workflow is built-in
to gnuplot.

You can also use TikZ to call gnuplot from within the LaTeX document, or
generate TikZ code from within gnuplot.

A very brief introduction:

[https://lwn.net/Articles/628537/](https://lwn.net/Articles/628537/)

~~~
Y_Y
Gnuplot is something i categorise with Fortran and hand writing latex as "I
use use it because my advisor used it, and he used it because his advisor used
it...". I understand that they were once the best tool for the job and they
still work, but better alternatives have emerged since, e.g. matplotlib,
C++/numpy and LyX respectively. I know this is a subjective matter but i feel
it's a shame that a lot of scientist don't shop around and hence end up stuck
with the "classical" option.

~~~
chestervonwinch
Have you used Fortran? Have you tried f2py, which converts Fortran modules
into NumPy compatible Python modules? I would rather use Fortran over C++ in a
heartbeat for scientific computing. For what it's worth, I thought for years
that Fortran was an old crusty language, not worthy of my time, choosing to
use C++ instead, and I regret not picking it up sooner.

~~~
Y_Y
I have used Fortran in a few instances where I was compelled to. I never gave
it much of a chance because I didn't like using it, and what's more it isn't
nearly as popular as C/Python/Matlab etc. for modern stuff. If popularity (and
hence code sharing) wasn't important I'd be using languages I like, like
Haskell and Scheme.

All the same your comment makes me think I may have dismissed it too quickly
so I'm going to give it another try for my next toy project (even if they
index arrays from 1). Thank you.

~~~
chestervonwinch
> even if they index arrays from 1

They don't have to! You can set the array index bounds to whatever you want.
For example,

    
    
        real(8) A(0:n-1)
        real(8) w(-p:q)
    

are both valid declarations and would be indexed by i=0,n-1 and i=-p,q
respectively. If you're using gfortran compile with the "-fbounds-check" flag
to save a lot of headaches!

------
radarsat1
I've historically just used matplotlib and it's TeX-based text renderer for
labels. Output PDF, includegraphics in a latex document. Works fine, text
looks great.
[http://matplotlib.org/users/usetex.html](http://matplotlib.org/users/usetex.html)

------
jacobolus
> _But your nice professional looking documents can be ruined because you
> insert the wrong type of figures._

Example A of the wrong type of figure for any possible context: 3-dimensional
pie charts.

~~~
stargazer-3
Not necessarily. For instance, some academic journals are on their way to
allow interactive 3d plots in the pdf files [1]

[1]
[http://journals.plos.org/plosone/article?id=10.1371/journal....](http://journals.plos.org/plosone/article?id=10.1371/journal.pone.0069446)

~~~
widdma
3D plots can useful when the data is _intrinsically_ 3D. But forcing a 3D plot
on 2D data, like a pie chart, makes it hard for a viewer to develop an
intuition for the data.

~~~
stargazer-3
Fair point, I overlooked the "pie" part of the chart argument. My comment was
originally intended to go as a reply to oriettaxx's post.

------
lottin
R can output Tikz code. Probably the best option for statistics-related work.

[http://www.texample.net/tikz/examples/tikzdevice-
demo/](http://www.texample.net/tikz/examples/tikzdevice-demo/)

~~~
jtraffic
I second this. I read a paper yesterday and the figures produced by R clashed
visually with the typesetting style. The Tikz ones are so, so, nice.

------
djoshea
If you're not specifically looking for Computer Modern fonts in your figures
but just want vector graphic WYSIWYG exports with your pick of font (beyond
what Matlab renders natively), my Matlab figure export tool works well:

[https://github.com/djoshea/matlab-save-
figure](https://github.com/djoshea/matlab-save-figure)

------
widdma
I found matlabl2tikz to be superior to plot2latex. I did my honours thesis
with plot2LaTeX+inkscape and my PhD one with a matlab2tikz, so I've had a fair
experience with both.

I have created some high quality, complex plots out through matlabl2tikz with
no problem. I haven't had the issues the author notes and I'd suggest they
file a bug report. The m2t community is active and friendly.

The other bonus of m2t is you can edit the files in PGFplots, which is a
powerful and complete language/library. For my use case, it was important that
my plots be repeatable, so manually doing it with Inkscape would be tedious.
Having easily editable source code allowed me to write scripts to filter the
output to what I needed.

The one problem I did find is that pgfplots can be quite slow. This isn't
m2t's fault though and can be overcome with TikZ's cache system.

Disclosure: I wrote the path simplification in matlab2tikz, but nothing else.

------
amelius
Maybe it's just me, but why can't Matlab just use the Latex fonts in an EPS,
and output that EPS as a single file? That sounds like a more robust and less
convoluted solution.

~~~
JorgeGT
I think that the goal of the poster is not just the font, is to be able to
modify the size/font/color/style when compiling the document (say that you
realize you want bold labels, or sans-serif instead of serif when you have a
lot of figures already generated).

~~~
amelius
In that case, I'd recommend structuring your scripts such that you can easily
rerun them later on (e.g. using a Makefile). That has also other advantages,
such as the ability to change graph colors, axes, or even data.

~~~
JorgeGT
This is what I do, really. Regarding styling I have a small styling script
that I put after each graph. This small script takes the active figure (gcf)
whatever it is, and changes it to my liking. So if I want to change the graph
styling at a later stage I modify this small script and then re-run the main
file.

But everything in MATLAB, then I just symlink the pdf figures to the
manuscript folder, so with an F5 in MATLAB and a build in Sublime I have the
manuscript updated.

~~~
tjl
My only problem with doing everything in Matlab (which I used to do) and
exporting the PDF is that you can't really match the fonts easily to the main
document. I picked a set of fonts (math and normal) for my Ph.D. thesis and I
wanted the fonts in the figures to match those in the main body. Using
matlab2tikz did that for me.

You could probably do the same thing but do the export of the tikz file
instead of the PDF. That's basically what I do.

------
Bedon292
I have taken to using PowerPoint to create the actual charts. Edit them and
make them look pretty. Then export as EMF, convert to EPS
([https://www.cs.bu.edu/~reyzin/pictips.html](https://www.cs.bu.edu/~reyzin/pictips.html))
and use the EPS in the Tex document.

Its a very manual process though, but I think the graphics that come out of it
seem to be nicer than Matplotlib, and Matlab can do. Am I crazy, or should I
really consider something like this?

~~~
lottin
Personally I prefer to generate all plots (and ideally tables as well)
programatically. It's more reliable and efficient. Imagine that at some point
you decide to include more data in the analysis (or remove erroneous data) you
don't want to re-do all the work again.

------
taliesinb
In Mathematica, Export[expr,"PDF"], Export[expr, "SVG"], and
Export[expr,"LaTeX"] for math expressions.

~~~
dallamaneni
I was thinking exactly the same. This post just reminds me how easy it is with
Mathematica

~~~
tnecniv
Mathematica is a really awesome tool.

------
thisrod
I started working on a Matlab command to export plotted data as Metapost
paths, and to generate a script that draws the plot. The idea being that you
would edit the script to get the style you wanted.

The proof of concept worked well for line and contour plots, but my thesis got
in the way of completing this.

------
moultano
Does anyone know of an equivalent solution for R/ggplot? I feel sad labelling
my plots with R's fonts and equation formatting, but haven't found an
alternative.

~~~
andrewla
tikzDevice [1] defines a device in tikz terms, which I've used for vanilla R
plots, and should work for ggplot, but I'm not positive.

Actually, a quick search yielded [2], so it appears that ggplot2 is supported
as well. And apparently supported in one of the knitr examples [3] (if you use
knitr, which I highly recommend).

[1]
[https://cran.r-project.org/web/packages/tikzDevice/index.htm...](https://cran.r-project.org/web/packages/tikzDevice/index.html)

[2] [http://iltabiai.github.io/tips/latex/2015/09/15/latex-
tikzde...](http://iltabiai.github.io/tips/latex/2015/09/15/latex-
tikzdevice-r.html)

[3]
[https://yihui.name/knitr/demo/graphics/](https://yihui.name/knitr/demo/graphics/)

------
partycoder
I wonder if there is a similar guide for Scilab.

