I'm writing a book in which I'm going to insert a lot of code listings (using minted). Usually, a code listing is followed by the output of the execution (either an equation, or plain text formatted with \texttt{}). In some cases, it could be followed by another text paragraph explaining what the code does.

I'm also using the package parskip: I like having spaces between text paragraphs, as well as no indentation. However, weird spacing is troubling me with code listings and equations, as we can see in the following example:

\usepackage[top=1in, bottom=1in, left=1.1in, right=1in]{geometry}


\usepackage{bm} % math bold italic font

\usepackage{amsfonts} % display R, C, Z, ...
\usepackage{amssymb} % \veebar

\DeclareMathOperator\erf{erf} % error function
\DeclareMathOperator\modf{frac} % fractionalpart
\usepackage[multiple]{footmisc} % multiple footnotes at one point

\usepackage{xurl} % autowrap long urls in footnotes
\usepackage{enumitem} % characters instead of numbers in enumerate lists

\usepackage[dvipsnames, table]{xcolor}
\definecolor{lightgray}{rgb}{0.9, 0.9, 0.9}
\setminted[python]{bgcolor=lightgray, breaklines}
%\AtBeginEnvironment{minted}{\setlength{\parskip}{-4pt}} % reduce empty vertical space after minted paragraph





a = cos(x)**x / (2 + exp(x))
\frac{\cos^{x}{\left(x \right)}}{e^{x} + 2}



print(isinstance(a, int))

        y = m x + q \\
        y = m x + q



# line of code




The picture shows the current situation: in general, I would love to reduce the white space around the equation below the code listings. In the second code listing, the "output" text is placed very far away: I would love to reduce that spacing. In the last code listing, there is just too much white space after it. Ideally, I would love for each code listing and the next paragraph to be treated as one paragraph, thus not having spacing between them...

I've played with \BeforeBeginEnvironment{minted} and \AtBeginEnvironment{minted}, unfortunately I'm unable to achieve what I desire.

Which brings me to this post: what can I change? Are there alternatives to parskip?

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.