matlab2tikz#

matlab2tikz is an add-on for Matlab which produces .tex files for inclusion in LaTeX documents. When creating figures and saving them as .jpg or .png files, it is burdensome to determine the fontsize for axes, labels, etc within the figure to ensure they are readable in the final document. Additionally, I have never managed to get all font sizes the same size as figures with different widths will lead to slight changes in the font size of the final image.

A solution to this problem is matlab2tikz. It is a function in Matlab which draws out the figures as a tikzpicture object, allowing LaTeX to take care of the text processing. As it was a bit of a faff to get set up, the process it took me to get it working is documented here.

(Note - there is a python version for matplotlib. Originally I wanted to move here, but if you take care then the pgf export options in matplotlib works pretty well at producing compiled .pdf files of your figures. This is how I make figures now!)

Installation#

Install matlab2tikz from the Matlab FileExchange.

Plot your figures in Matlab. Note that I experienced a few issues when using certain features - the workarounds I came up with are listed on the next page.

Call matlab2tikz either in the script or in the command window. Save the file with some filename as <filename>.tex - this take the currently open figure and save a .tex file which can be included in your Latex document.

Move your .tex file and any accompanying files (e.g. sometimes a .png file may be generated as well) to your Latex project directory (or upload them to Overleaf, etc.).

Within the preamble of the LaTeX document, add in the following lines:

\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usetikzlibrary{plotmarks}
\usetikzlibrary{arrows.meta}
\usepgfplotslibrary{patchplots}
\usepackage{grffile}

In the body at the desired location, use the command \input{<filename>.tex} to include the figure.