minted internally uses the Verbatim
environment from the fancyvrb package. In the documentation of the implementation, the following formula for the spaces is given:
<topskip> = \topsep + \partopsep + \parskip
<botskip> = \topsep + \partopsep
And
Except when in label or after nobreak, \parskip
is added with \addvspace
, so that net space is:
MAX{\topsep (+\partopsep) + \parskip , \lastskip }
(The usual \@item
works the same way.)
Hence, setting \partopsep
to some other value does the trick; I’ve tried it, and you need a negative value to remove the margin:
\setlength\partopsep{-\topsep}
This removes most of the space between the text body and the code. To get a distance of 0.5cm, add its distance to that:
\setlength\partopsep{-\topsep}
\addtolength\partopsep{-\parskip}
\addtolength\partopsep{0.5cm}
An implementation can be found in https://tex.stackexchange.com/a/19423