6

I'm using the KOMA scrbook class to write my thesis. I have source code blocks that I wish to display with minted. An MWE can be seen here:

% Intended LaTeX compiler: pdflatex
\documentclass{scrbook}
\usepackage{listings}
\usepackage{minted}
\setminted{autogobble=true,fontsize=\small,baselinestretch=0.8,frame=lines}
\setminted[python]{python3=true,tabsize=4}
\usemintedstyle{trac}
\renewcommand{\listingscaption}{Code Snippet}

\title{MWE}
\begin{document}

\maketitle
\tableofcontents


\chapter{Test}

\begin{listing}[htbp]
\begin{minted}[]{python}
a = 2
\end{minted}
\caption{\label{example}
caption}
\end{listing}

\end{document}

And this is what the code block looks like: enter image description here

How can I globally (in the preamble) reduce the distance between the listing and its caption?!

I'm looking for a solution that does not change the distance between figures or tables and their captions. I've tried using the newfloat environment as in \usepackage[newfloat]{minted}, and then calling \captionsetup[listing]{skip=-10pt}, but then the \renewcommand{\listingscaption}{Code Snippet} command seems to stop having any effect.

Help and suggestions highly appreciated.

3 Answers 3

2
+50

How about this

\documentclass{scrbook}
\usepackage{listings}
\usepackage{minted}
\setminted{autogobble=true, fontsize=\small, baselinestretch=0.8, frame=lines}
\setminted[python]{python3=true, tabsize=4}
\usemintedstyle{trac}
\renewcommand{\listingscaption}{Code Snippet}
\AtEndEnvironment{listing}{\vspace{-8pt}} % <------- 
\title{MWE}
\begin{document}

\maketitle
\tableofcontents
\chapter{Test}

\begin{listing}[htbp]
\begin{minted}[]{python}
a = 2
\end{minted}
\caption{\label{example}caption}
\end{listing}

\end{document}

enter image description here

Of course you can change the value of \vspace to whatever you like.

2
  • Thank you. Very simple solution and doesn't require loading any extra packages. Worked just fine.
    – Daniel
    Mar 8, 2019 at 21:44
  • Thanks for answer, saved my time. Dec 3, 2020 at 12:31
3
\documentclass{scrbook}
\usepackage{listings}
\usepackage{minted}
\setminted{autogobble=true,fontsize=\small,baselinestretch=0.8,frame=lines}
\setminted[python]{python3=true,tabsize=4}
\usemintedstyle{trac}
\renewcommand{\listingscaption}{Code Snippet}
\title{MWE}
\usepackage{caption}
\captionsetup[plain]{skip=-10pt}
\begin{document}

\maketitle
\tableofcontents


\chapter{Test}

\begin{listing}[htbp]
\begin{minted}[]{python}
a = 2
\end{minted}
\caption{\label{example}
caption}
\end{listing}

\end{document}

enter image description here

1
1

To change spacing of captions I usually just use a \vspace{} with a distance varying from case to case. You could for example try the following. (Unfortunately I can't get your code to work so I can't replicate the situation but it's worth a try)

\documentclass{scrbook}
\usepackage{listings}
\usepackage{minted}
\setminted{autogobble=true,fontsize=\small,baselinestretch=0.8,frame=lines}
\setminted[python]{python3=true,tabsize=4}
\usemintedstyle{trac}
\renewcommand{\listingscaption}{Code Snippet}

\title{MWE}
\begin{document}

\maketitle
\tableofcontents


\chapter{Test}

\begin{listing}[htbp]
\begin{minted}[]{python}
a = 2
\end{minted}
\vspace{-0.5cm}
\caption{\label{example}
caption}
\end{listing}

\end{document}

You must log in to answer this question.

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