TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

How do I get the appropriate vertical space around a \begin{code}/\end{code} or \begin{spec}/\end{spec} in a figure?

Here is a basic example with fancyhdr added to show the top of the page:

\usepackage{fancyhdr} \pagestyle{fancy} \fancyhf{} \fancyhead[C]{}
%include polycode.fmt
Some text.
main = putStrLn "Hello, world!"
\caption{Hello world example}

Basic example

Unfortunately, when you compare this figure to another without code, the spacing above and below the code is noticeably more.

When looking at the code of lhs2TeX, I noticed the use of \abovedisplayskip and \belowdisplayskip. So I tried setting those to 0pt:


Using <code>\abovedisplayskip</code> and <code>\belowdisplayskip</code>

This looks more like other figures with just text. However, if I want to center the code, I get extra vertical space again:


With centering

What's the recommended way to get the appropriate vertical spacing around a (possibly centered) code block in a figure?

share|improve this question
up vote 1 down vote accepted

You can try saying \texths in the figure. That should remove all outer space and should treat the code block more or less as if it was plain text.

share|improve this answer
Excellent. That avoids the need for \abovedisplayskip and \belowdisplayskip. The centering seems to have the same space-increasing effect on plain text and code blocks, which suggests that it is independent of lhs2TeX. Any idea about how to fix that? – Sean Leather Jun 24 '14 at 10:30
Here's the solution: use \centering instead of \begin{center}/\end{center} inside a float. – Sean Leather Jun 24 '14 at 10:34

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.