# Number signs in \mintinline in \footnote

I'm having an issue with typesetting code using the minted package when I want to highlight for example print(x) # prints x in a footnote:

\documentclass{article}
\usepackage{minted}
\begin{document}
Comments in Python\footnote{\mintinline{python}{print(x) # prints x}}.
\end{document}


The issue is that # is printed twice:

If I were to highlight code with the percent sign, I could change its catcode:

\catcode\%=11


However, changing the catcode of # gives an error, since it is used in the definition of \mintinline. The error is:

! Undefined control sequence.
\PYG #1#2->\FV@PYG
{#1}{\FancyVerbBreakStart #2\FancyVerbBreakStop }
l.2 ...x}\PYG{p}{)} \PYG{c+c1}{\PYGZsh{} prints x}


How can I fix this (in a scalable way)? I intend to use this quite a lot, since # is a common character in the ARM assembly code I intend to typeset. (I cannot provide an ARM example here, since the ARM lexer is not in the latest pygments release yet).

• The minted documentation that things get complicated when putting \mintinline in for example footnotes when using # and %, but does not give a solution. – Keelan Sep 28 '16 at 6:08
• There might not be a direct solution. If there's another macro grabbing the \mintinline everything is tokenized. Isn't there a macro to define something, like \defmintinline\name{print(x) # prints x}? Then you can use \name whenever you want and it works right. – Manuel Sep 28 '16 at 9:20
• @Manuel you were right, thanks. It turned out to be in the FAQ section, where I didn't look at first. I wrote an answer about it. – Keelan Sep 28 '16 at 10:52

Dealing with verbatim material in footnotes is always a pain.

You can do this in two steps: defining the string to typeset and then use it.

\documentclass{article}
\usepackage{xparse}
\usepackage{minted}
\ExplSyntaxOn
\NewDocumentCommand{\mintedstring}{mv}
{
\tl_clear_new:c { l_minted_string_#1_tl }
\tl_set:cn { l_minted_string_#1_tl } { #2 }
}
\NewDocumentCommand{\mintinlinestring}{mm}
{
\minted_inline_string:nv { #1 } { l_minted_string_#2_tl }
}
\cs_new_protected:Nn \minted_inline_string:nn
{
\mintinline{#1}{#2}
}
\cs_generate_variant:Nn \minted_inline_string:nn { nv }
\ExplSyntaxOff

\setlength{\textheight}{2cm} % just to keep the picture small

\begin{document}

\mintedstring{print}{print(x) # prints x}%
\footnote{\mintinlinestring{python}{print}}.

\end{document}


• This should be in the package, in my opinion, it's a really useful thing and leaves out all the “pain” of footnotes, for instance; and also the possibility to define multiline verbatim code and use it later. Similar to the behaviour in ConTeXt. – Manuel Sep 28 '16 at 12:25
• Thanks a lot, I didn't notice the font size yet. Minted is on github, are you interested in making a pull request? If you don't have the time for that, I can also do it. – Keelan Sep 28 '16 at 13:44

I dove further into the documentation and it turns out there is something written about it:

I want to use \mintinline in a context that normally doesn’t allow verbatim content. The \mintinline command will already work in many places that do not allow normal verbatim commands like \verb, so make sure to try it first. If it doesn’t work, one of the simplest alternatives is to save your code in a box, and then use it later. For example,

\newsavebox\mybox
\begin{lrbox}{\mybox}
\mintinline{cpp}{std::cout}
\end{lrbox}

\commandthatdoesnotlikeverbatim{Text \usebox{\mybox}}


Hence, my code could be adapted to:

\documentclass{article}
\usepackage{minted}
\begin{document}
\newsavebox\mybox
\begin{lrbox}{\mybox}
\mintinline{python}{print(x) # prints x}
\end{lrbox}
`