# 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