Skip to content

Including SVGs

Wolfgang Hochleitner edited this page Jan 12, 2023 · 6 revisions

What?

Scalable Vector Graphics (SVG) is a standard vector graphics format. While it can be converted to PDF using a vector graphics program, including SVG files directly is also a viable option.

How?

First and foremost, the svg package is required. This is all that is needed when using Overleaf. For a local setup, the free vector graphics editor Inkscape must also be installed. Its command-line interface provides the tools to split an SVG file into a text component that LaTeX can render.

Setup

Include the package in your main document's preamble:

\usepackage{svg}

When using a local LaTeX setup, also install Inkscape. It is available for all common operating systems. Use the latest stable version.

Finally, for pdflatex to call Inkscape commands, the --shell-escape flag must be set in your build command.

In TeXstudio, go to "Options - Configure TeXstudio" (Windows) or "TeXstudio - Preferences" (Mac OS), then select "Commands" and edit the command for PdfLaTeX to the following:

Windows:

pdflatex.exe -synctex=1 -interaction=nonstopmode --shell-escape %.tex

Mac OS:

pdflatex -synctex=1 -interaction=nonstopmode --shell-escape %.tex

⚠️ Warning: Enabling shell access allows for the execution of arbitrary code and is, therefore, a potential security risk. Activate the flag only when you need it. ⚠️

Including SVG Images

Place an SVG image in the images folder and use the \includesvg[..]{..} command to include the file. Use a figure environment to make the image float. The following snippet includes the SVG file Variable_Resistor.svg and scales it to 50% of the text width. The image contains no text.

\begin{figure}
    \centering
    \includesvg[width=0.5\textwidth]{Variable_Resistor.svg}
    \caption{This SVG image of a variable resistor contains no text. No special options are needed.}
    \label{fig:resistor}
\end{figure}

When the SVG image includes text, svg can either have LaTeX render the text or display it "as is" in the file. This behavior is controlled by the parameter inkscapelatex, which is true by default. Therefore it must not be added if the text should be replaced. The image SVG-Unschaerfe-Filter.svg contains text that LaTeX automatically replaces.

\begin{figure}
    \centering
    \includesvg[width=0.5\textwidth]{SVG-Unschaerfe-Filter.svg}
    \caption{Text in SVG files is rendered by LaTeX as a default setting. The SVG file is split into a PDF and \latex\ file so the text can be replaced.}
    \label{fig:filter-latex-text}
\end{figure}

To deactivate text replacement, add inkscapelatex=false to the options of the \includesvg command:

\begin{figure}
    \centering
    \includesvg[inkscapelatex=false,width=0.5\textwidth]{SVG-Unschaerfe-Filter.svg}
    \caption{By setting \texttt{inkscapelatex=false}, the text is not split into separate files and, therefore, is not rendered by \latex.}
    \label{fig:filter-no-latex-text}
\end{figure}

See the provided Overleaf example below to see these commands in action.

Where?

SVG images will occur in your regular chapter documents. Place your SVG files in your images folder.

Package Information

All commands above are part of the svg package. Refer to the package's manual for additional configuration options.

Supplemental Materials

Try out this feature in a sample document on Overleaf: https://www.overleaf.com/read/kmcpycwsfddd