# svg package \includesvg with underscores in sgv

I have an SVG file that includes text with underscores. When I include the svg with the \includesvg command (from svg package), I get an error that results from the .pdf_tex file containing text with the un-escaped special character in it.

The diagram shows variable names etc so the underscores need to be there.

I'm trying to implement the advice from the answer to the question: Define an "escape underscore" environment, and pass in the escape to the pretex argument defined int he svg package documentation.

First, I have the svg package included in my header:

\usepackage{svg}


Then, declare the custom macro and pass it into the \includesvg command thusly:

% The custom \escapeus macro from https://tex.stackexchange.com/questions/20890/define-an-escape-underscore-environment
\makeatletter
\DeclareRobustCommand*{\escapeus}[1]{%
\begingroup\@activeus\scantokens{#1\endinput}\endgroup}
\begingroup\lccode\~=\_\relax
\lowercase{\endgroup\def\@activeus{\catcode\_=\active \let~\_}}
\makeatother

\begin{figure}
\centering % Make it look nice in the center
% Pass in the custom macro to the svg environment
\includesvg[pretex=\escapeus]{mysvgfile}
\end{figure}


When I use the code as shown above, I get several errors:

File ended while scanning use of \@import ...retex=\escapeus]{mysvgfile}
Missing $inserted ...retex=\escapeus]{mysvgfile} Extra }, or forgotten$ ...retex=\escapeus]{mysvgfile}
Extra }, or forgotten $...retex=\escapeus]{mysvgfile} Missing$ inserted \end{figure}
Missing } inserted \end{figure}
Missing } inserted \end{figure}
....


If someone can tell me what I'm missing I would really appreciate it.

This is basically a follow-up on the question: How to escape underscores in the resulting .pdf_tex file? which I attempted to answer with this partial solution, and another user suggested that I ask this as a new question since I just can't get this to work and there was no activity on the other question one for a year.

To my amazement it was as easy as changing \includesvg[pretex=\escapeus]{mysvgfile} to \escapeus{\includesvg{mysvgfile}}.

\escapeus expects to be passed some TeX as #1, on which it operates to remove the underscores. You can see by feeding it an empty argument {} that the errors above are removed, only to be replaced by the original error when the underscore is encountered. However the \escapeus command can handle being passed the entire result of the \includesvg command.

• I wonder if a tweak along these lines should be built in to the svg package. I also wonder (and will test) what happens if the svg contains subscripts. – Chris H Jan 12 '17 at 12:14
• Apparently inkscape's pdf+latex export routine can't deal with subscripts anyway -- test<sub>subscript</sub> ends up as testsubscript -- so this doesn't cause any new problems. – Chris H Jan 12 '17 at 12:18

You can also use the parameter inkscapelatex=false. This will tell LaTeX not to render the text.

So this should work:

\documentclass{article}
\usepackage{svg}

\begin{document}

\begin{figure}
\centering
\includesvg[inkscapelatex=false, width=\linewidth]{pic}
\caption{caption}
\label{img:label}
\end{figure}

\end{document}
`

Source: package documentation

• Although not specifically what I was asking, this is another option to avoid the problem to start with, and was something else I needed to do in other places where you actually had the fonts in the SVG that you wanted, and didn't want LaTeX "helping" you by re-formatting your nice SVG at all. – Jedidiah Bartlett Feb 11 at 19:55