fpc/docs/packages/fancyhdr/fancyhdr.tex
2000-02-28 08:49:09 +00:00

1763 lines
66 KiB
TeX

\documentclass[a4paper]{article}
\usepackage{multicol}
\usepackage{float}
\usepackage{makeidx}
\usepackage{layout}
\usepackage{array}
\usepackage{a4wide}
\usepackage{boxedminipage}
%%\usepackage{fancyheadings}
%%\pagestyle{fancy}
%%\lhead{\rightmark}
%%\rhead{\thepage}
%%\cfoot{}
\makeindex
\title{Page layout in \LaTeX}
\author{Piet van Oostrum\thanks{A considerable part of this article was
written by George
Gr\"atzer (University of Manitoba) in \emph{Notices Amer. Math. Soc.}
Thanks, George!}\\
Dept.\ of Computer Science\\
Utrecht University}
\def\latex/{\protect\LaTeX{}}
\def\tex/{\TeX}
\def\ams/{\protect\pAmS}
\def\pAmS{{\the\textfont2
A\kern-.1667em\lower.5ex\hbox{M}\kern-.125emS}}
\def\amslatex/{\ams/-\latex/}
\newcommand{\PSNFSS}{{\sf
PSNFSS}}
\renewcommand{\bs}{\symbol{'134}}
\newcommand{\Cmd}[1]{\texttt{\def\{{\char`\{}\def\}{\char`\}}\bs#1}}
\newcommand{\CmdIndex}[1]{\index{#1@\texttt{\bs#1}}}
\newcommand{\TTindex}[1]{\index{#1@\texttt{#1}}}
\newcommand{\PSindex}[1]{\index{page style!#1@\texttt{#1}}}
%\floatstyle{ruled}
\restylefloat{figure}
\renewcommand{\topfraction}{0.9}
\renewcommand{\bottomfraction}{0.9}
\renewcommand{\textfraction}{0.05}
\setlength{\parindent}{0pt}
\setlength{\parskip}{1ex}
\setlength{\emergencystretch}{4em}
\makeatletter
\renewcommand\l@section {\@dottedtocline{1}{1.5em}{2.3em}}
\makeatother
\newenvironment{block}{\vspace{8pt}\begin{minipage}{\textwidth}}{\end{minipage}\vspace{8pt}}
\newenvironment{fblock}{\vspace{8pt}\begin{boxedminipage}{\textwidth}}{\end{boxedminipage}\vspace{8pt}}
\begin{document}
\maketitle
\begin{abstract}
This article describes how to customize the page layout of your LaTeX
documents, i.e how to change page margings and sizes,
headers and footers, and the
proper placement of figures and tables (collectively called floats) on
the page.
Originally this was the documentation of the \textsf{fancyheadings}
package. It did contain also other info, e.g. advanced use of marks.
It has now been upgraded to include more, e.g. the handling of floats.
The fancyheadings documentation has been upgraded to conform to version 2
of this package\footnote{this version is due to be released Real Soon Now}. For reasons of compatibility with certain operating systems, the
name of the package has been changed to \textsf{fancyhdr}.
Although this paper uses \LaTeXe{} commands, most of the techniques can
be used with older \LaTeX versions with appropriate changes.
\end{abstract}
\tableofcontents
\section{Introduction}
\label{sec:intro}
A page in a \LaTeX{} document is built from various elements as shown in
figure \ref{fig:layout}.
\begin{figure}[htbp]
\begin{center}
\leavevmode
\layout
\vspace{3cm}
\caption{Page elements. The values shown are those in effect in the current document, not the defaults.}
\label{fig:layout}
\end{center}
\end{figure}
%\thispagestyle{fancy}
\thispagestyle{plain}
The body contains the main text of the document
together with the so called floats (tables and figures).
The pages are constructed by \LaTeX's output routine, which is quite
complicated and should therefore not be modified. Some of the packages
described in this paper contains small modifications to the output routine
to accomplish things that cannot be done in another way. You should use
these packages to get the desired result rather than fiddling with the
output routine yourself.
There a a number of things that you must be aware of:
\begin{enumerate}
\item The margins on the left are not called \Cmd{leftmargin}, but
\Cmd{evensidemargin} (on even-numbered pages) and \Cmd{oddsidemargin}
(on odd-numbered pages). In one-sided documents
\Cmd{oddsidemargin} is used for either. \Cmd{leftmargin} is also a valid
\latex/ parameter but it has a different use (namely the indentation of
lists).
\item Most of the parameters should not be changed in the middle of a
document. Some changes might work at a pagebreak. If you want to change
the height of a single page, you can use the \Cmd{enlargethispage}
command.
\end{enumerate}
The margin notes
area contains small pieces of information created by the \Cmd{marginpar}
command. On twosided documents the margin notes appear on the left and right
alternatively. The margin notes are not on fixed places with respect to the
paper but at approximately the same height as the paragraph in which they
appear. Due to the algorithm used to decide the placement of margin notes,
in a twosided document unfortunately
they may appear on the wrong side if they are close
to a page break.
If you want to put information on fixed places in the margins you may use
the technique described in sections \ref{sec:movie} and \ref{sec:thumb}.
The first part of this paper describes how to change the header and footer
areas. The last part describes how to get your floats at the desired place.
\section{Page headers and footers}
The page headers and footers in \LaTeX{} are defined by the
\Cmd{pagestyle} and \Cmd{pagenumbering} commands. \Cmd{pagestyle}
defines the general contents of the headers and footers (e.g. where the
page number will be printed), while
\Cmd{pagenumbering} defines the format of the page number.
\LaTeX{} has four standard pagestyles:
\begin{tabular}{>{\tt}lp{10cm}}
empty & no headers or footers \\
plain & no header, footer contains page number centered \\
headings & no footer, header contains name of chapter/section and/or
subsection and page number \\
myheadings & no footer, header contains page number and user supplied information
\end{tabular}
Although these are useful styles, they are quite limited. Additional page
styles can be defined by defining commands of the form \Cmd{ps@xxx}. This
command is executed when a \Cmd{pagestyle\{xxx\}} is given in the document.
The \Cmd{ps@xxx} command should define the following
commands for the contents of the headers and footers:
\noindent
\begin{tabular}{lp{10cm}}
\Cmd{@oddhead} & header on odd numbered pages in two-sided documents
(on all pages in one-sided) \\
\Cmd{@evenhead} & header on even numbered pages in two-sided documents \\
\Cmd{@oddfoot} & footer on odd numbered pages in two-sided documents
(on all pages in one-sided) \\
\Cmd{@evenfoot} & footer on even numbered pages in two-sided documents \\
\end{tabular}
These are not user commands, but rather ``variables'' that are used by
\latex/'s output routine.
As the command names contain the character '\texttt{@}', they should
be defined in a package file, or otherwise be sandwiched between the commands
\Cmd{makeatletter} and \Cmd{makeatother}.
The \Cmd{pagenumbering} command defines the layout of the page number. It
has a parameter from the following list:
\begin{tabular}{>{\tt}ll}
arabic & arabic numerals \\
roman & lower case roman numerals \\
Roman & upper case roman numerals \\
alph & lower case letter \\
Alph & upper case letter
\end{tabular}
The \Cmd{pagenumbering\{xxx\}} defines the command \Cmd{thepage} to be the
expansion of the page number in the given notation \texttt{xxx}.
The pagestyle command
then would include \Cmd{thepage} in the appropriate place. Additionally
the \Cmd{pagenumbering} command resets the page number to~1.
The \Cmd{pagestyle} and \Cmd{pagenumbering} apply to the page that is
being constructed, so they should be used at a location where it is clear
to what page they apply (see section \ref{sec:change}).
\section{What is \textsf{fancyhdr}}
The \textsf{fancyhdr} macro package allows you to customize
in \latex/ your page headers and footers in an easy way. You can
define:
\begin{itemize}
\item three-part headers and footers
\item decorative lines in headers and footers
\item headers and footers wider than the width of the text
\item multi-line headers and footers
\item separate headers and footers for even and odd pages
\item different headers and footers for chapter pages
\item different headers and footer on pages with floats
\end{itemize}
Of course, you also have complete control over fonts, uppercase
and lowercase displays, etc.
%%% \section{Where to get it?}\label{get}
%%% You only need the file \verb|fancyhdr.sty| which you can find at the
%%% \index{ftp}
%%% CTAN sites: \verb|ftp.shsu.edu| (U.S.), \verb|ftp.tex.ac.uk| (U.K.), and
%%% \verb|ftp.dante.de| (Germany).
%%% You will find it in the directory\\
%%% \verb|/tex-archive/macros/latex209/contrib/fancyhdr|. Although
%%% \textsf{fancyhdr}
%%% is a \latex/ 2.09 style file, it will work with \LaTeXe{}. Also this
%%% \TTindex{fixmarks.sty}
%%% \TTindex{extramarks.sty}
%%% article and the two packages \texttt{fixmarks} and \texttt{extramarks} can
%%% be found there. (The code for these packages given in this article is a
%%% simplified version.)
\section{Simple use of \textsf{fancyhdr}} To use this package in a
\LaTeXe\ document, place the file \verb|fancyhdr.sty| in a
directory/folder where \tex/ can find it (normally in the input
directory/folder), and include in the preamble of your document
after
\begin{verbatim}
\documentclass{...}
\end{verbatim}
the commands\footnote{For LaTeX2.09 you should specify \texttt{[fancyhdr]}
in the \Cmd{documentstyle} instead of the \Cmd{usepackage}
command.}:
\PSindex{fancy}
\begin{verbatim}
\usepackage{fancyhdr}
\pagestyle{fancy}
\end{verbatim}
We can visualize the page layout we can create with \textsf{fancyhdr}
as follows:
\begin{fblock}
\noindent\makebox[\textwidth]{LeftHeader\hfill
CenteredHeader\hfill RightHeader}
\noindent\makebox[\textwidth]{\hrulefill}\\[\baselineskip]
\noindent\makebox[\textwidth]{\hfill page body\hfill}\\[\baselineskip]
\noindent\makebox[\textwidth]{\hrulefill}
\noindent\makebox[\textwidth]{LeftFooter\hfill
CenteredFooter\hfill RightFooter}
\end{fblock}
The LeftHeader and LeftFooter are left justified; the
CenteredHeader and CenteredFooter are centered; the
RightHeader and RightFooter are right justified.
We define each of the six ``fields'' and the two decorative lines
separately.
\section{A simple example} K. Grant is writing a report to Dean
A. Smith, on ``The performance of new graduates'' with the
following page layout:
\begin{fblock}
\noindent\makebox[\textwidth]{\hfill\bfseries The performance of new
graduates}
\noindent\makebox[\textwidth]{\hrulefill}\\[\baselineskip]
\noindent\makebox[\textwidth]{\hfill page body\hfill}\\[\baselineskip]
\noindent\makebox[\textwidth]{\hrulefill}
\noindent\makebox[\textwidth]{From: K. Grant\phantom{3}\hfill
To: Dean A. Smith\hfill \phantom{From: K. Grant}3}
\end{fblock}
\noindent where ``3'' is the page number. The title: ``The
performance of new graduates'' is bold.
This is accomplished by these commands following \Cmd{pagestyle\{fancy\}}
\footnote{Note that version 1 of fancyheadings used the
\Cmd{setlength} command to change the \texttt{\bs...rulewidth} parameters.}:
\CmdIndex{lhead}
\CmdIndex{rhead}
\CmdIndex{chead}
\CmdIndex{lfoot}
\CmdIndex{rfoot}
\CmdIndex{cfoot}
\CmdIndex{headrulewidth}
\CmdIndex{footrulewidth}
\begin{verbatim}
\lhead{}
\chead{}
\rhead{\bfseries The performance of new graduates}
\lfoot{From: K. Grant}
\cfoot{To: Dean A. Smith}
\rfoot{\thepage}
\renewcommand{\headrulewidth}{0.4pt}
\renewcommand{\footrulewidth}{0.4pt}
\end{verbatim}
(The \Cmd{thepage} macro displays the current page number.
\Cmd{bfseries} is the \LaTeXe's way of selecting bold face.)
This is now fine, except that the first page does not need
all these headers and footers. To eliminate all but the
centered page number, issue the command
\CmdIndex{thispagestyle}
\begin{verbatim}
\thispagestyle{plain}
\end{verbatim}
after the
\Cmd{begin\{document\}}
and the
\CmdIndex{maketitle}
\Cmd{maketitle}
commands.
Alternatively, issue
\begin{verbatim}
\thispagestyle{empty}
\end{verbatim}
if you do not want any headers or footers.
In fact the standard \latex/ classes have the command \Cmd{maketitle}
defined in such a way that a \Cmd{thispagestyle\{plain\}} is automatically
issued. So if you \emph{do} want the fancy layout on a page containing
\Cmd{maketitle} you must issue a \Cmd{thispagestyle\{fancy\}} after the
\Cmd{maketitle}.
\section{An example of two-sided printing}\label{two-sided}
\TTindex{twoside}
Some document classes, such as \verb|book.cls|, print two-sided by default: the even
pages and the odd pages have different layouts; other document classes
use the \verb|twoside| option to print two-sided.
Now let us print the report two-sided. Let the above page
layout be used for the odd (right-side) pages, and the
following for the even (left-side) pages:
\begin{fblock}
\noindent\makebox[\textwidth]{\bfseries The performance of new
graduates\hfill}
\noindent\makebox[\textwidth]{\hrulefill}\\[\baselineskip]
\noindent\makebox[\textwidth]{\hfill page body\hfill}\\[\baselineskip]
\noindent\makebox[\textwidth]{\hrulefill}
\noindent\makebox[\textwidth]{4\phantom{To: Dean A. Smith}\hfill
From: K. Grant\hfill \phantom{4}To: Dean A. Smith}
\end{fblock}
\noindent where ``4'' is the page number.
Here are the commands:
\begin{verbatim}
\fancyhead{} % clear all fields
\fancyhead[RO,LE]{\bfseries The performance of new graduates}
\fancyfoot[LE,RO]{\thepage}
\fancyfoot[LO,CE]{From: K. Grant}
\fancyfoot[CO,RE]{To: Dean A. Smith}
\renewcommand{\headrulewidth}{0.4pt}
\renewcommand{\footrulewidth}{0.4pt}
\end{verbatim}
\CmdIndex{fancyhead}
\CmdIndex{fancyfoot}
We use the more general commands \Cmd{fancyhead} and \Cmd{fancyfoot}.
These have an additional parameter between square brackets that specifies
for which pages and/or parts of the header/footer they apply. The first
\Cmd{fancyhead} command omits this parameter, and thus applies to all
header fields. In general this is only useful to get rid of the defaults or
a previous definition, as is done here. The selectors that can be used
between the square brackets are given in figure~\ref{fig:sel}. Selectors can be combined so
\Cmd{fancyhead[LE,RO]\{text\}}
will define the field for both the left header on even pages and the right
header on odd pages. If you don't give an E or O the definition applies to
both. Similar for LRC. So the use of \Cmd{lhead} in the previous section
is just an abbreviation for \Cmd{fancyhead[L]}.
The selectors may be given as uppercase or lowercase letters.
\CmdIndex{fancyhf}
There is also a more general command \Cmd{fancyhf} that you can use to
combine the specifications for headers and footers. This allows additional
selectors H (header) and F (footer). In fact \Cmd{fancyhead} and
\Cmd{fancyfoot} are just \Cmd{fancyhf} with H and F prespecified.
\begin{figure}[tb]
\begin{center}
\leavevmode
\begin{tabular}{|l|l|}
\hline
E & Even page \\
O & Odd page \\
\hline
L & Left field \\
C & Center field \\
R & Right field \\
\hline
H & Header \\
F & Footer \\
\hline
%% T & float at Top \\
%% B & float at Bottom \\
%% F & Float page \\
%% \hline
\end{tabular}
\end{center}
\caption{Selectors}
\label{fig:sel}
\end{figure}
Again, you may
use \Cmd{thispagestyle\{plain\}} for a simple page layout for
page~1.
\section{Redefining \texttt{plain} style}
Some \latex/ commands, like \Cmd{chapter}, use the \Cmd{thispagestyle}
command to automatically switch to the \texttt{plain} page style, thus
ignoring the page style currently in effect.
To customize even such pages you must redefine the \texttt{plain}
pagestyle. As we indicated before you could do this by defining the
\Cmd{ps@plain} command, but \textsf{fancyhdr} gives you an easier way
with the \Cmd{fancypagestyle} command. This command can be used to
redefine existing pagestyles (like \texttt{plain}) or to define new ones,
e.g. if part of your document is to use a different pagestyle. This command
has two parameters: one is the name of the pagestyle to be defined, the
second consists of commands that change the headers and/or footers, i.e.
\verb|fancyhead| etc. Also allowed are changes to \Cmd{headrulewidth} and
\Cmd{footrulewidth}.
As an example, let us
redefine the \texttt{plain} style for the report in Section~\ref{two-sided} by
making the page number bold.
\PSindex{plain}
\begin{verbatim}
\fancypagestyle{plain}{%
\fancyhf{} % clear all header and footer fields
\fancyfoot[C]{\bfseries \thepage} % except the center
\renewcommand{\headrulewidth}{0pt}
\renewcommand{\footrulewidth}{0pt}}
\end{verbatim}
\section{The default layout}\label{default}
Let us use the \verb|book.cls| documentclass and the default settings for
\textsf{fancyhdr}; so we only issue the commands
\begin{verbatim}
\usepackage{fancyhdr}
\pagestyle{fancy}
\end{verbatim}
and let \textsf{fancyhdr} take care of everything. On the
pages where new chapters start, we get a centered page number in
the footer; there is no header, and there are no decorative lines.
On an even page, we get the layout:
\begin{fblock}
\noindent\makebox[\textwidth]{\sl 1.2 EVALUATION\hfill
CHAPTER 1. INTRODUCTION}
\noindent\makebox[\textwidth]{\hrulefill}\\[\baselineskip]
\noindent\makebox[\textwidth]{\hfill page body\hfill}\\[\baselineskip]
\noindent\makebox[\textwidth]{\hrulefill}
\noindent\makebox[\textwidth]{\hfill4\hfill}
\end{fblock}
On an odd page, we get the layout:
\begin{fblock}
\noindent\makebox[\textwidth]{\sl CHAPTER 1. INTRODUCTION\hfill
1.2 EVALUATION}
\noindent\makebox[\textwidth]{\hrulefill}\\[\baselineskip]
\noindent\makebox[\textwidth]{\hfill page body\hfill}\\[\baselineskip]
\noindent\makebox[\textwidth]{\hrulefill}
\noindent\makebox[\textwidth]{\hfill
3\hfill }
\end{fblock}
\noindent where the header text is slanted uppercase.
This default layout is produced by the following commands:
\CmdIndex{rightmark}
\CmdIndex{leftmark}
\begin{verbatim}
\fancyhead[LE,RO]{\slshape \rightmark}
\fancyhead[LO,RE]{\slshape \leftmark}
\fancyfoot[C]{\thepage}
\end{verbatim}
The following settings are used for the decorative lines:
\begin{tabbing}
\CmdIndex{headrulewidth}
\noindent \Cmd{headrulewidth}\qquad \qquad \qquad \=0.4\=pt\\
\CmdIndex{footrulewidth}
\Cmd{footrulewidth}\>0\>pt
\end{tabbing}
The header text is turned into all uppercase in \verb|book.cls|.
\section{The scoop on \latex/'s marks}\label{sec:custom}
Usually, for documents of class \verb|book| and \verb|report|, you may
want to use chapter and section information in the headings (chapter only
for one-sided printing), and for documents of class \verb|article|,
section and subsection information (section only for one-sided
printing). \latex/ uses a marker mechanism to remember the chapter and
section (section and subsection) information for a page; this is
discussed in detail in the
\latex/ \emph{Companion}, Section 4.3.1.
There are two ways you can use and change the higher- and lower-level
sectioning information available to you. The macros:
\CmdIndex{rightmark}
\CmdIndex{leftmark}
\Cmd{leftmark}
(higher-level) and \Cmd{rightmark} (lower-level) contain the information
processed by \latex/, and you can use them directly as shown in
Section~\ref{default}.
The \Cmd{leftmark} contains the Left argument of the \emph{Last}
\Cmd{markboth} on the page, the \Cmd{rightmark} contains the Right
argument of the \emph{fiRst} \Cmd{markboth} or the only argument of the
\emph{fiRst} \Cmd{markright} on the page. If no marks are present on a
page they are ``inherited'' from the previous page.
You can influence how chapter, section, and subsection
information (only two of them!) is displayed by redefining the
\CmdIndex{chaptermark}
\Cmd{chaptermark},
\CmdIndex{sectionmark}
\CmdIndex{subsectionmark}
\Cmd{sectionmark}, and \Cmd{subsectionmark} commands\footnote{There are
similar commands for \texttt{paragraph} and \texttt{subparagraph} but
they are seldom used.}. You must put the redefinition
after the first call of \Cmd{pagestyle\{fancy\}} as this
sets up the defaults.
Let us illustrate this with chapter info. It is made up of three parts:
\begin{itemize}
\CmdIndex{thechapter}
\item the number (say, 2), displayed by the macro \Cmd{thechapter}
\item the name (in English, Chapter), displayed by the macro
\CmdIndex{chaptername}
\Cmd{chaptername}
\item the title, contained in the argument of
\Cmd{chaptermark}.
\end{itemize}
Figure~\ref{fig:markers} shows some variants for ``Chapter 2.\ Do it now''
(the last example is appropriate in some non-English languages). The \%
signs at the end of the lines are to prevent unwanted space. Normally you
would continue the lines and remove these \% signs\footnote{the \texttt{\bs
MakeUppercase} command is used in \LaTeXe{} to generate uppercase text,
while in \LaTeX{} 2.09 \Cmd{uppercase} is used. The difference is
that \Cmd{MakeUppercase} also deals with non-ASCII letters.
Fancyhdr defines \Cmd{MakeUppercase} to be an alias for
\Cmd{uppercase} if it isn't defined.}.
\begin{figure}[tb]
\CmdIndex{chaptermark}
\CmdIndex{uppercase}
\CmdIndex{MakeUppercase}
\setlength{\columnsep}{20pt}\small
\begin{multicols}{2}
\noindent Code:\\
\mbox{}\\
\verb|\renewcommand{\chaptermark}[1]{%|\\
\verb| \markboth{\chaptername|\\
\verb| \ \thechapter.\ #1}{}}|\\
\mbox{}\\
\verb|\renewcommand{\chaptermark}[1]{%|\\
\verb| \markboth{\MakeUppercase{%|\\
\verb| \chaptername}\ \thechapter.%|\\
\verb| \ #1}{}}|\\
\mbox{}\\
\verb|\renewcommand{\chaptermark}[1]{%|\\
\verb| \markboth{\MakeUppercase{%|\\
\verb| \chaptername\ \thechapter.%|\\
\verb| \ #1}}{}}|\\
\mbox{}\\
\verb|\renewcommand{\chaptermark}[1]{%|\\
\verb| \markboth{#1}{}}|\\
\mbox{}\\
\verb|\renewcommand{\chaptermark}[1]{%|\\
\verb| \markboth{\thechapter.\ #1}{}}|\\
\mbox{}\\
\verb|\renewcommand{\chaptermark}[1]{%|\\
\verb| \markboth{\thechapter.%|\\
\verb| \ \chaptername.\ #1}{}}|\\
Prints:\\
\mbox{}\\
Chapter 2.\ Do it now\\
\mbox{}\\
\mbox{}\\
\mbox{}\\
CHAPTER 2.\ Do it now\\
\mbox{}\\
\mbox{}\\
\mbox{}\\
\mbox{}\\
CHAPTER 2.\ DO IT NOW\\
\mbox{}\\
\mbox{}\\
\mbox{}\\
\mbox{}\\
Do it now\\
\mbox{}\\
\mbox{}\\
2.\ Do it now\\
\mbox{}\\
\mbox{}\\
2.\ Chapter.\ Do it now\\
\mbox{}\\
\end{multicols}
\caption{Marker variants}\label{fig:markers}
\end{figure}
For the lower-level sectioning information, do the same with
\Cmd{markright}.
So if ``Section 2.2.\ First steps'' is the current section,
then
\begin{verbatim}
\renewcommand{\sectionmark}[1]{\markright{\thesection.\ #1}}
\end{verbatim}
will give
``2.2.\ First steps''
Redefining the \Cmd{chaptermark} and \Cmd{sectionmark} commands may
not eliminate all uppercaseness. E.g.\ the bibliography will have a title
\CmdIndex{uppercase}
\index{BIBLIOGRAPHY}
of \textsc{bibliography} in the header, as the \Cmd{MakeUppercase} is
explicitly given in the definition of \Cmd{thebibliography}. Similar for
\index{INDEX}
\textsc{index} etc.\ If you don't want to redefine these commands, you can
use the \Cmd{nouppercase} command that \textsf{fancyhdr} makes available in the header
and footer fields. Note that this may screw other things, like uppercase
roman numerals in your headers, so it should be used with care. Essentially
this command typesets its argument in an environment where
\Cmd{MakeUppercase} and \Cmd{uppercase} are changed into do-nothing operations.
\begin{verbatim}
\lhead{\nouppercase{\rightmark}}
\rhead{\nouppercase{\leftmark}}
\end{verbatim}
It should be noted that the \latex/ marking mechanism works fine with
chapters (which always start on a new page) and sections (which are
reasonably long). It does not work quite as well with short sections and
subsections. This is a problem with \latex/, not with
\textsf{fancyhdr}.
As an example let's take a page layout where the leftmarks are generated by
the sections and the rightmarks by the subsections (as is default in the
\texttt{article} class). Take a page with some short sections, e.g.
\begin{samepage}
\noindent Section 1.\\
subsection 1.1\\
subsection 1.2\\
Section 2.
\end{samepage}
As the leftmark contains the \emph{last} mark of the page it will be
``Section 2.'', and the rightmark will be ``subsection 1.1'' as it will be
the \emph{first} mark of the page. So the page header info will combine
section 2 with subsection 1.1 which isn't very nice.
The best you can do in these cases is use only the \Cmd{rightmark}s and
redefine \Cmd{sectionmark} accordingly. A \latex/ command
\CmdIndex{firstleftmark}
\Cmd{firstleftmark} would also be a nice addition (see the
\texttt{extramarks} package in section~\ref{sec:xmarks}).
Another problem with the marks in the standard \latex/ classes is that the
higher level sectioning commands (e.g. \Cmd{chapter}) call
\Cmd{markboth} with an empty right argument. This means that on the first
page of a chapter (or a section in article style) the \Cmd{rightmark}
will be empty. If this is a problem you must manually insert extra
\Cmd{markright} commands or redefine the \Cmd{chaptermark}
(\Cmd{sectionmark}) commands to issue a \Cmd{markboth} command with
two decent parameters.
As a final remark you should also note that the \verb|*| forms of the
\Cmd{chapter} etc.\ commands do \emph{not} call the mark commands. So if
you want your preface to set the header info but not be numbered nor be put in
the table of contents, you must issue the \Cmd{markboth} command
yourself, e.g.
\begin{verbatim}
\chapter*{Preface\markboth{Preface}{}}
\end{verbatim}
Entering the \Cmd{markboth} command inside the \Cmd{chapter*} insures
that the mark will not be separated from the title by a page break. Of
course with \Cmd{chapter*} this wouldn't be a problem if you put the mark
command after the chapter title, as the \Cmd{chapter*} command starts at a
new page. However with a \Cmd{section*} it could be dangerous to say:
\begin{verbatim}
\section*{Preface}
\markboth{Preface}{}
\end{verbatim}
as a page break may occur between the two commands.
\section{Dictionary style headers}
\index{dictionary}
\index{concordance}
Dictionaries and concordances usually have a header containing the first
word defined on the page or both the first and the last words. This can
easily be accomplished with \textsf{fancyhdr} and \latex/'s
\texttt{mark} mechanism. Of course if you use the marks for dictionary
style headers, you cannot use them for chapter and section information, so
if there are also chapters and sections present, you must redefine the
\Cmd{chaptermark} and \Cmd{sectionmark} to make them harmless:
\begin{verbatim}
\renewcommand{\chaptermark}[1]{}
\renewcommand{\sectionmark}[1]{}
\end{verbatim}
Now you do a \Cmd{markboth\{\#1\}\{\#1\}} for each dictionary or concordance
entry \verb|#1| and use \Cmd{rightmark} for the first entry defined on
the page and \Cmd{leftmark} for the last one.
If you want to use a header entry of the form \textsf{firstword--lastword}
it would be nice if this would be reduced to just the form
\textsf{firstword} if both are the same. This could happen if there is just
one entry on the page. In this case a test must be made to check if the
marks are the same. However, \tex/'s marks are strange beasts, which
cannot be compared out of the box with the plain \tex/ \Cmd{if} commands.
\TTindex{ifthen}
Fortunately the \texttt{ifthen} package works well:
\begin{verbatim}
\newcommand{\mymarks}{
\ifthenelse{\equal{\leftmark}{\rightmark}}
{\rightmark} % if equal
{\rightmark--\leftmark}} % if not equal
\fancyhead[LE,RO]{\mymarks}
\fancyhead[LO,RE]{\thepage}
\end{verbatim}
Dictionaries are often done with two columns. Unfortunately there is a bug
in \latex/'s twocolumn option which causes some marks to be lost. If you
use the package in figure~\ref{fig:fixmarks}, this will be
solved\footnote{This is a simplification of the actual code. Get the real
package from the CTAN sites.}\footnote{The
\texttt{multicol} package uses a similar technique.}.
\begin{figure}[tb]
\small
\TTindex{fixmarks.sty}
\begin{verbatim}
% fixmarks.sty:
% Patch LaTeX's output routine to handle marks correctly with two columns.
% Joe Pallas <pallas@edu.stanford.neon>
% Corrected by Piet van Oostrum <piet@cs.ruu.nl> on Feb 5, 1993, Oct 5, 1994
\def\@outputdblcol{\if@firstcolumn \global\@firstcolumnfalse
% Remember the marks from the first column
\global\setbox\@leftcolumn\copy\@outputbox
\splitmaxdepth=\maxdimen \cbaddness=10000
\setbox\@outputbox\vsplit\@outputbox to\maxdimen
\xdef\@firstcoltopmark{\topmark}%
\xdef\@firstcolfirstmark{\splitfirstmark}%
\ifx\@firstcolfirstmark\empty\global\let\@setmarks\relax\else
\gdef\@setmarks{\let\firstmark\@firstcolfirstmark
\let\topmark\@firstcoltopmark}%
\fi
% End of change
\else \global\@firstcolumntrue
\setbox\@outputbox\vbox{\hbox to\textwidth{\hbox to\columnwidth
{\box\@leftcolumn \hss}\hfil \vrule width\columnseprule\hfil
\hbox to\columnwidth{\box\@outputbox \hss}}}\@combinedblfloats
% Override current first and top with those of first column if necessary
\@setmarks
% End of change
\@outputpage \begingroup \@dblfloatplacement \@startdblcolumn
\@whilesw\if@fcolmade \fi{\@outputpage\@startdblcolumn}\endgroup
\fi}
\end{verbatim}
\caption{Fix marks in twocolumn style}
\label{fig:fixmarks}
\end{figure}
\section{Fancy layouts}
\index{multi-line}
You can make a multi-line field with the \Cmd{\bs} command. It is also
possible to put extra space in a field with the \Cmd{vspace} command.
Note that if you do this you will probably have to increase the height of
\CmdIndex{headheight}
\CmdIndex{footskip}
the header (\Cmd{headheight}) and/or of the footer (\Cmd{footskip}),
\index{Overfull \verb+\vbox+ \ldots}
otherwise you may get error messages ``Overfull \Cmd{vbox} \ldots has
occurred while \Cmd{output} is active''%
\footnote{If you use \texttt{11pt} or \texttt{12pt} you will probably also
have to do this, because \LaTeX's defaults are quite small}.
See Section 4.1 of the \latex/ \emph{Companion} for detail.
For instance, the following code will place the section title and the
subsection title of an article in two lines in the upper right hand
corner:
\begin{verbatim}
\documentclass{article}
\usepackage{fancyhdr}
\pagestyle{fancy}
\addtolength{\headheight}{\baselineskip}
\renewcommand{\sectionmark}[1]{\markboth{#1}{}}
\renewcommand{\subsectionmark}[1]{\markright{#1}}
\rhead{\leftmark\\\rightmark}
\end{verbatim}
You can also customize the decorative lines. You can make the decorative
line in the header quite thick with
\CmdIndex{headrulewidth}
\begin{verbatim}
\renewcommand{\headrulewidth}{0.6pt}
\end{verbatim}
or you can make the decorative line in the footer disappear with
\CmdIndex{footrulewidth}
\begin{verbatim}
\renewcommand{\footrulewidth}{0pt}
\end{verbatim}
The decorative lines, themselves, are defined in the two macros
\Cmd{headrule} and \Cmd{footrule}. For instance,
if you want a dotted line rather than a solid line in the header,
redefine the command \Cmd{headrule}:
\begin{verbatim}
\renewcommand{\headrule}{\vbox to 0pt{\hbox
to\headwidth{\dotfill}\vss}}
\end{verbatim}
\CmdIndex{footruleskip}
There is one additional parameter that you can set: \Cmd{footruleskip}. It
defines the distance between the decorative line in the footer and the top
of the footer text line. By default it is set to 30\% of the normal line
distance. You may want to adjust it if you use unusally large or small
fonts in the footer. Change it with \Cmd{renewcommand}.
\section{Two book examples}
The following definitions give an approximation of the style
used in L. Lamport's \latex/ book.
Lamport's header overhangs the outside margin. This is done as follows.
The width of headers and footers is \Cmd{headwidth}, which by default
equals the width of the text: \Cmd{textwidth}. You can make the width
\CmdIndex{headwidth}
wider (or narrower) by redefining \Cmd{headwidth} with the
\Cmd{setlength} and \Cmd{addtolength} commands.
To overhang the outside margin where the marginal notes are
\CmdIndex{marginparsep}
\CmdIndex{marginparwidth}
printed, add both \Cmd{marginparsep} and \Cmd{marginparwidth} to
\Cmd{headwidth} with the commands:
\begin{verbatim}
\addtolength{\headwidth}{\marginparsep}
\addtolength{\headwidth}{\marginparwidth}
\end{verbatim}
\begin{flushleft}
You must issue these commands \emph{after} the first
\Cmd{pagestyle\{fancy\}} command as this
will establish the default for \Cmd{headwidth}\footnote{In version 2 of
\textsf{fancyhdr} it is usually safe to do it before, but it is still
advised to do after.}.
\end{flushleft}
And now a complete definition of Lamport's book style:
\begin{verbatim}
\documentclass{book}
\usepackage{fancyhdr}
\pagestyle{fancy}
\addtolength{\headwidth}{\marginparsep}
\addtolength{\headwidth}{\marginparwidth}
\renewcommand{\chaptermark}[1]{\markboth{#1}{}}
\renewcommand{\sectionmark}[1]{\markright{\thesection\ #1}}
\fancyhf{}
\fancyhead[LE,RO]{\bfseries\thepage}
\fancyhead[LO]{\bfseries\rightmark}
\fancyhead[RE]{\bfseries\leftmark}
\fancypagestyle{plain}{%
\fancyhead{} % get rid of headers
\renewcommand{\headrulewidth}{0pt} % and the line
}
\end{verbatim}
\PSindex{plain}
Notice that the \Cmd{chaptermark} and \Cmd{sectionmark} commands have
been redefined to eliminate the chapter numbers and the uppercaseness.
For the second example, we take the \amslatex/ book.% \cite{gG93B}.
% George Gratzer, Math into LaTeX, An Introduction to LaTeX and AMS-LaTeX,
% Birkhauser Boston, ISBN 0-8176-3805-9
Chapter pages have no headers or footers. So we declare
\begin{verbatim}
\thispagestyle{empty}
\end{verbatim}
for every chapter page, and we do not need to redefine plain.
Chapter and section titles appear in the form: 2.\ DO IT NOW, so we have
to redefine \Cmd{chaptermark} and \Cmd{sectionmark} as follows (see
Section~\ref{sec:custom}):
\begin{verbatim}
\renewcommand{\chaptermark}[1]%
{\markboth{\MakeUppercase{\thechapter.\ #1}}{}}
\renewcommand{\sectionmark}[1]%
{\markright{\MakeUppercase{\thesection.\ #1}}}
\end{verbatim}
In an even-header, the page number is printed as the LeftHeader and
the chapter info as the RightHeader; in an odd-header, the section info
is printed as the LeftHeader and the page number as the RightHeader. The
CenteredHeaders are empty. There are no footers.
There is a decorative line in the header. It is 0.5pt wide, so we need
the commands:
\begin{verbatim}
\renewcommand{\headrulewidth}{0.5pt}
\renewcommand{\footrulewidth}{0pt}
\end{verbatim}
The font used in the headers is 9 pt bold Helvetica. The \PSNFSS\ system
by Sebastian Rahtz uses the short (Karl Berry) name \verb|phv| for
Helvetica, so this font is selected with the commands:
\begin{verbatim}
\fontfamily{phv}\fontseries{b}\fontsize{9}{11}\selectfont
\end{verbatim}
(See Sections 7.6.1 and 11.9.1 of the \latex/ \emph{Companion}.)
Let us define a shorthand for this:
\begin{verbatim}
\newcommand{\helv}{%
\fontfamily{phv}\fontseries{b}\fontsize{9}{11}\selectfont}
\end{verbatim}
Now we are ready for the page layout:
\begin{verbatim}
\documentclass{book}
\usepackage{fancyhdr}
\pagestyle{fancy}
\renewcommand{\chaptermark}[1]%
{\markboth{\MakeUppercase{\thechapter.\ #1}}{}}
\renewcommand{\sectionmark}[1]%
{\markright{\MakeUppercase{\thesection.\ #1}}}
\renewcommand{\headrulewidth}{0.5pt}
\renewcommand{\footrulewidth}{0pt}
\newcommand{\helv}{%
\fontfamily{phv}\fontseries{b}\fontsize{9}{11}\selectfont}
\fancyhf{}
\fancyhead[LE,RO]{\helv \thepage}
\fancyhead[LO]{\helv \rightmark}
\fancyhead[RE]{\helv \leftmark}
\end{verbatim}
\section{Special page layout for float pages}
\label{sec:float}
\index{float page}
Some people want to have a special layout for float pages (pages only
containing floats). As these pages
are generated autonomically by \latex/, the user doesn't have any control
over them. There is no \Cmd{thispagestyle} for float pages and any change
of the page style will at least also affect the page before the float page.
With \textsf{fancyhdr}, however, you can specify in each of
the header- or footer fields
\Cmd{iffloatpage\{}value for float page\verb|}{|value for other pages\verb|}|
You can even use this to get rid of the decorative line on float pages only
by defining:
\begin{verbatim}
\renewcommand{\headrulewidth}{\iffloatpage{0pt}{0.4pt}}
\end{verbatim}
Sometimes you may want to change the layout also for pages that contain a
float on the top of the page or a float on the bottom of the page.
\textsf{fancyhdr} gives you the commands \Cmd{iftopfloat} and \Cmd{ifbotfloat}
similar to
\Cmd{iffloatpage}.
Note: Marks in floats will not be visible in \latex/'s output routine, so
it is not useful to put marks in floats. So there is currently no way to
let a float (e.g.\ a figure caption) influence the page header or footer.
\section{Those blank pages}
\label{sec:blank}
In the \texttt{book} class when the \texttt{openany} option is not given or
in the \texttt{report} class when the \texttt{openright} option is given,
chapters start at odd-numbered pages, half of the time causing a blank page
to be inserted. Some people prefer this page to be completely empty, i.e.\
without headers and footers. This cannot be done with \Cmd{thispagestyle}
as this command would have to be issued on the \emph{previous} page. There
is, however, no magic necessary to get this done:
\PSindex{empty}
\CmdIndex{clearpage}
\CmdIndex{cleardoublepage}
\begin{verbatim}
\clearpage{\pagestyle{empty}\cleardoublepage}
\end{verbatim}
As the \Cmd{pagestyle\{empty\}} is enclosed in a group it only affects the
page that may be generated by the \Cmd{cleardoublepage}. You can of
course put the above in a private command. If you want to have this done
automatically at each chapter start or when you want some other text on the
page then you must redefine the \Cmd{cleardoublepage} command.
\index{blank page}
\begin{verbatim}
\makeatletter
\def\cleardoublepage{\clearpage\if@twoside \ifodd\c@page\else
\hbox{}
\vspace*{\fill}
\begin{center}
This page intentionally contains only this sentence.
\end{center}
\vspace{\fill}
\thispagestyle{empty}
\newpage
\if@twocolumn\hbox{}\newpage\fi\fi\fi}
\makeatother
\end{verbatim}
\section{\textsf{N} of \textsf{M} style page numbers}
\label{sec:nofm}
Some document writers prefer the pages to be numbered as \textsf{n} of
\textsf{m} where \textsf{m} is the number of pages in the document. There
\TTindex{nofm.sty}
is a package \texttt{nofm.sty} available, but some versions of it are
defective, and most don't work with fancyhdr because they take over the
\TTindex{lastpage.sty}
complete page layout. For \LaTeXe{} there is a package \texttt{lastpage}
available which you can use with \textsf{fancyhdr} as follows:
\begin{verbatim}
\usepackage{lastpage}
...
\cfoot{\thepage\ of \pageref{LastPage}}
\end{verbatim}
If you are still using \latex/2.09 and you are not able to switch to
\LaTeXe{} you can use the \latex/2.09 compatible \texttt{lastpage209.sty}.
which is defined as
follows:
\begin{verbatim}
\let\origenddocument=\enddocument
\def\enddocument{\clearpage\if@filesw
{\addtocounter{page}{-1} \immediate\write\@mainaux
{\string\newlabel{LastPage}{{}{\thepage}}}}\origenddocument}
\end{verbatim}
The value of the \texttt{LastPage} label can be used to
make different headers or footers on the last page of a document. E.g.\ if
you want the footer of every odd page, except if it is the last one, to
contain the text ``please turn over'', this can be done as
follows\footnote{This requires a reasonably recent version of the
\texttt{ifthen} package.}:
\begin{verbatim}
\usepackage{lastpage}
\usepackage{ifthen}
...
\rfoot{\ifthenelse{\isodd{\value{page}} \and \not
\value{page}=\pageref{LastPage}{please turn over}{}}
\end{verbatim}
\section{Chapter or section related page numbers}
In technical documentation very often page numbers are used of the form
2-10 where the first number is the chapter number and the second is the
pagenumber relative to the chapter. Sometimes section is used rather than
chapter. The package \textsf{chappg} can be used to get this format. If you
want to change the layout e.g. use a dot rather than a dash, or a section
rather than a chapter, you should make a private copy and edit it.
This package redefines \Cmd{thepage} as
\Cmd{arabic\{chapter\}-}\Cmd{arabic\{page\}}.
Unfortunately this gives numbers rather than letters for appendices. A
better definition would be \Cmd{thechapter-}\Cmd{arabic\{page\}} but you can
give this definition yourself after the \Cmd{usepackage\{chappg\}} command.
What the package also does is reset the page number to 1 at the beginning
of each chapter.
There is a fundamental difference between the page numbering of the style
``\emph{m} of \emph{n}'' as described in the previous section and the
current one. The \emph{m} of \emph{n} style is only used in the page header
or footer, but not in the table of contents, index, or references like
``\emph{See page \emph{xx}}''. Therefore it does not change the command
\Cmd{thepage}. The page numbering style ``2-10'', however should be used
in all references to the page number, therefore it must be done by redefining
\Cmd{thepage}.
\section{When to change the headers and footers?}
\label{sec:change}
Sometimes you want to change the header or footer layout in the course of a
document. Some of these changes can be accomplished by using the mark
mechanism as may be seen in section~\ref{sec:custom} and \ref{sec:xmarks}.
However, sometimes we want a more drastic change, e.g to change the page
numbering from roman to arabic (with \Cmd{pagenumbering}), to change one of
\index{page style!changes}
the \textsf{fancyhdr} fields or to change to another page style.
Sometimes you may be surprised to find the change to occur too early. In
general the above mentioned changes take effect immediately, i.e.\ on the
page that is currently being built. If you want the change to take effect
at the next page you must make sure that the current page is finished. In
\CmdIndex{clearpage}
most cases this can be done by issuing a \Cmd{clearpage} command before
any of the above mentioned changes. If this is not possible you can use the
\TTindex{afterpage.sty}
\texttt{afterpage} package with:\\
\Cmd{afterpage\{}\Cmd{lhead\{new value\}\}} or
\CmdIndex{pagenumbering}
\Cmd{afterpage\{}\Cmd{pagenumbering\{roman\}}. You cannot use \Cmd{afterpage}
to change the \Cmd{pagestyle} as the commands issued by \Cmd{afterpage}
are local in a group, and the \Cmd{pagestyle} command makes only local
changes. The \Cmd{pagenumbering} and the \textsf{fancyhdr} commands
make global changes so they will work, as will the \Cmd{thispagestyle}
command.
It should be noted that although the \textsf{fancyhdr} commands like
\Cmd{fancyhead} take effect immediately, this does not mean that any
``variables'' used in these commands get the value they have at the place
where these commands are given. E.g.\ if \Cmd{fancyfoot[C]\{}\Cmd{thepage\}} is given
the page number that will be inserted in the footer is not the page number
of the page where this command is given, but rather the page number of
the actual page where the footer is constructed. Of course for the page
number this is what you expect, but it is also true for other commands.
So if you have a book where each chapter is written by a different author
and you want the name of the author in the lower left-hand corner you can
use the following commands:
\begin{verbatim}
\newcommand{\TheAuthor}{}
\newcommand{\Author}[1]{\renewcommand{\TheAuthor}{#1}}
\lfoot{\TheAuthor}
\end{verbatim}
\noindent and start each chapter with the command
\Cmd{Author\{Real Name\}}.
If however, the author name would be changed before a page is
completed the wrong author could come in the footer. This would be the case
if you gave the above command \emph{before} the \Cmd{chapter} command
rather than after it.
Another source of problems is the fact that \tex/'s output routine processes
commands ahead, so it may already have processed some commands that produce
text that will appear on the next page. See the next section for an example.
\section{Headers and footers induced by the text}
\label{sec:xmarks}
We have seen how we can use \LaTeX's marks to get information from the
document contents to the headers and footers. The marks mechanism is the
only reliable mechanism that you can use to get changing information to the
headers or footers. This is because \latex/ may be processing your document
ahead before deciding to break the page.
Sometimes the two marks that \latex/ offers are not enough. An example is
the following:
\begin{quote}
If a solution to an exercise goes across a page break, then I would like
to have ``(Continued on next page\ldots)'' at the bottom of the
\index{Continued\ldots}
first page and ``(Continued\ldots)'' at the top in the margin of the next page.
\end{quote}
You cannot use \latex/'s mark mechanisms for this if you also want to use
chapter and section information.
The code from figure~\ref{fig:xmarks} constitutes a package that gives you
two extra marks that can be used in this situation\footnote{After I made
this package I discovered a package \texttt{secret.sty} that does a
similar thing to mark confidential paragraphs if they cross a page
boundary. It does it, however, by changing the output routine.}.
\begin{figure}[tb]\small
\CmdIndex{extramarks}
\begin{verbatim}
% extramarks.sty
\def\@leftmark#1#2#3#4{#1}
\def\@rightmark#1#2#3#4{#2}
\def\markboth#1#2{{\def\protect{\noexpand\protect\noexpand}
\let\label\relax \let\index\relax \let\glossary\relax
\expandafter\@markboth\@themark{#1}{#2}
\mark{\@themark}}\if@nobreak\ifvmode\nobreak\fi\fi}
\def\markright#1{{\def\protect{\noexpand\protect\noexpand}
\let\label\relax \let\index\relax \let\glossary\relax
\expandafter\@markright\@themark
{#1}\mark{\@themark}}\if@nobreak\ifvmode\nobreak\fi\fi}
\def\@markright#1#2#3#4#5{\gdef\@themark{{#1}{#5}{#3}{#4}}}
\def\@markboth#1#2#3#4#5#6{\gdef\@themark{{#5}{#6}{#3}{#4}}}
\def\leftmark{\expandafter\@leftmark\botmark{}{}{}{}}
\def\rightmark{\expandafter\@rightmark\firstmark{}{}{}{}}
\def\firstleftmark{\expandafter\@leftmark\firstmark{}{}{}{}}
\def\lastrightmark{\expandafter\@rightmark\botmark{}{}{}{}}
\def\@themark{{}{}{}{}}
\def\extramarks#1#2{{\def\protect{\noexpand\protect\noexpand}
\let\label\relax \let\index\relax \let\glossary\relax
\expandafter\@markextra\@themark{#1}{#2}
\mark{\@themark}}\if@nobreak\ifvmode\nobreak\fi\fi}
\def\@markextra#1#2#3#4#5#6{\gdef\@themark{{#1}{#2}{#5}{#6}}}
\def\firstxmark{\expandafter\@firstxmark\firstmark{}{}{}{}}
\def\topxmark{\expandafter\@firstxmark\topmark{}{}{}{}}
\def\lastxmark{\expandafter\@lastxmark\botmark{}{}{}{}}
\def\@firstxmark#1#2#3#4{#3}
\def\@lastxmark#1#2#3#4{#4}
\end{verbatim}
\caption{Package for extra marks in \latex/}
\label{fig:xmarks}
\end{figure}
Here is a way to use this package:
\begin{verbatim}
\usepackage{extramarks}
...
\pagestyle{fancy}
\lhead{\firstxmark}
\rfoot{\lastxmark}
...
\extramarks{}{Continued on next page\ldots}
Some text that may or may not cross a page boundary...
\extramarks{Continued\ldots}{}
\end{verbatim}
\CmdIndex{extramarks}
Note that the \Cmd{extramarks} command must be close to the text, i.e no
empty lines (paragraph boundaries) should intervene. Otherwise the page may
be broken at that boundary and the extramarks would come on the wrong page.
There are two new marks that can be used in the page layout with this
package: If commands of the form
\verb|\extramarks{|$m_1$\verb|}{|$m_2$\verb|}| are given
\CmdIndex{firstxmark}
\CmdIndex{lastxmark}
\Cmd{firstxmark} gives you the first $m_1$ value and
\Cmd{lastxmark} gives you the last $m_2$ value
of the current page.
\CmdIndex{firstleftmark}
\CmdIndex{lastrightmark}
It also gives you the \Cmd{firstleftmark} and \Cmd{lastrightmark}
commands that complement the standard \latex/ marks.
To stress the point that marks are the correct way to do this, let me
give you a ``solution'' that will not work\footnote{Actually there is
another way but it requires two \latex/ passes: you can put \Cmd{label}
commands before and after the text and compare the \Cmd{pageref}s.}:
\begin{verbatim}
\lhead{Continued}
\rfoot{Continued on next page\ldots}
Some text that may or may not cross a page boundary...
\lhead{}
\rfoot{}
\end{verbatim}
You may be tempted to think that the first \Cmd{lhead} and \Cmd{rfoot}
will be in effect when \tex/ breaks the page in the middle of the text,
and the last ones when the page breaks after the text. This is not true as
the whole paragraph (including the last definitions) will be processed
\index{page break}
before \tex/ considers the page break, so at the time of the page break the
last definitions are effective, whether the page break occurs inside the text
or outside of it. Putting a paragraph boundary between the
text and the last definitions will not work either, because you don't want
the first definitions to be in effect when \tex/ decides to break the page
exactly at this boundary. Actually the marks mechanism was invented to get
rid of these kinds of problems.
In the above example the text ``Continued'' appears in the page header. It
\index{margin}
may be nicer to put it in the margin. This can be easily
accomplished by positioning it at a fixed place relative to the page header.
In plain \tex/ you would use a concoction of \Cmd{hbox to 0pt},
\Cmd{vbox to 0pt}, \Cmd{hskip},\Cmd{vskip}, \Cmd{hss} and
\Cmd{vss} but fortunately
\latex/'s \texttt{picture} environment gives a much cleaner way to do this.
In order not to disturb the normal header layout we put the text in a zero-sized
\texttt{picture}. Generally this is the best way to position things on fixed
places on the page. You can then also use the normal headings. See also
section~\ref{sec:thumb} for another example of
this technique.
\TTindex{picture}
\begin{verbatim}
\lhead{\setlength{\unitlength}{\baselineskip}%
\begin{picture}(0,0)
\put(-2,-3){\makebox(0,0)[r]{\firstxmark}}
\end{picture}\leftmark}
\end{verbatim}
This solution can of course also be used for the footer. Make sure you put the
\texttt{picture} as the first thing in left-handside entries and last in right-handside
ones.
Finally you may want to put ``(Continued\ldots)'' in the \emph{text}
rather than in the header or the margin. Then you have to use the
\TTindex{afterpage.sty}
\texttt{afterpage} package. We also decide to make a separate
environment for it.
\begin{verbatim}
\newenvironment{continued}{\par
\extramarks{}{Continued on next page\ldots}
\afterpage{\noindent\firstxmark\vspace{1ex}}
}{\extramarks{(Continued\ldots)}{}\par}
\end{verbatim}
It is a bit dangerous to use \Cmd{firstxmark} outside the page layout
routine, but apparently with \Cmd{afterpage} this works. If you would
need the information further on in the page you must remember the state of
the marks in your own variable. You can set this in one of the
\textsf{fancyhdr} fields. For example if you want to add something
\emph{after} the broken piece of text you can use the following:
\begin{verbatim}
\newcommand{\mysaved}{}
\newenvironment{continued}{\par
\extramarks{}{Continued on next page\ldots}
}{\extramarks{(Continued\ldots)}{}\par\vspace{1ex}\mysaved}
\lhead{\leftmark}
\chead{\ifthenelse{\equal{\lastxmark}{}}
{\gdef\mysaved{}}
{\gdef\mysaved{\noindent[Continued from previous page]}}}
\end{verbatim}
If you want to include one of the marks or other varying information in the
saved text, you must use \Cmd{xdef} rather than \Cmd{gdef}.
\section{A movie}
\label{sec:movie}
\index{movie}
\TTindex{picture}
If you put at each page on the same place a picture that slightly changes
from page to page you can get a movie-like effect by flipping through the
pages. You can create such a movie easily with fancyhdr. For
simplicity we assume that each picture is in a postscript (EPS) file called
\texttt{pic}$\langle n\rangle$.\texttt{ps} where $\langle n\rangle$ is the page number and that we use
the \texttt{graphics} or \texttt{graphicx} package\footnote{If you use an older version of
\LaTeX{} you could use the \texttt{epsf} or \texttt{epsfig} package.}.
\TTindex{graphics}
\TTindex{graphicx}
\TTindex{epsf}
\TTindex{epsfig}
To put the movie in the righthandside bottom corner the following will work:
\begin{verbatim}
\rfoot{\setlength{\unitlength}{1mm}
\begin{picture}(0,0)
\put(5,0){\includegraphics{pic\thepage.ps}}
\end{picture}}
\end{verbatim}
Notice that the \Cmd{unitlength} parameter should
be set locally in the fancyhdr field in order to avoid unwanted
interference with its value in the text.
\section{Thumb-indexes}
\label{sec:thumb}
\index{bible}
Some railroad guides and expensive bibles have so called
\index{thumb-index}
\emph{thumb-indexes}, i.e.\ there are marks on the sides of the pages that
indicate where the chapters are. You can create these by printing black
blobs in the margin of the pages. The vertical position should be
determined by the chapter number or some other counter. As the position is
independent of the contents of the page, we print these blobs as part of
the header in a zero-sized \texttt{picture} as described in the previous
section.
Of course we have to take care of two-sided printing, and we may want to
have an index page with all the blobs in the correct position. The solution
requires some hand-tuning to get the blobs nicely spaced out vertically.
For the application that I had there were 12 sections, so I made the blobs
18 mm apart, i.e. 9~mm blob separated by 9~mm whitespace. In order to avoid
calculations they are set in a \texttt{picture} environment with the
\Cmd{unitlength} set to 18~mm. Page numbers are set in the headers at the
outer sides, and the blobs are attached to these. In this example the
section numbers are used to position the blobs, but you can replace this
with any numeric value.
See figure~\ref{fig:overview} for the resulting
overview page and figure~\ref{fig:thumb} for the code.
\begin{figure}[htbp]
\setlength{\unitlength}{9mm}
\newcommand{\blob}{\rule[-.2\unitlength]{1\unitlength}{.5\unitlength}}
\newcounter{line}
\newcommand{\secname}[1]{\addtocounter{line}{1}%
\put(1,-\value{line}){\blob}
\put(-7.5,-\value{line}){\arabic{line}}
\put(-7,-\value{line}){#1}}
\newcommand{\overview}{1
\begin{picture}(0,0)
\secname{Introduction}
\secname{The first year}
\secname{Specialisation}
\end{picture}}
\begin{center}
\leavevmode
\begin{picture}(11.3,5)
\put(0,0){\framebox(11.3,5)[tr]{}}
\put(9,4.5){\overview}
\end{picture}
\end{center}
\caption{Thumb-index overview page}
\label{fig:overview}
\end{figure}
\begin{figure}[hp]\small
\begin{verbatim}
\setlength{\unitlength}{18mm}
\newcommand{\blob}{\rule[-.2\unitlength]{2\unitlength}{.5\unitlength}}
\newcommand\rblob{\thepage
\begin{picture}(0,0)
\put(1,-\value{section}){\blob}
\end{picture}}
\newcommand\lblob{%
\begin{picture}(0,0)
\put(-3,-\value{section}){\blob}
\end{picture}%
\thepage}
\pagestyle{fancy}
\cfoot{}
\newcounter{line}
\newcommand{\secname}[1]{\addtocounter{line}{1}%
\put(1,-\value{line}){\blob}
\put(-7.5,-\value{line}){\Large \arabic{line}}
\put(-7,-\value{line}){\Large #1}}
\newcommand{\overview}{\thepage
\begin{picture}(0,0)
\secname{Introduction}
\secname{The first year}
\secname{Specialisation}
...etc...
\end{picture}}
\begin{document}
\fancyhead[R]{\overview}\mbox{}\newpage % This produces the overview page
\fancyhead[R]{} % Front matter may follow here
\clearpage
\fancyhead[RE]{\rightmark}
\fancyhead[RO]{\rblob}
\fancyhead[LE]{\lblob}
\fancyhead[LO]{{\leftmark}
...
\end{verbatim}
\caption{Thumb-index code}
\label{fig:thumb}
\end{figure}
\section{Float placement}
Floats are page elements that float with respect to the rest of the
document. Standard floats are tables and figures, but with the
\textsf{float} package you can easily make new ones, like algorithms. Most
of the time floats work satisfactory, but sometimes \LaTeX{} seems too
stubborn to do what you want. This section describes how you can influence
\LaTeX{} so that it will do most of the time what you want. There might,
however be some pathological cases where it is impossible to convince
\LaTeX{} to do things your way. In the following we will use figures as an
example but everything applies to other floats as well.
The most encountered problems with floats are:
\begin{enumerate}
\item You want a float at a certain position in the text, but \LaTeX{}
moves it, usually to the next page.
\item From a certain point, \LaTeX{} moves all your floats to the end of
the document or the end of a chapter.
\item \LaTeX{} complains about ``Too many floats''.
\end{enumerate}
In the first two cases you must first check if you have given the correct
``placement'' parameter to you float, e.g. \Cmd{begin\{figure\}[htp]}
specifies that your figure may be placed either: Here (i.e. in the text
position where the command is given), on the Top of a page (which may be
the page where you put the command), or on a separate Page of floats. You
could also have specified ``b'' for Bottom of the page. The order of the
letters is insignificant, you cannot force \LaTeX{} to try Bottom first
and then Top by specifying \texttt{[bt]}.
If \LaTeX{} doesn't put the float at the place where you expected it, it is
usually caused by the following:
\begin{enumerate}
\item The float didn't fit on the page. In this case it has to move to the
next page or even further. If you didn't specify either \texttt{[t]} or
\texttt{[b]} in the position parameter, \latex/ must save it until it has
enough for a page of floats. So don't specify only \texttt{[h]}. If you
want to give \latex/ a chance to put the float on a page of floats, you
must also specify ``p''.
\item The placement would violate the constraints imposed by \latex/'s
float placement parameters. This is one of the most occurring causes and
it can easily be corrected by changing the parameters. Here is a list of
them:
\end{enumerate}
\begin{center}
\CmdIndex{topfraction}
\CmdIndex{bottomfraction}
\CmdIndex{textfraction}
\CmdIndex{floatpagefraction}
\TTindex{topnumber}
\TTindex{bottomnumber}
\TTindex{totalnumber}
\begin{tabular}{>{\tt}llc}
\hline
\multicolumn{3}{c}{Counters -- change with \Cmd{setcounter}}\\
\hline
topnumber & max. number of floats at top of page & 2 \\
bottomnumber & max. number of floats at bottom of page & 1 \\
totalnumber & max. number of floats on a page & 3\\
\hline
\multicolumn{3}{c}{Other -- change with \Cmd{renewcommand}}\\
\hline
\bs topfraction & max fraction of page for floats at top & 0.7 \\
\bs bottomfraction & max fraction of page for floats at bottom & 0.3 \\
\bs textfraction & min fraction of page for text & 0.2 \\
\bs floatpagefraction & min fraction of floatpage that should have
floats & 0.5 \\
\hline
\end{tabular}
\end{center}
There are also some others for double column floats in two-column documents.
The values in the righthand column are the defaults for the standard
\latex/ classes. Other classes could use different defaults. As you see
with the default values a float will not be put in the bottom of a page if
its height is more than 30\% of the page height. So if you specify [hb] for
a float which is taller it has to move to a float page. But if it is less
than 50\% of the page height it will have to wait until some more floats
are given before a float page can be filled to satisfy the
\Cmd{floatpagefraction} parameter. If you have this kind of behaviour you
can easily adapt the parameters, e.g. with:
\begin{verbatim}
\renewcommand{\textfraction}{0.05}
\renewcommand{\topfraction}{0.95}
\renewcommand{\bottomfraction}{0.95}
\renewcommand{\floatpagefraction}{0.35}
\setcounter{totalnumber}{5}
\end{verbatim}
You may want to be careful not to make \Cmd{floatpagefraction} too small,
otherwise you may get too many small floatpages.
You can force \latex/ to ignore most of the parameters for one specific float
occurrence by
including an exclamation mark (!) in the placement parameters, e.g.
\begin{verbatim}
\begin{figure}[!htb]
\end{verbatim}
Floats which contain a ``\texttt{t}'' in the position parameter could be
placed before the place where they are referenced (but on the same page).
This is normal behaviour for \latex/ but some people just don't like it.
There are a number of ways to prevent this:
\begin{enumerate}
\item Of course deleting the ``\texttt{t}'' will help, but in general this is
undesirable, as you may want the float to be placed at the top of the
next page.
\item use the \texttt{flafter} package which causes floats never to be
placed ``backwards''.
\item use the command \Cmd{suppressfloats[t]}\footnote{This command and the
``!'' placement parameter are not defined in \LaTeX2.09.}. This
command will cause floats for the top position \emph{on this page} to be
moved to the next page. This can also be done with \texttt{[b]} or
without parameter for all floats on this page.
\end{enumerate}
If in spite of all your attempts \latex/ still moves your floats to the end
of the document or the end of a chapter, you can insert a \Cmd{clearpage}
command. This will start a new page and insert all pending floats before
continueing. If it is undesirable to have a pagebreak you can use the
\textsf{afterpage} package and the following command:
\begin{verbatim}
\afterpage{clearpage}
\end{verbatim}
This will wait until the current page is finished and then flush all
outstanding floats. In some pathological circumstances \texttt{afterpage}
may give strange results, however.
Finally, if you want a float only at the place where you define it, without
\latex/ moving it whatsoever, you can use the \textsf{float} package and
give the command:
\begin{verbatim}
\restylefloat{figure}
\end{verbatim}
in the preamble. Now you will be able to
specify \texttt{[H]} as the position parameter, which will mean ``HERE and only
HERE''. This may cause an unwanted page break however\footnote{There used
to be a \texttt{here.sty} with the same effect, but this is incompatible
with \LaTeXe}. If you want to avoid the unwanted pagebreak, i.e. let
\LaTeX{} move the float only if it doesn't fit on the page, the use the
\textsf{afterpage} package with:
\begin{verbatim}
\afterpage{\clearpage \begin{figure}[H] ... \end{figure}}
\end{verbatim}
Complaints from \latex/ about ``Too many floats'' are usually caused by one
of the above problems: floats not being able to be placed and \latex/
collecting too many of them. The solutions given above, especially those
with \Cmd{clearpage} in them will usually help. In some cases there
really are too many floats, as \latex/ has a limited number of ``boxes'' to
store the floats. The package \textsf{morefloats} can be used to increase
this number. If you need still more then you must edit a private copy of
this file, but even then there will be some limit that you cannot pass.
Then your only resort will be to change your document.
\section{Multipage Floats}
\LaTeX's floats cannot be split across pages. Sometimes, however, you want
to have a table or figure that doesn't fit on one page. The easiest way is
to split these into multiple table or figure environments, but this has a
number of undesirable effects:
\begin{itemize}
\item Where do you split it? This is in general more difficult for tables
than for figures.
\item How do you keep them together?
\item You don't want more than one entry in the list of figures/tables.
\end{itemize}
Although these problems are not fully solvable in all cases, here are a
couple of suggestions:
\subsection{Tables}
For tables longer than a page you can use the \texttt{longtable} package.
\TTindex{longtable}
This package defines a \texttt{longtable} environment that is a kind of
amalgamation of \texttt{table} and \texttt{tabular}. It has approximately
the same syntax as the \texttt{tabular} environment, but it adds some
features of \texttt{table}, like captions. Longtables will be automatically
split when they don't fit on the page. And they will be entered in the list
of tables when a caption is given. They will not float, however, and cannot
be used inside a float environment. This could mean that another
\texttt{table} environment, which was defined before the
\texttt{longtable}, will float past it, and therefore the numbers may get
out of order. Another problem could be that the \texttt{longtable} starts
rather far down the page, which isn't a pleasant sight. If you want the
\texttt{longtable} to start at the top of the page, the best thing to do is
to include it in an \Cmd{afterpage} command (using the \texttt{afterpage}
package). As a \texttt{longtable} is by definition large, it is best to put
it in a separate file, and \Cmd{input} it in the \Cmd{afterpage} command:
\CmdIndex{afterpage}
\begin{verbatim}
\afterpage{\input{mytable}}
\end{verbatim}
\begin{verbatim}
\afterpage{\clearpage\input{mytable}}
\end{verbatim}
The last form has the additional advantage that most of the outstanding
floats will be printed first.
\subsection{Figures}
There isn't an equivalent \texttt{longfigure} solution, so for figures you
will usually have to split it yourself. In general this is less of a
problem. However, the problem you get now is how to keep them together,
i.e. how to get the parts on subsequent pages, and how to get a single
entry in the list of figures.
You will have to split the figure into pieces and put each part
in a separate \texttt{figure} environment. To keep them together it is best
to use only the \texttt{[p]} placement, so that they will be put on
floatpages. As they are bigger than a page this is appropriate. The first
part would then get a \Cmd{caption}, the subsequent parts would be used
without a caption. If you want to add a caption-like text, enter it as
normal text rather than a \Cmd{caption}, so that it will not be entered in
the list of figures. It may also be desirable to issue a \Cmd{clearpage}
first, just like we did for the \texttt{longtable}, and to encapsulate this
in the \Cmd{afterpage} command. E.g.
\begin{verbatim}
\afterpage{\clearpage\input{myfigure}}
\end{verbatim}
where \texttt{myfigure.tex} contains:
\begin{verbatim}
\begin{figure}[p]
\includegraphics{myfig1.eps}
\caption{This is a multipage figure}
\label{fig:xxx}
\end{figure}
\begin{figure}[p]
\includegraphics{myfig2.eps}
\begin{center}
Figure~\ref{fig:xxx} (continued)
\end{center}
\end{figure}
\end{verbatim}
You have to make sure that the last part is big enough, otherwise \LaTeX{}
could decide to postpone it until it has collected some more floats. This
can be done either by making the figure big enough (e.g. by adding some
\Cmd{vspace}), or by tweaking the \Cmd{floatpagefraction}
\CmdIndex{floatpagefraction} parameter.
If you want your multipage figure to start at a lefthand-side
(even-numbered) page you can use a test in the \Cmd{afterpage} command
(using the \texttt{ifthen} package):
\begin{verbatim}
\afterpage{\clearpage
\ifthenelse{\isodd{\value{page}}{\afterpage{\input{myfigure}}} % odd page
{\input{myfigure}}}} % even page
\end{verbatim}
If there are too many floats on the skipped page, this may still fail to
start your multipage figure on an even page, however.
\section{Contact information}
Piet van Oostrum\\
Dept.\ of Computer Science\\
Utrecht University\\
P.O. Box 80.089\\
3508 TB Utrecht, The Netherlands\\
Telephone: +31 30 2531806 Telefax: +31 30 2513791\\
E-mail: piet@cs.ruu.nl \\
WWW: http://www.cs.ruu.nl/people/piet
\printindex
\end{document}
% Local Variables:
% mode: latex
% TeX-master: t
% End: