1

I would like to add a little separation between some lines of code for readibility. Consider this MWE:

\documentclass{article}

\usepackage{minted}

\begin{document}

\begin{minted}[linenos]{python} foo(1) bar(1) foofoo(1) foobar(1) \end{minted}

\end{document}

The produced output of course looks like

1 foo(1)
2 bar(1)
3 foobar(1)
4 foobaz(1)

What I would want to see is

1 foo(1)
2 bar(1)

3 foobar(1) 4 foobaz(1)

(With not necessarily an entire line’s spacing between lines 2 and 3, but some extra space.)


I first tried to use the minted options [escapeinside=] and \\[2ex], but it did nothing. I mean the \\ was recognized as a TeX command, since it did not show in the output, but no 2ex spacing was added.

I then stumbled upon David’s (brilliantly engineered ☺) answer here, but that solution still increments the line counter on the next line.

\renewcommand\theFancyVerbLine{%
    \ifnum\value{FancyVerbLine}=3
        \setcounter{FancyVerbLine}3
    \else
        {\scriptsize\arabic{FancyVerbLine}}
    \fi
}

I cannot do \setcounter{FancyVerbLine}2 as that would cause an infinite loop, setting each line number to 2 starting from the 3rd line and not printing anything.

bp99
  • 605
  • I also experimented with adding a \newif\ifthreeocunted\threecountedfalse and then doing \ifthreecounted\arabic{FancyVerbLine}\else\setcounter{FancyVerbLine}2\threecountedtrue in the \ifnum... block, but something is still missing. Line numbers are only printed up to 2. – bp99 Oct 24 '21 at 21:51

1 Answers1

1

You might use an invisible rule with the appropriate depth.

\documentclass{article}

\usepackage{minted}

\newcommand\spacer[1]{\rule[-#1]{0pt}{#1}}

\begin{document}

\begin{minted}[linenos,escapeinside=||]{python} foo(1) bar(1)|\spacer{3ex}| foofoo(1) foobar(1) \end{minted}

\end{document}

enter image description here

egreg
  • 1,121,712
  • This is superb. Thanks. I think it’s useful to have a default parameter if used often \newcommand*\spacer[1][3ex]{\rule[-#1]{0pt}{#1}} – bp99 Oct 24 '21 at 22:36
  • @bp99 Yes, that can be a good idea. For some reasons, |\rule[3ex]{0pt}{3ex}| doesn't work, but I'm not going to investigate. – egreg Oct 24 '21 at 22:41