Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Matlab2tikz is script which generates native Latex/Tikz figures from MATLAB.

I am looking for an equivalent method/function in Mathematica to generate similar results.

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

A similar question is there.

Further information: Tikz is a native Latex graphics package. For many users of Latex it provides very high quality diagrams. Users therefore need to export data from Mathematica to plot within Tikz. An example is shown in the Figure.

A minimal working example of Tikz usage is provided for a simple case, although the ultimate graphics capabilities of Tikz is vast.

Other examples are 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 [



share|improve this question
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). – Szabolcs Feb 27 '12 at 11:50
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 Feb 27 '12 at 13:07
up vote 10 down vote accepted

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.

share|improve this answer
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. – rcollyer Feb 27 '12 at 15:21
@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 Feb 27 '12 at 15:25
@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 Feb 27 '12 at 15:38
@celtschk Despite the fact that it has been in there since v.2, I've never used FullGraphics. Thanks. – rcollyer Feb 27 '12 at 15:42
@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 Feb 27 '12 at 15:43

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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