Mathematica2tikz an equivalent function



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 <>
% All rights reserved.
% The latest updates can be retrieved from
% 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 [





Posted 2012-02-27T11:30:36.960

Reputation: 1 207

1If 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). – Szabolcs – 2012-02-27T11:50:00.727

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? – Szabolcs – 2012-02-27T13:07:16.760



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.


Posted 2012-02-27T11:30:36.960

Reputation: 213 047

Your coordList2TikZ function is quite useful; I modified it by spelling coordinates correctly and adding a \n to each coordinate pair. Without the extra newline copying-and-pasting from Mathematica to TeX was inserting backslash-newlines into my tex file at the continuation of each line. – Matthew Leingang – 2012-12-10T14:23:18.173

@Matthew Thanks for the comments! Feel free to edit the function in my post, and if you have a larger set of functions to work with tikz, please do post a more complete answer. Unfortunately I do not know/use tikz, so my answer was just a simple starting example of how one might go about implementing this. – Szabolcs – 2012-12-10T14:45:43.550

1I 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. – rcollyer – 2012-02-27T15:21:17.203

@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 (??). – Szabolcs – 2012-02-27T15:25:00.243

@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. – celtschk – 2012-02-27T15:38:46.637

@celtschk Despite the fact that it has been in there since v.2, I've never used FullGraphics. Thanks. – rcollyer – 2012-02-27T15:42:44.477

@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. – celtschk – 2012-02-27T15:43:20.623

@celtschk FullGraphics is pretty broken. It misplaces labels, renders grids completely differently, etc. It seems to have been an emulation of the real rendering which they failed to update recently ... Since you guys know TikZ, why don't you write an answer? Then I'll delete mine. – Szabolcs – 2012-02-27T15:46:11.403

@Szabolcs I understand, but he gave a higher level example than I was considering. For instance, Line[{{0,0}, {1, 0}, {1,1}}] would become \draw (0,0) -- (1,0) -- (1,1);. If Line were replaced by Polygon the TikZ code would then be \draw (0,0) -- (1,0) -- (1,1) -- cycle;. GraphicsComplex could be achieved by first specifying the translations for primitives, and the TikZ implementation would be a for loop. – rcollyer – 2012-02-27T15:48:13.630

@Szabolcs I'll write one probably tonight. – rcollyer – 2012-02-27T15:48:55.517

@rcollyer Normal[] fixes GraphicsComplex. Now the difficult part will be styles I guess. It's not trivial to determine the style of each primitive in Mathematica. – Szabolcs – 2012-02-27T15:49:55.567

I think it's a straightforward problem, just time consuming. – rcollyer – 2012-02-27T15:51:36.963


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)


Posted 2012-02-27T11:30:36.960

Reputation: 12 994

1Very 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. – yarchik – 2017-11-03T10:06:44.507