22

How do I control how much vertical space appears before and after a verbatim environment?

With the following source (run through pdflatex):

\documentclass[varwidth]{standalone}
\begin{document}
text before verbatim
\begin{verbatim}
some verbatim text
\end{verbatim}
text after verbatim
\end{document}

I get the following:

bad behavior

I would like to be able to tweak things to get the following behavior:

good behavior

Edit:

I would also like the solution to work for verbatim environments in a list item:

\documentclass[varwidth]{standalone}
\begin{document}
\begin{itemize}
\item text before verbatim
\begin{verbatim}
some verbatim text
\end{verbatim}
text after verbatim
\end{itemize}
\end{document}

bad behavior in a list

1
  • 1
    If you don't need verbatim text, you can always use \ttfamily or \texttt{...} and then have the regular paragraph skip between the lines.
    – Werner
    Feb 3, 2012 at 21:30

1 Answer 1

19

verbatim uses trivlist which adds above and below it the current values of \topsep and \partopsep (the latter if at the beginning of a paragraph); since the trivlist is started when a group has already been opened by verbatim, it's safe to set locally the values: the command to patch is \@verbatim, that starts with \trivlist, so we can set the parameters just before this command:

\usepackage{etoolbox}
\makeatletter
\preto{\@verbatim}{\topsep=0pt \partopsep=0pt }
\makeatother

Here's an example, where I put two minipage environments next to each other just to show the line spacing:

\documentclass{standalone}
\usepackage{etoolbox}
\makeatletter
\preto{\@verbatim}{\topsep=0pt \partopsep=0pt }
\makeatother
\begin{document}
\begin{minipage}{5cm}
text before verbatim
\begin{verbatim}
some verbatim text
\end{verbatim}
text after verbatim
\end{minipage}%
\begin{minipage}{5cm}
abc\\
def\\
ghi
\end{minipage}
\end{document}

enter image description here

6
  • fantastic! thank you! do you happen to have a version that works with enumitem up your sleeve? :) Feb 3, 2012 at 23:30
  • @RichardHansen It's not clear what you are asking.
    – egreg
    Feb 3, 2012 at 23:39
  • The enumitem package apparently modifies \trivlist to restore spacing to defaults. This allows it to modify the spacing for normal lists without affecting spacing for verbatim and others. Unfortunately, it means that your solution won't work if enumitem is in use. Feb 3, 2012 at 23:45
  • 1
    I was wrong---it's not enumitem's fault. The verbatim still has space above and below when in a list environment (see edited question). Your solution reduces that space, but there's still some there. I'm guessing something else is responsible for that space... Feb 4, 2012 at 6:24
  • 2
    It seems that \AtBeginEnvironment{verbatim}{\setlist[trivlist]{nolistsep}} works. \setlist[trivlist]{nolistsep} can be found in the document of enumitem (Section 12).
    – jiaopjie
    Feb 12, 2019 at 4:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .