Make publishable plots in Mathematica

MonsteraTech
3 min readSep 9, 2023

--

Wolfram Mathematica can be an amazing visualizing tool, but the default plot style doesn’t fit well in scientific publications.

In this post, we will see how to make mathematica plots beautiful and suited for a scientific paper.

MaTeX

MaTeX allows you to create LaTeX labes in Mathematica.

First, you need to install the MaTeX package

Before starting, you will need

  • A TeX system that includes pdflatex with the standalone, exscale and lmodern packages. Both TeX Live and MiKTeX should work.
  • Ghostscript 9.15 or later.
  • On OS X, MacTeX 2015 includes a compatible version of Ghostscript. If you use an older TeX distribution on OS X, get a recent Ghostscript from Richard Koch’s page. I installed Ghostscript using Homebrew, thus in my case the code to run was
ConfigureMaTeX[  "pdfLaTeX" -> "/Library/TeX/texbin/pdflatex",   "Ghostscript" -> "/opt/homebrew/bin/gs"  ]
ConfigureMaTeX[  "Ghostscript" -> "C:\\Program Files\\gs\\gs10.00.0\\bin\\gswin64c.exe"]
  • You’ll need to point to the CLI executable (gswin64c.exe). Make sure to have in the path the correct version number you installed in gs10.00.0

Now let’s open a Mathematica Notebook. If your installed Mathematica version is 11.3 or later, you can install or upgrade MaTeX by simply evaluating

ResourceFunction["MaTeXInstall"][]

For older versions, see here

Once installed, load it in your notebook as

<<MaTeX`

When loading MaTeX for the first time, it will try to automatically detect TeX and Ghostscript. If this fails, it will display instructions on configuring the location of the pdflatex and gs executables using the ConfigureMaTeX command. As an example, on my OS X system I needed to use the following configuration:

ConfigureMaTeX["pdfLaTeX" -> "/Library/TeX/texbin/pdflatex", "Ghostscript" -> "/usr/local/bin/gs"]

See if the installation worked by evaluating

MaTeX["x^2"]

Next, you can take the basic Mathematica Plot

and transform it into something nicer:

To do so, we’ll use the following code.

Plot[Sin[x], {x, 0, 2 \\[Pi]},
ImageSize -> Large,
PlotStyle -> {Black, Thickness@0.005},
PlotRange -> {{0, 2 \\[Pi]}, {-1.5, 1.5}},
Frame -> True, GridLines -> Automatic,
LabelStyle -> {FontFamily -> "CMU Serif", FontSize -> 20},
FrameLabel -> {
MaTeX["\\alpha", Magnification -> 2],
MaTeX ["\\sin \\alpha", Magnification -> 2]},
FrameTicks -> {
{Table[{j, MaTeX[j, Magnification -> 1.8]}, {j,
Range[-1.5, 1.5, 0.5]}] (* y axis *),
None},
{Table[{j,
MaTeX[ToString[j/\\[Pi]*180] <> "^\\circ",Magnification -> 1.8]}, {j,
Range[0, 2 \\[Pi], \\[Pi]/2]}] (* x axis *),
None}
}
]

This code is using the Wolfram Mathematica function Plot to create a graph of the sine function, where the horizontal axis represents the variable x, and the vertical axis represents the value of the sine function at that x value. The x range goes from 0 to 2π and the y range goes from -1.5 to 1.5.

The option ImageSize -> Large makes the graph larger. PlotStyle -> {Black, Thickness@0.005} sets the color of the graph to black and the thickness of the line to 0.005.

The option Frame -> True adds a frame to the graph and GridLines -> Automatic adds gridlines to the graph.

LabelStyle -> {FontFamily -> "CMU Serif", FontSize -> 20} sets the font family and size for the labels on the graph.

FrameLabel -> {MaTeX["\\alpha", Magnification -> 2],MaTeX ["\\sin \\alpha", Magnification -> 2]} sets the labels for the horizontal and vertical axis, respectively, using the MaTeX function to display the labels in LaTeX format with a magnification of 2.

FrameTicks -> is used to customize the tick marks on the graph. The option sets the tick marks on the y-axis to be at -1.5, -1, -0.5, 0, 0.5, 1, 1.5 and on x-axis to be at 0, 90, 180, 270, 360 (in degree) respectively. It is using the MaTeX function to display the tick labels in LaTeX format with a magnification of 1.8.

And this is how one can make plots in Mathematica formatted with LaTeX labels so that they fit nicer in pubblications.

Hope this helps!

--

--

MonsteraTech

Mathematical Physicist working in Celestial Mechanics and Astrodynamics