Mathematica2tikz an equivalent function

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