-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfancyhdr.tex
1729 lines (1494 loc) · 64.9 KB
/
fancyhdr.tex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
\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}}
\newcommand{\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}
\addtolength{\textheight}{-0.5in} % make it print better on US letter paper
\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 header fields
\fancyhead[RO,LE]{\bfseries The performance of new graduates}
\fancyfoot{} % clear all footer fields
\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. Similar the \Cmd{fancyfoot} command
without square brackets clears all footer fields. In this particular
example it could be omitted as all footer fields are specified.
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 David Carlisle's fix2col.sty\TTindex{fix2col.sty} this will be solved.
\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{The width of the headers and footers}
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 using the commands
\CmdIndex{fancyheadoffset}
\CmdIndex{fancyfootoffset}
\CmdIndex{fancyhfoffset}
\Cmd{fancyheadoffset}, \Cmd{fancyfootoffset} and \Cmd{fancyhfoffset}%
\footnote{These commands are defined since fancyhdr version 3.0. In older
versions you just changed \Cmd{headwidth} with the
\Cmd{setlength} and \Cmd{addtolength} commands. You are encouraged to use
these newer offset commands instead. You certainly can't mix the two
approaches.}.
These are similar to \Cmd{fancyhead}, \Cmd{fancyfoot} and \Cmd{fancyhf}.
The only difference is that the offsets don't accept the C option.
Usage: \Cmd{fancyhfoffset[place]\{length\}}
and similar for the others.
It defines offsets to be applied to the header/footer to let it stick into
the margins if length $>$ 0 and to be smaller if length $<$ 0.
place is like in \Cmd{fancyhead}, except that only E,O,L,R (or the lowercase)
can be used.
With these commands \Cmd{headwidth} will be dynamically calculated in
the headers/footers to be \Cmd{textwidth} + left offset + right offset, so
that e.g. you can use the following to define a red line in the header
and a blue line in the footer (please note that the width of the footer is
also called \Cmd{headwidth} although it may differ from the width of the
header):
\begin{verbatim}
\renewcommand{\headrule}{{\color{red}%
\hrule width\headwidth height\headrulewidth \vskip-\headrulewidth}}
\setlength{\footrulewidth}{\headrulewidth}
\renewcommand{\footrule}{{\color{blue}%
\vskip-\footruleskip\vskip-\footrulewidth
\hrule width\headwidth height\footrulewidth\vskip\footruleskip}}
\end{verbatim}
\section{Two book examples}
The following definitions give an approximation of the style
used in Leslie Lamport's \latex/ book.
Lamport's header overhangs the outside margin. This is done with the
offset commands described in the previous section.
\begin{verbatim}
\documentclass{book}
\usepackage{fancyhdr}
\pagestyle{fancy}
\usepackage{calc}
\fancyheadoffset[LE,RO]{\marginparsep+\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.\