3

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:

\documentclass{article}
\usepackage{fancyhdr} \pagestyle{fancy} \fancyhf{} \fancyhead[C]{}
%include polycode.fmt
\begin{document}
Some text.
\begin{figure}[t]
\begin{code}
main = putStrLn "Hello, world!"
\end{code}
\caption{Hello world example}
\end{figure}
\end{document}

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:

...
\begin{figure}[t]
\setlength{\abovedisplayskip}{0pt}
\setlength{\belowdisplayskip}{0pt}
\begin{code}
...

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:

...
\begin{figure}[t]
\setlength{\abovedisplayskip}{0pt}
\setlength{\belowdisplayskip}{0pt}
\begin{center}
...

With centering

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

1 Answers1

2

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.

kosmikus
  • 311
  • 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
  • 2
    Here's the solution: use \centering instead of \begin{center}/\end{center} inside a float. – Sean Leather Jun 24 '14 at 10:34