5

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.

1
+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.

  • Thank you. Very simple solution and doesn't require loading any extra packages. Worked just fine. – dangom Mar 8 at 21:44
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

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}

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.