15

For chapters that end with a mostly empty page, I'd like to insert some form of ornament. The ornament should not be inserted if there is not enough room in the page. Is there some way of testing available vertical space?

(I don't need any help for inserting the ornament per se, only with testing the availability of vertical space in the current page. What I want to avoid is the ornament ending up all by itself on a useless otherwise blank page.)

2 Answers2

7

Since TeX does not understand chapters or sections we can only carry a test when typesetting the next chapter heading i.e., when we type \chapter.

So before we typeset the new chapter heading we examine the remaining space of the page. If there is enough space left on the page we introduce an ornament otherwise we just print the chapter heading.

\documentclass{book}
\usepackage{lipsum}
\usepackage{fourier}
\textheight380pt
\def\PrintPageParameters{\noindent\hskip5cm\vbox{\tt
  \noindent vsize: \the\vsize;\\
  \noindent pagetotal: \the\pagetotal; \\
  pagegoal: \the\pagegoal\\ }%
}
\def\ornament{\vspace{10pt}\hfill\aldineright\aldineleft\hfill\hfill}
\def\Chapter#1{%
  \ifdim\pagetotal<350pt \PrintPageParameters\\ \ornament\chapter{#1}  
    \else    
    \chapter{#1} 
  \fi
}
\begin{document}
\chapter{Introduction}
\lipsum[1-4]
\Chapter{After Ornaments}
\end{document}

This will not work for the last chapter of the Book, but perhaps at that point you might want a different type of ornament.

I kept the macro '\Chapter as simple as possible in order to demonstrate the technique. One would need to extend it to cater for the starred version of the command.

David Carlisle
  • 757,742
yannisl
  • 117,160
  • 1
    there are problems in your code for limit cases. You see it if you change to \lipsum[1-6], but there are other problems if the page is nearly filled-up but not quite (I believe 350pt is too much, it works better with 330pt). For better portability, you should probably be testing if \dimexpr\pagegoal-\pagetotal\relax is >50pt rather than if \pagetotal is < 330pt as it would work with arbitrary \textheight. And 50pt should probably be replaced by the measured height of the ornamental stuff. – Philippe Goutet May 11 '11 at 08:00
  • @Philippe Thanks for the comments. I originally had the code with a calculation based on the remaining space, but decided to hard code the values to keep the code more understandable and yes one should integrate the height of the ornament or image in the calculation. However, the main idea I wanted to convey was that the only marker that is available is the next chapter command that the user types. I will edit the code later to add your suggestion as a separate block. – yannisl May 11 '11 at 13:13
4

Inspired by the code in memoir.cls and wrote this small command :

\newcommand{\ifenoughspace}[3]{%
  \@tempdimc\pagegoal \advance\@tempdimc-\pagetotal%
  \ifdim #1>\@tempdimc #3 \else #2\fi}

Usage: \ifenoughspace{length}{what to do if enough space}{what to do otherwise}