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

\documentclass[12pt]{article}
\usepackage{tikz}
\usepackage{pgfplots}

\begin{document}

\begin{figure}
\centering
% This file was created by matlab2tikz v0.1.4.
% Copyright (c) 2008--2011, Nico Schlömer <nico.schloemer@gmail.com>
%
%   http://www.mathworks.com/matlabcentral/fileexchange/22022-matlab2tikz
% where you can also make suggestions and rate matlab2tikz.
%
\begin{tikzpicture}

\begin{axis}[%
scale only axis,
width=3.2877in,
height=2.37695in,
xmin=0, xmax=100,
ymin=0, ymax=100,
xlabel={x label},
ylabel={y label},
xmajorgrids,
ymajorgrids,
legend entries={$y=x$},
legend style={at={(0.97,0.03)},anchor=south east,fill=white,draw=black,nodes=right}]
color=blue,
solid
]
coordinates{
(1,1)(2,2)(3,3)(4,4)(5,5)(6,6)(7,7)(8,8)(9,9)(10,10)(11,11)(12,12)(13,13)(14,14)(15,15)(16,16)(17,17)(18,18)(19,19)(20,20)(21,21)(22,22)(23,23)(24,24)(25,25)(26,26)(27,27)(28,28)(29,29)(30,30)(31,31)(32,32)(33,33)(34,34)(35,35)(36,36)(37,37)(38,38)(39,39)(40,40)(41,41)(42,42)(43,43)(44,44)(45,45)(46,46)(47,47)(48,48)(49,49)(50,50)(51,51)(52,52)(53,53)(54,54)(55,55)(56,56)(57,57)(58,58)(59,59)(60,60)(61,61)(62,62)(63,63)(64,64)(65,65)(66,66)(67,67)(68,68)(69,69)(70,70)(71,71)(72,72)(73,73)(74,74)(75,75)(76,76)(77,77)(78,78)(79,79)(80,80)(81,81)(82,82)(83,83)(84,84)(85,85)(86,86)(87,87)(88,88)(89,89)(90,90)(91,91)(92,92)(93,93)(94,94)(95,95)(96,96)(97,97)(98,98)(99,99)(100,100)
};

\end{axis}
\end{tikzpicture}

\end{figure}

\end{document}

• 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

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.

Examples:

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" <>
StringJoin[
"(" <> ToString[#1, CForm] <> "," <> ToString[#2, CForm] <> ")" & @@@ data] <>
"\n};"


(StringForm will be useful here as well.)

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

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

(* ==>
coodinates{
(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.

• 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

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:

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.