7

I'm using the code found in this answer to allow minted to provide inline code. But, unfortunately, this breaks line numbering, which is not shown anymore.

How can I have both inline code and line numbering with minted?


MWE: no inline code with line numbering:

\documentclass[a6paper,10pt]{article}

\usepackage{minted}

\begin{document}

Ciao \mint[linenos]{c}/int a = 1;/ Prova

\begin{minted}[linenos]{c}
    int a = 1;
\end{minted}

\end{document}

Result:

enter image description here

MWE with inline code, line numbering not working:

\documentclass[a6paper,10pt]{article}

\usepackage{minted}

\makeatletter
% avoid space tokens since we're in horizontal mode
\renewcommand\mint[3][]{%
  \DefineShortVerb{#3}%
  \minted@resetoptions
  \setkeys{minted@opt}{#1}%
  \SaveVerb[aftersave={%
    \UndefineShortVerb{#3}%
    \minted@savecode{\FV@SV@minted@verb}%
    \minted@pygmentize{#2}%
    \DeleteFile{\jobname.pyg}}]{minted@verb}#3}
\renewcommand\minted@savecode[1]{%
  \immediate\openout\minted@code\jobname.pyg\relax
  \immediate\write\minted@code{#1}%
  \immediate\closeout\minted@code}
\renewcommand\minted@pygmentize[2][\jobname.pyg]{%
  \def\minted@cmd{pygmentize -l #2 -f latex -F tokenmerge
    \minted@opt{gobble} \minted@opt{texcl} \minted@opt{mathescape}
    \minted@opt{linenos} -P "verboptions=\minted@opt{extra}"
    -o \jobname.out.pyg #1}%
  \immediate\write18{\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
\RecustomVerbatimEnvironment{Verbatim}{BVerbatim}{}

\begin{document}

Ciao \mint[linenos]{c}/int a = 1;/ Prova

\begin{minted}[linenos]{c}
    int a = 1;
\end{minted}


\end{document}

Output:

enter image description here

  • Would a verbatim solution be good enough, or do you need the color highlighting that minted provides? Also, how is inline code supposed to look, with line numbering? – Steven B. Segletes Jun 25 '13 at 18:41
  • @StevenB.Segletes I'd like to have the syntax highlighting with inline code as well. The problem with line numbering is not with inline code: all line numbering is broken(as you can see in the last output). – Bakuriu Jun 25 '13 at 18:54
  • Too bad. I know how to do a few verbatim tricks, but don't know anythong about minted. Good luck from another reader! – Steven B. Segletes Jun 25 '13 at 19:05
1

Starting from version 2, minted supports \mintinline:

\documentclass{article}

\usepackage{minted}

\begin{document}

Ciao \mintinline{c}/int a = 1;/ Prova

\begin{minted}[linenos]{c}
    int a = 1;
\end{minted}

\end{document}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.