15

I want to use the minted package to give me syntax highlighting but it has a spacing of more than a centimeter.

Setting:

\setlength{\parskip}{0pt}
\setlength{\parsep}{0pt}
\setlength{\headsep}{0pt}
\setlength{\topskip}{0pt}
\setlength{\topmargin}{0pt}
\setlength{\topsep}{0pt}
\setlength{\partopsep}{0pt}

does not help at all. Is there any way to reduce the spacing from the rest of the text?

Cœur
  • 37,241
  • 25
  • 195
  • 267
cvandonderen
  • 153
  • 1
  • 4

2 Answers2

13

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

Community
  • 1
  • 1
Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
  • I like how this answer appears to be so much simpler than the one liked to. :) – lcnittl Jun 12 '20 at 09:45
  • This affects more than `minted` environments. – Raffi Khatchadourian Jul 27 '20 at 16:03
  • @RaffiKhatchadourian Yes, of course: the first sentence in my answer should have made that clear. If you want it to only affect minted environments, use `\newenvironment` in conjunction with `\VerbatimEnvironment` to redefine the variables locally, or use the solution I’ve linked at the very end of my answer to adjust all fancyvrb `Verbatim` environments. – Konrad Rudolph Jul 27 '20 at 18:54
2

If you want to change the line spacing you can use:

\linespread{1.0}

And play a little bit around with the number in the curly brackets.

The commands you have given as an example refer to the page environment. See this Wiki for additional explanations and examples.

Mehdi Charife
  • 722
  • 1
  • 7
  • 22
mropa
  • 11,562
  • 10
  • 33
  • 29
  • That helps a bit. The problem is that I have the following code: An example piece of HTML code would look like: \begin{minted}[frame=lines, framesep=2mm]{html} Hello \end{minted} The output of this example in a browser would be: {\bf Hello} . When I but this in my document there is a gap of a centimeter before and after the minted environment. I want to reduce this distance to half a centimeter. How to do that? – cvandonderen Feb 23 '10 at 14:53
  • @cvandonderen take a look at Konrad's answer, that should do the trick. – mropa Feb 23 '10 at 15:28