How to escape string in a command argument

Viewed 552

Minimal example:

\documentclass{article}

\usepackage{minted}
\usepackage{syntax}

\begin{document}

\begin{minted}{some_thing}
foo
\end{minted}

\end{document}

When compiling with Miktex/pdflatex, I get

! Missing \endcsname inserted.
<to be read again>
                   \protect
l.8 \begin{minted}{some_thing}

So far, I have figured out that the underscore in some_thing is the problem. This is only a problem when I add the syntax package. Note that the best case here is still an error: Package minted Error: Missing Pygments output;. The argument in question is supposed to point to a file. The example is really contrived, sorry.

So I guess I need to escape the underscore somehow? Just going some\_thing gives me the same error. I guess command arguments are somehow different. What can I do here?

1 Answers

I found an answer here: https://tex.stackexchange.com/a/302168

\# is a \chardef token which typesets a # but doesn't expand to a # in filenames etc

The post uses the command \string to escape a #.

That explains why \_ doesn't do anything in that place. In my case, \begin{minted}{some\string_thing} works just fine.

I tried \begin{minted}{\string{some_thing}} because that would be more readable IMO, but that doesn't work. I guess that's not how \string works? ¯\_(ツ)_/¯

Related