8

I'd like to put some inline code using minted but when I try to typesent the empty string "" I get an "undefined control sequence" error.

Sample code:

\documentclass[a4paper,10pt]{article}
\usepackage[utf8]{inputenc}
\usepackage[italian]{babel}
\usepackage{minted}

\begin{document}
\footnote{\mint{bash}/""/}
\end{document}

The pdf is created anyway but I get this instead of the expected "":

\def {\futurelet \protect \let \relax }\def {\futurelet \protect \let \relax }

(Yes, I really mean that the above commands are printed in the footnote in the pdf).

Commenting out babel solves the problem, but I need babel in my real TeX file. Also I do not want to use `` and '', since they give an output that does not match with the code inserted using \inputminted. Is there a way to fix this? Eventually how can I put real a doublequote(a.k.a. ASCII 34)? Note that \mint is like \verb so normal LaTeX commands are ignored.

On a side note I'm using this answer to typeset inline code with minted, but it is not the cause of the problem.

2
  • 1
    Just say \shorthandoff{"} after \begin{document}, if you don't need the shorthands. However, expect problems with \mint in footnotes.
    – egreg
    Nov 20, 2012 at 18:44
  • @egreg That fixes the problem. Could you explain why using mint in footnotes could create problems? And, since I need to have some syntax highlighting of keywords in the footnotes how should I do it without using mint and being consistent with the rest of the document? (Also note that I cannot use custom commands otherwise Kile would screw up the highlightining due to a lot of $ inside code, so saying create a newcommand with an optional arg is not a solution).
    – Bakuriu
    Nov 20, 2012 at 19:44

1 Answer 1

8

If you don't have to use minted, you could try my pythontex package, which uses the same Pygments highlighting library. It provides a \pygment command for inline use, and this seems to work fine in footnotes, except that # and % characters don't work (the # and % work fine outside footnotes in normal text, and it wouldn't be hard to get around the footnote issue if these characters are needed). The package also provides a pygments environment and an \inputpygments command.

\documentclass[a4paper,10pt]{article}
\usepackage[utf8]{inputenc}
\usepackage[italian]{babel}
\usepackage{pythontex}

\begin{document}
\footnote{Quotes: \pygment{bash}/""/.  More: \pygment{bash}/echo ${a[$i,$j]}/.}
\end{document}

enter image description here

More explanation and patch for minted

The ultimate issue is that minted uses verbatim content (ShortVerb from fancyvrb), and that's problematic in footnotes. My package uses a different approach, and also caches all highlighted content to increase speed.

Here's a rough patch for minted that should give you what you want. It makes \mint into an inline command. It's based on some things I did in my package, so it should work in footnotes as well as my package does. I had to make new versions of \minted@savecode and \minted@pygmentize to get rid of excess spaces (added lots of %), and to temporarily swap Verbatim for BVerbatim. It's very important to avoid any extra spaces hanging around for inline use; the examples show that regular text can be right up against highlighted content.

Comment on performance: If you are going to redefine \mint for inline use (or, perhaps better, define a new command for that purpose), you will presumably use inline code fairly frequently. On my system, compiling takes about 12 s when \mint{bash}/""/ is used 40 times; it's about 23 s for 80 times. pythontex takes about 3.5 s for 40 uses of \pygment{bash}/""/ (this involves running pdflatex, then the pythontex script, and then pdflatex again, to bring back highlighted results). It's about the same for 80 uses, about 5.5 s for 800 uses, and about 23 s for 8000. And all of those numbers are only when pythontex actually has to highlight everything; since results are saved, the pythontex script and second pdflatex run are only needed when code is modified. If you are recompiling after editing non-code text, you only need to run pdflatex, and should get more or less normal pdflatex speeds.

enter image description here

\documentclass[a4paper,10pt]{article}
\usepackage[utf8]{inputenc}
\usepackage[italian]{babel}
\usepackage{minted}

\makeatletter
\renewcommand\mint{%
  \begingroup
  \let\do\@makeother\dospecials
  \catcode`\{=1
  \catcode`\}=2
  \@ifnextchar[{\endgroup\mint@i}{\endgroup\mint@i[]}}
\def\mint@i[#1]#2{%
  \minted@resetoptions
  \setkeys{minted@opt}{#1}%
  \gdef\mint@lang{#2}%
  \begingroup
  \let\do\@makeother\dospecials
  \mint@ii}
\def\mint@ii#1{%
  \endgroup
  \def\mint@iii##1#1{%
    \endgroup
    \expandafter\edef\csname FV@SV@minted@verb\endcsname{\detokenize{##1}}%
    \minted@savecode@inline{\FV@SV@minted@verb}%
    \minted@pygmentize@inline{\mint@lang}%
    \DeleteFile{\jobname.pyg}}%
  \begingroup
  \let\do\@makeother\dospecials  
  \mint@iii}

\newcommand\minted@savecode@inline[1]{%
  \immediate\openout\minted@code\jobname.pyg
  \immediate\write\minted@code{#1}%
  \immediate\closeout\minted@code}

\newcommand\minted@pygmentize@inline[2][\jobname.pyg]{%
  \RecustomVerbatimEnvironment{Verbatim}{BVerbatim}{}%
  \def\minted@cmd{pygmentize -l #2 -f latex -F tokenmerge
    \minted@opt{gobble} \minted@opt{texcl} \minted@opt{mathescape}
    \minted@opt{startinline} \minted@opt{funcnamehighlighting}
    \minted@opt{linenos} -P "verboptions=\minted@opt{extra}"
    -o \jobname.out.pyg #1}%
  \immediate\write18{\minted@cmd}%
  % For debugging, uncomment:
  %\immediate\typeout{\minted@cmd}
  \ifthenelse{\equal{\minted@opt@bgcolor}{}}%
   {}%
   {\begin{minted@colorbg}{\minted@opt@bgcolor}}%
  \input{\jobname.out.pyg}%
  \ifthenelse{\equal{\minted@opt@bgcolor}{}}%
   {}%
   {\end{minted@colorbg}}%
  \DeleteFile{\jobname.out.pyg}}
\makeatother


\begin{document}

Before environment
\begin{minted}{bash}
""
\end{minted}
After environment

Before inline\mint[]{bash}/""/After inline

Before note\footnote{Before\mint{bash}/""/After}

\end{document}

Edit 2013/07/30

I am now maintaining minted, and have incorporated code similar to the patch here to create a new inline command, \mintinline, in the development version on GitHub.

1
  • 1
    When you say "unless you need # or % characters" you mean only in the footnotes or in general? Because I do not think I need them in the footnotes, but I do need them in the text. Also can you provide some information about why footnotes interact badly with mint?(or some useful link?)
    – Bakuriu
    Nov 20, 2012 at 21:44

You must log in to answer this question.

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