TransWikia.com

Remove whitespace before and after minted

TeX - LaTeX Asked by ZuseZ3 on April 6, 2021

I am including Rust code into my Latex files by using minted. I am mostly showing 1-3 lines of code, with an explanation afterwards. So the large gaps before and after begin{minted}{rust}...end{minted} sections leaf my pages ripped.

I am using the following command for the one-lineres, they are fine due to not having any whitespaces around.
Unfortunately, it removes indentations and is therefore not suited for multi-line functions:
newcommand{rust}[1]{mintinline{rust}{#1}}

How can I get the behaviour of mintinline for a multiline function without any whitespaces above or under my code?

usepackage{minted}
newcommand{rust}[1]{mintinline{rust}{#1}}
begin{document}
This is a text directly above the code
begin{minted}[autogobble]{rust}
if 5 > 3 {
    var = 3;
}
end{minted}
This is a text which should come directly under the code.
end{document}

One Answer

Five years ago the opposite question was asked in How to space before and after a `minted` code block with bgcolor?, where minted blocks with a background color did not have any vertical space. This issue has since been fixed, but for the current question you can reverse the answer there, i.e., instead of adding medskip to the box with the background color you can remove the medskip using etoolbox. You probably don't want a background color, but setting white looks the same as no color at all.

MWE:

documentclass{article}
usepackage{minted}
usepackage{etoolbox}
makeatletter
% replace medskip before and after the box with nothing, i.e., remove it
patchcmd{minted@colorbg}{medskip}{}{}{}
patchcmd{endminted@colorbg}{medskip}{}{}{}
makeatother
begin{document}
begin{minipage}[t]{0.5textwidth}
noindent This is a text directly above the code
begin{minted}[autogobble]{rust}
if 5 > 3 {
    var = 3;
}
end{minted}
This is a text which should come directly under the code.
end{minipage}
begin{minipage}[t]{0.5textwidth}
noindent This is a text directly above the code
begin{minted}[autogobble,bgcolor=white]{rust}
if 5 > 3 {
    var = 3;
}
end{minted}
This is a text which should come directly under the code.
end{minipage}
end{document}

Result side by side:

enter image description here

Answered by Marijn on April 6, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP