5

How can I reduce the space around all alltt environment blocks?

\documentclass{article}
\usepackage{alltt}
\begin{document}

Test1
\begin{alltt}
some code
\end{alltt}
Test2

\end{document}

This question seems to be relevant but doesn't provide a clean answer:

Proper way of vertical spacing before/after environments

jaynp
  • 635

2 Answers2

5

The space is comming from topsep, so you need to set this to 0 for this environment. The easiest way to this is with \AtBeginEnvironment from the etoolbox package which will keep the change local to the environment:

Sample output

\documentclass{article}
\usepackage{alltt}
\usepackage{etoolbox}

\AtBeginEnvironment{alltt}{\setlength{\topsep}{0pt}}

\begin{document}

Test1
\begin{alltt}
some code
\end{alltt}
Test2

\end{document}

Putting \showthe\topsep in the file outside the patched environment, gives

> 8.0pt plus 2.0pt minus 4.0pt.
l.14 \showthe\topsep

showing its standard value for this document. This stretchable space was what you were seeing in the original. The alltt environment is built form a trivlist. If you look in the LaTeX source (texdoc source2e on most systems), you will see a description of the vertical spacing parameters for such environments.

Andrew Swann
  • 95,762
0

You could redefine the environment, adding an adjustment of the space at the beginning and the end, which you can still modify by changing the factors:

\documentclass{article}
\usepackage{alltt}
\begin{document}

\let\oldalltt\alltt
\renewenvironment{alltt}{\vspace{-0.75\baselineskip}\begin{oldalltt}}{\end{oldalltt}\vspace{-0.1\baselineskip}}

Test1
\begin{alltt}
some code
\end{alltt}
Test2

\end{document}
muk.li
  • 3,620