How to Include SVG Diagrams in LaTeX
At a glance
- 1. Install Inkscape
Install Inkscape so LaTeX can convert SVG files during compilation.
- 2. Load the package
Add the svg package in your document preamble.
- 3. Insert the diagram
Use includesvg with the SVG filename, width, or height options.
- 4. Enable conversion
Compile with shell escape so LaTeX can run Inkscape.
SVG (Scalable Vector Graphics) diagrams can be included in LaTeX without losing resolution. The most convenient method is the svg package, which calls Inkscape during compilation and performs the conversion for you.
You need two things: the LaTeX svg package and an installed Inkscape command-line executable. The automatic method works with the normal PDF-producing workflows, such as pdflatex, xelatex, and lualatex.
The recommended method: the svg package
Put the SVG file, for example diagram.svg, in the same directory as your .tex file. Then use this complete document:
\documentclass{article}
\usepackage{graphicx}
\usepackage{svg}
\begin{document}
Here is a vector diagram:
\begin{figure}[ht]
\centering
\includesvg[width=0.75\linewidth]{diagram}
\caption{An SVG diagram included in LaTeX.}
\label{fig:diagram}
\end{figure}
\end{document}
The argument to \includesvg is normally the filename without the .svg extension. You can also write \includesvg{diagram.svg}, but omitting the extension lets the package handle the file name more flexibly.
The width option scales the diagram to 75 percent of the text width. Other useful options include:
\includesvg[width=6cm]{diagram}
\includesvg[height=4cm]{diagram}
\includesvg[width=\textwidth]{diagram}
Do not specify both width and height unless you deliberately want to change the diagram’s aspect ratio.
Compile with shell escape
The svg package needs permission to execute Inkscape. Compile the document with shell escape enabled:
pdflatex --shell-escape main.tex
For other engines, use the corresponding command:
xelatex --shell-escape main.tex
lualatex --shell-escape main.tex
In an online editor, enable the option usually called shell escape. Use the editor’s documented setting for allowing external commands. Some hosted LaTeX services already provide Inkscape; others restrict external programs, in which case manual conversion is more reliable.
Shell escape allows a LaTeX document to run external commands, so do not enable unrestricted shell escape for untrusted .tex files. The restricted form, where supported, is safer, but the exact permitted-command list depends on the TeX distribution and editor.
Controlling text in the SVG
There are two common ways to handle text inside an SVG.
By default, the svg package asks Inkscape to create a PDF without a LaTeX text overlay. If you set inkscapelatex=true, it asks Inkscape to create a PDF plus a LaTeX text overlay. This can make labels in the diagram use LaTeX fonts and mathematical notation. It also means the generated auxiliary files must remain available during compilation.
If you want Inkscape to put all SVG text directly into the converted PDF instead, disable the LaTeX overlay:
\includesvg[
width=0.8\linewidth,
inkscapelatex=false
]{diagram}
This is often the simplest choice for diagrams whose labels already have the desired appearance. It can also avoid problems with text positioning or missing overlay files.
For example, an SVG containing the label $x^2$ is not automatically interpreted as LaTeX mathematics merely because it is included in a LaTeX document. If the label must be typeset by LaTeX, create the overlay through Inkscape’s LaTeX export and check that the SVG’s text is suitable for that workflow.
Manual conversion to PDF
If automatic conversion is unavailable, convert the SVG before compiling the LaTeX document. Inkscape can export it from a terminal:
inkscape diagram.svg --export-type=pdf
This creates diagram.pdf. Include that file with the standard graphicx package:
\documentclass{article}
\usepackage{graphicx}
\begin{document}
\begin{figure}[ht]
\centering
\includegraphics[width=0.75\linewidth]{diagram.pdf}
\caption{A manually converted diagram.}
\end{figure}
\end{document}
You can also specify the output name explicitly:
inkscape diagram.svg --export-filename=diagram.pdf
Manual conversion is useful in automated builds, restricted online environments, and projects where generated files are checked into version control. PDF preserves vector geometry, so vector portions of the diagram remain sharp when enlarged.
A concrete example with a directory
Suppose your project has this structure:
project/
├── main.tex
└── figures/
└── pipeline.svg
Use the relative path in the document:
\documentclass{article}
\usepackage{svg}
\begin{document}
\includesvg[
width=\linewidth,
inkscapelatex=false
]{figures/pipeline}
\end{document}
Compile from the project directory with:
pdflatex --shell-escape main.tex
The package creates intermediate conversion files near the SVG or in the configured output directory. Keep those files available if the document needs to be compiled again, or remove them after a successful build if your build system regenerates them automatically.
Common mistakes
Inkscape is not installed or not on the path
An error such as “Inkscape could not be found” means LaTeX cannot locate the executable. Install Inkscape and test this command in a terminal:
inkscape --version
If it works in the terminal but not from LaTeX, configure the Inkscape executable path in the svg package according to its documentation or your editor’s environment settings.
Shell escape is disabled
If \includesvg produces an error about conversion or an expected generated file is missing, check that the compiler command includes --shell-escape.
The file path is wrong
File names and directory names are case-sensitive on case-sensitive filesystems, including Linux and case-sensitive macOS volumes. Figures/Diagram.svg and figures/diagram.svg are different paths in that situation.
The SVG contains unsupported features
Inkscape may render filters, linked images, fonts, or animations differently from a web browser. Open the SVG in Inkscape first and export it manually to verify the result. For a static LaTeX document, PDF is usually the most dependable final format.
Using the wrong compilation route
The svg package is intended for PDF-oriented LaTeX workflows. If you must produce DVI output, convert the SVG to a format supported by that workflow first, rather than expecting \includesvg to work unchanged.
For most projects, use \includesvg during development and either retain the automatic conversion or commit the generated PDF for a reproducible build. If the build environment cannot run external programs, convert the SVG to PDF once and use \includegraphics.
References
Spotted an error? Send a correction. Read how we write and check articles in our editorial policy.