Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question

1 Answer 1

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

Your Answer

 
discard

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.