How to Marry SVG and LaTeX

I am currently writing up my PhD dissertation. And I use a lot of figures and graphs to describe my approach, various concepts, relationships between concepts and architectures. The problem with simply using bitmap images like PNG or JPG is that they mostly look blurred and fuzzy as they do not scale. So vector graphics to the rescue.

The thing is that LaTeX does not support SVG files natively. And I do have a lot of SVG files which are generated from using PlantUML via a plugin in Intellij, or Graphviz. All of them need to be converted to PDFs, which can contain vector graphics, either before or during the compilation of the LaTeX document.

Despite the fact that it is possible to convert the SVG files to PDF files during compilation through the LaTeX package svg, I wanted to convert them before the compilation to keep my LaTeX project simple. I already compile graphviz DOT and NEATO files to SVG. Hence, this fits my workflow.

To actually convert SVG to PDF files, I used Apache Batik, more specifically the Apache Batik rasterizer. This is a Java-based command line tool which can, among other things, can convert SVG files to PDF files. But this only works using Batik 1.7.1, as Batik 1.8 fails with a ClassNotFoundError. So ensure that you use the correct version.

Apache Batik rasterizer has be to started using Java 6 or above. I used the latest Java 8 update 101, and put it on the PATH. To convert a single SVG file to a PDF file, one has to execute the following command with file.svg being the file to be converted:

java -jar batik-1.7.1/batik-rasterizer.jar -m application/pdf file.svg

I use a modified version of the previous command to convert all SVG files within a folder to their PDF version:

java -jar batik-1.7.1/batik-rasterizer.jar -m application/pdf -d folder folder/*.svg

In LaTeX itself, I was not required to change anything as the includegraphics command will automatically use the PDF version if no PNG file exists. For this, the files should not end with a a file extension as shown below.

\begin{figure}[htb]
\centering
\includegraphics[width=0.8\linewidth]{figures/file}
\caption{My Figure}
\label{fig:myfigure}
\end{figure}

Now, all my figures are crystal clear in the PhD dissertation. And they can be in your LaTeX document as well!

 •  0 comments  •  flag
Share on Twitter
Published on December 05, 2016 06:06
No comments have been added yet.