[Tex/LaTex] Remove blank lines in minted

minted

Is it possible to remove blank (empty) lines in minted, and gobble multiple blank lines to a specified amount, like the emptylines option in listings?

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{minted}
\usepackage{listings}
% I want something like the emptylines option
\lstset{language=[Visual]{C++},emptylines=1}
\begin{document}
\begin{minted}[frame=single]{cpp}
int i = 0;


i++;
\end{minted}
\begin{lstlisting}
int i = 0;


i++;
\end{lstlisting}
\end{document}

Best Answer

The minted package uses fancyvrb behind the scenes. This package doesn't have a build-in facility to skip blank lines. It is possible to add a test to skip blank lines, for example

\begingroup
\makeatletter
\catcode`\^^M=\active
\gdef\FancyVerbGetLine#1^^M{%
  \@nil
  \FV@CheckEnd{#1}%
  \ifx\@tempa\FV@EnvironName
    \ifx\@tempb\FV@@@CheckEnd\else\FV@BadEndError\fi
    \let\next\FV@EndScanning
  \else
    \def\FV@Line{#1}%
    \ifx\FV@Line\@empty % Test added here
      \def\next{\FV@GetLine}%
    \else
      \def\next{\FV@PreProcessLine\FV@GetLine}%
    \fi
  \fi
  \next}%
\endgroup

Dealing with picking particular numbers of blank lines is more tricky: Id be tempted to simply go for listings or edit the code source rather than try to alter this.

Related Question