1

I've created a listing with multiple code blocks using the \inputminted command And want to reduce the space between the blocks.

A minimal working example is given here:

\documentclass[11pt,oneside]{scrreprt}
\usepackage{caption}
\usepackage{xcolor}
\usepackage{makeidx}

\usepackage[chapter]{minted}
\usemintedstyle{friendly}
\setminted{%
  bgcolor=lightgray,
  linenos
}

\makeindex

\begin{document}
\begin{listing}
    \inputminted{cpp}{helloworld.cpp}
    \inputminted{cpp}{helloworld.cpp}
    \caption{Example}
    \label{code}
\end{listing}
\end{document}

And helloworld.cpp:

#include <iostream>

int main() {
    std::cout << "Hello world!" << std::endl;
    return 0;
}

The generated output is:

Example for multiple minted inputs in a single listing.

I could reset the line numbers inside a minted environment. That way I could possibly implement an .h and a .cpp file. However I don't know how to do that with inputminted. A small distance between the blocks would be my desired solution.

1 Answer 1

1

Between two consecutive \inputminted, several glue items are added

.....\glue 6.0 plus 2.0 minus 2.0
.....\glue(\baselineskip) 13.6
.....\glue 6.0 plus 2.0 minus 2.0
.....\glue(\baselineskip) 7.6
.....\vbox(6.0+0.0)x0.0

Indeed, if I try

\inputminted{cpp}{\jobname.cpp}

\vspace{-\medskipamount}
\vspace{-2\baselineskip}
\vspace{0.4pt}% just to show a very small white space

\inputminted{cpp}{\jobname.cpp}

I get

enter image description here

I suggest a macro \betweenminted which you can use to decide the amount of vertical space.

\begin{filecontents*}{\jobname.cpp}
#include <iostream>

int main() {
    std::cout << "Hello world!" << std::endl;
    return 0;
}
\end{filecontents*}

\documentclass[11pt,oneside]{scrreprt}
\usepackage{caption}
\usepackage{xcolor}

\usepackage[chapter]{minted}

\usemintedstyle{friendly}
\setminted{
  bgcolor=lightgray,
  linenos
}

\newcommand{\betweenminted}[1]{%
  \par\vspace{-\medskipamount}\vspace{-2\baselineskip}%
  \vspace{#1}%
}

\begin{document}

\begin{listing}

\inputminted{cpp}{\jobname.cpp}

\betweenminted{\medskipamount}

\inputminted{cpp}{\jobname.cpp}

    \caption{Example}
    \label{code}
\end{listing}

\end{document}

enter image description here

Production note: I changed the name of the input file just not to clobber my files.

You must log in to answer this question.

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