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.

I have a series of figure-like paragraphs generated by a command of mine, and would like to have a minimum amount of vertical space above and below these figure-like paragraphs, but I don't want the double the space when two of them follow eachother.

Using css, I would use the following code, since CSS merges two consecutive margins and leaves only a space equivalent to the largest :

.myclass { margin-top: 1cm; margin-bottom: 1cm; }

I can achieve the result I want (the same space above the first \mycommand, between the two \mycommand, and after the second \mycommand) using the following LaTeX code, but I would like to avoid the \vskip 1cm that I manually added after the second \mycommand :

\documentclass{article}
\usepackage{lipsum}
\def\mycommand{
  \bgroup
  \vskip 1cm
  \rule{10cm}{2cm}
  \par
  \egroup
}
\begin{document}
\lipsum[1]
\mycommand
\mycommand
\vskip 1cm
\lipsum[2]
\lipsum[3]
\end{document}

Manually added space

If I choose the option to redefine \mycommand like this, then I have twice the space between the first \mycommand and the second :

\def\mycommand{
  \bgroup
  \vskip 1cm
  \rule{10cm}{2cm}
  \par
  \vskip 1cm
  \egroup
}

Double space

I have tried fiddling with TeX's glues (1cm minus 1cm for example), but didn't get the result I want.

share|improve this question
add comment

1 Answer 1

up vote 7 down vote accepted

LaTeX provides \addvspace{<len>} that adds vertical space only if needed, up to <len>. This seems to be what you're after:

enter image description here

\documentclass{article}
\usepackage{lipsum}% http://ctan.org/pkg/lipsum
\def\mycommand{
  \bgroup
  \addvspace{1cm}
  \rule{10cm}{2cm}
  \par
  \addvspace{1cm}
  \egroup
}
\begin{document}
\lipsum[1]
\mycommand
\mycommand
\lipsum[2]
\lipsum[3]
\end{document}

One area where \addvspace is commonly used is in ToC-entries (like the "List of Figures"/LoF) to denote the gap between chapter entries (in book and report document classes). For example, if some chapter has no figures, you don't want an additional (or larger) gap between the LoF entries. Here is an extract from the \chapter command in book.cls:

\addtocontents{lof}{\protect\addvspace{10\p@}}%
\addtocontents{lot}{\protect\addvspace{10\p@}}%

that adds 10pt.

share|improve this answer
add comment

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.