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:


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
    \lowercase{\endgroup\def\@activeus{\catcode`\_=\active \let~\_}}

    \centering % Make it look nice in the center
    % Pass in the custom macro to the svg environment

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.


2 Answers 2


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
    Commented Jan 12, 2017 at 12:14
  • 1
    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
    Commented Jan 12, 2017 at 12:18

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

So this should work:



    \includesvg[inkscapelatex=false, width=\linewidth]{pic}


Source: package documentation

  • 1
    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. Commented Feb 11, 2020 at 19:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .