Matlab2tikz is a script which generates native LaTeX/TikZ figures from MATLAB.

I am looking for an equivalent method/function in Mathematica. Obviously the final script may need some manual tinkering as Mathematica has numerous different plotting styles. I am looking for the equivalent of ListLinePlot[]

A similar question was asked on TeX StackExchange.

Further information: TikZ is a native Latex graphics package that produces high quality plots and diagrams. An example along with the source code to showcase the syntax is shown below. (Other examples can be found here.)

Figure A



% This file was created by matlab2tikz v0.1.4.
% Copyright (c) 2008--2011, Nico Schlömer <[email protected]>
% All rights reserved.
% The latest updates can be retrieved from
%   http://www.mathworks.com/matlabcentral/fileexchange/22022-matlab2tikz
% where you can also make suggestions and rate matlab2tikz.

scale only axis,
xmin=0, xmax=100,
ymin=0, ymax=100,
xlabel={x label},
ylabel={y label},
legend entries={$y=x$},
legend style={at={(0.97,0.03)},anchor=south east,fill=white,draw=black,nodes=right}]
\addplot [



  • 1
    $\begingroup$ If you give some examples of what Tikz syntax looks like / what sort of output you desire, you might get more / better answers. I think many people will not reply only because they are not familiar with Tikz. Generating other representations from a Graphics object should usually be easy (but tedious). $\endgroup$
    – Szabolcs
    Feb 27, 2012 at 11:50
  • $\begingroup$ Thanks, but by example I meant source code. What do you need to convert a Graphics expression into? How do you draw a curved line or axes with tikz? $\endgroup$
    – Szabolcs
    Feb 27, 2012 at 13:07

2 Answers 2


This is not a full answer, just a starting point:

  • I would first write some functions that convert Mathematica graphics to a representation that is really close to the structure TikZ uses (similar to how Mathematica represents C using SymbolicC). I don't know TikZ, so this should be designed by someone who is quite familiar with it.

  • Then I'd write a set of functions that can convert this representation to a string that is syntactically correct TikZ code.


Extracting lines from a Plot:

This will extract the two lines from the plot:

lines = Cases[Plot[{Sin[x], Cos[x]}, {x, 0, 10}], Line[coords_] :> coords, Infinity]

Knowing exactly what sort of Graphics object Plot likes to generate, we can extract the style too:

Cases[Plot[{Sin[x], Cos[x]}, {x, 0, 10}], {style_, _Line}, Infinity]

Converting a symbolic representation to a string:

Looking at your example TikZ input, we can make something like the coordinate section using

coordList2TikZ[data_?MatrixQ] :=
 "coordinates{\n" <>
   "(" <> ToString[#1, CForm] <> "," <> ToString[#2, CForm] <> ")" & @@@ data] <>

(StringForm will be useful here as well.)

This will take a list of coordinates and output some TikZ code:

coordList2TikZ[{{1, 2}, {3, 4}}]

(* ==>

You can try coordList2TikZ /@ lines as well. I do not know what sort of scientific notation TikZ uses, so I just used the C-style 1.23e-5 one.

  • 1
    $\begingroup$ I know a little TikZ, so I think an intermediate language is a mistake. A Graphics object is essentially a list of primitives which can relatively easily be translated into a TikZ procedure. The difficult parts are the axes and frames as they not fully specified in the object itself, so AbsoluteOptions would be needed. $\endgroup$
    – rcollyer
    Feb 27, 2012 at 15:21
  • $\begingroup$ @rcollyer I suggested that because I assumed that the structure would not be completely equivalent, and it's much easier to do transformations in symbolic form (even just reorderings). So if there's an intermediate symbolic representation, it'll be easier to extend or modify. Otherwise one would always need to tinker with the string translation code. E.g. the example code didn't seem to have anything that directly corresponds to a line primitive. Instead it had a plot object (??). $\endgroup$
    – Szabolcs
    Feb 27, 2012 at 15:25
  • $\begingroup$ @rcollyer: For 2D plots, a graphical representation of axes etc. can be obtained by calling FullGraphics on the graphics object. AbsoluteOptions would however probably be needed for getting the plot range. $\endgroup$
    – celtschk
    Feb 27, 2012 at 15:38
  • $\begingroup$ @celtschk Despite the fact that it has been in there since v.2, I've never used FullGraphics. Thanks. $\endgroup$
    – rcollyer
    Feb 27, 2012 at 15:42
  • $\begingroup$ @Szabolcs: TikZ has also line primitives etc., for example, a line can be drawn with \draw (x1,y1) -- (x2,y2) -- (x3,y3) -- (x4,y4); Indeed, I didn't know that there's also a plot primitive. I guess that the \addplot command gives more compact code, though. $\endgroup$
    – celtschk
    Feb 27, 2012 at 15:43

I don't know if this can be considered as an answer to your question, but: Do not do that! That would mean learning a new tool (not even talking about programming it), facing singular situations which cannot be dealt with, and all that for a value close to zero.

I recommend the following workflow:

  1. Run your Mathematica code
  2. Export the results you want to plot as csv, dat, txt or similar.
  3. In $\LaTeX$, use tikz and pgfplots to import your data and plot.

This way, you uncouple the "science" (the results) and its visualization. Developers of pgfplots have already spent a lot of time to think about a simple way of dealing with a vast variety of cases. No need to add another redundant layer. Also, it is cleaner: you end up with 1) Mathematica code, 2) data, 3) $\LaTeX$ code, instead of a Mathematica-generated $\LaTeX$ code. Some of my colleagues use matlab2tikz, editing the tex file is tedious and in the end, I think it does not bring a single advantage compared to the above-described workflow.

(It is a bit like using Lyx: the purpose of LaTeX is precisely not to be limited by WYSIWYG, with Lyx you combine the drawbacks of Openoffice (or similar) with the drawbacks of LaTeX, in a way)

  • 2
    $\begingroup$ Very reasonable and pragmatic answer! For 95% of cases MA already provides acceptable graphics. For fine tuning, I would use professional WYSIWYG tools such as adobe illustrator. (Gnuplot) +Latex+TIKZ does provide excellent quality, but at considerable amount of work. 3D graphics cannot even be efficiently handled by these tools. $\endgroup$
    – yarchik
    Nov 3, 2017 at 10:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.