## Mathematica2tikz an equivalent function

57

26

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}


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

13

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.

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

8

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.