PDF letter size
PDF legal size

Example using Mathematica to generate similar plot as Tikz using Lua

Nasser M. Abbasi

May 20, 2020   Compiled on May 20, 2020 at 9:05pm

This note show how to generate Latex document in Mathematica that gives same output as code I saw using Lualatex in paper Numerical metods with Lualatex [1]. The idea of the above paper is to use lua code to assist in making the computation and then use Tikz for the plotting part. Lua makes it easier to do the computation to generate the data to be plotted, then the data is passed back to tikz for plotting.

There are a number of examples given in the above paper. Below I show how to generate complete Latex document from inside Mathematica which gives the same plot for one example. Later I will add the other examples.

The advtange of using Mathematica to generate the complete Latex document, is that the computation is done in the same notebook, and Mathematica is much more powerfull at computation. I have used this method before to generate large Latex reports, all from inside Mathematica.

Ofcourse, one have to generate the Latex code using Mathematica strings. But this is not a problem I found. One has to just escape each Latex ”.

Here is the complete Latex code, taken from the above paper for one example, where I just added the preamble to make it compile as a standalone Latex file. I used TexLive 2016.

Compiling the above using lualatex tikz.tex, here is the pdf generated

Figure 1:output of tikz.tex, example code from the paper

Next I opened a Mathematica notebook, and wrote the code which generated a full latex file, self contained, which does the same computation and generate the plot. Here is the Mathemmatica code. After Making the plot, the image is saved to pdf file, and then used in the next statement.

running the above Mathematica code, it generated the following Latex file called mma.tex

Now compiling the above Latex file, also using lualatex mma.tex gave this pdf file

Figure 2:output of mma.tex, Mathematica generated Latex

The main objective to this method might be is that the graphics and the annotations generated by Mathematica is not using native Latex notations and so it might not fit well in the document as those generated by Tikz and Lua code. But this also has a solution, thanks to a new package called Matex.

I did not Matex in this example as it was not needed, but could have easily done so if annotations are needed, and will do that when I add more examples later on for illustrations. Matex allows one to add Latex annotations to a plot and graphics.


This is the paper that I took the lua code above to write equivalent one in Mathematica. A well written and useful paper with many good Lua code examples.