Writing a thesis in LaTeX - part 4
In the previous part of this blog series, an introduction to the bib2gls
and glossaries-extra
packages for automatically generating glossaries was given. In this last part of the blog series, a neat workflow for creating and integrating vector graphics (e.g., svg graphics) in the thesis document will be presented. This will be done in two steps:
- Creating vector graphics in
matplotlib
. - Integrating the vector graphics in LaTeX with the help of Inkscape
A zipped version of the project can be downloaded here. Compiling the project gives you the following pdf:
Prerequisites
-
python
,matplotlib
andnumpy
must be installed. By installing anaconda, all that is done at once. - Inkscape must be installed and added to the system’s PATH variables (make sure to chose this option during installation).
- The following user defined tool must be added in Texstudio:
txs:///pdflatex/[--shell-escape]
(Preferences > Build > add the command in the lower part of the window and give it a name, such as “svg2tex”)
Creating scientific plots with matplotlib
First, we will add a new folder to the LaTeX project, which contains the Jupyter notebook file that creates the vector graphics. Furthermore, we add a mplstyle file, which contains necessary matplotlib
style settings, in the source folder. The final project structure looks as follows:
First, the vector graphic, that will be integrated in the LaTeX document, has to be generated. This is done in the notebook plot.ipynb
, which loads the necessary python packages numpy
and matplotlib
:
Futhermore, the pre-defined matplotlib style svg.mplstyle
is loaded and used for all plots:
The file svg.mplstyle
contains only a few matplotlib
settings, that we will need for integrating the vector graphic in the LaTeX document:
That’s it. Now, we can create vector graphics and save them in the figures folder:
This creates and saves a vector graphic called plot.svg
in the figures folder.
In the next step, this svg figure will be further processed by Inkscape to a pdf_tex file and a pdf file, which finally can be integrated in the LaTeX document. How to do this automatically is described in the next section.
Integrating vector graphics in LaTeX
First, the svg package is loaded in the preamble of the main.tex
file and a few options are set:
One very handy feature of integrating vector graphics this way is, that the font can be globally defined for all vector graphics in the entire document. This can be done with the floatrow
package. Say we want the fontsize of all vector graphics in the document to be smaller (e.g., footnotesize
), than this can be set globally in the preamble:
What’s still missing is the actual integration of the vector graphic in the introduction.tex
file. This can be done as follows:
That is it! Feel free to try to integrate a second figure and change the fontsize to large
and see what happens.
Note, that in order to successfully compile the entire document including the svg file and the nomenclature in TeXstudio, you have to compile in the following order: “svg2tex” user tool > “bib2gls” user tool > F5.
Summary
In this last part of the bolg series, an automated workflow for integrating vector graphics into a LaTeX document was presented. The main takeaways should be:
-
matplotlib
is a free and efficient way of creating svg files that can easily be integrated into a LaTeX document - With the help of the
svg
andfloatrow
packages, the fonts of vector graphics can be globally set. This leads to a uniform appearance of all plots and can be handy for late changes.
With this last part of the blog series, I hope I could convince you that writing a thesis in LaTeX is worth the effort and I hope that I could help you to write a beautiful thesis in LaTeX with less pain but more fun!