fancyhdr.tex 66 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762
  1. \documentclass[a4paper]{article}
  2. \usepackage{multicol}
  3. \usepackage{float}
  4. \usepackage{makeidx}
  5. \usepackage{layout}
  6. \usepackage{array}
  7. \usepackage{a4wide}
  8. \usepackage{boxedminipage}
  9. %%\usepackage{fancyheadings}
  10. %%\pagestyle{fancy}
  11. %%\lhead{\rightmark}
  12. %%\rhead{\thepage}
  13. %%\cfoot{}
  14. \makeindex
  15. \title{Page layout in \LaTeX}
  16. \author{Piet van Oostrum\thanks{A considerable part of this article was
  17. written by George
  18. Gr\"atzer (University of Manitoba) in \emph{Notices Amer. Math. Soc.}
  19. Thanks, George!}\\
  20. Dept.\ of Computer Science\\
  21. Utrecht University}
  22. \def\latex/{\protect\LaTeX{}}
  23. \def\tex/{\TeX}
  24. \def\ams/{\protect\pAmS}
  25. \def\pAmS{{\the\textfont2
  26. A\kern-.1667em\lower.5ex\hbox{M}\kern-.125emS}}
  27. \def\amslatex/{\ams/-\latex/}
  28. \newcommand{\PSNFSS}{{\sf
  29. PSNFSS}}
  30. \renewcommand{\bs}{\symbol{'134}}
  31. \newcommand{\Cmd}[1]{\texttt{\def\{{\char`\{}\def\}{\char`\}}\bs#1}}
  32. \newcommand{\CmdIndex}[1]{\index{#1@\texttt{\bs#1}}}
  33. \newcommand{\TTindex}[1]{\index{#1@\texttt{#1}}}
  34. \newcommand{\PSindex}[1]{\index{page style!#1@\texttt{#1}}}
  35. %\floatstyle{ruled}
  36. \restylefloat{figure}
  37. \renewcommand{\topfraction}{0.9}
  38. \renewcommand{\bottomfraction}{0.9}
  39. \renewcommand{\textfraction}{0.05}
  40. \setlength{\parindent}{0pt}
  41. \setlength{\parskip}{1ex}
  42. \setlength{\emergencystretch}{4em}
  43. \makeatletter
  44. \renewcommand\l@section {\@dottedtocline{1}{1.5em}{2.3em}}
  45. \makeatother
  46. \newenvironment{block}{\vspace{8pt}\begin{minipage}{\textwidth}}{\end{minipage}\vspace{8pt}}
  47. \newenvironment{fblock}{\vspace{8pt}\begin{boxedminipage}{\textwidth}}{\end{boxedminipage}\vspace{8pt}}
  48. \begin{document}
  49. \maketitle
  50. \begin{abstract}
  51. This article describes how to customize the page layout of your LaTeX
  52. documents, i.e how to change page margings and sizes,
  53. headers and footers, and the
  54. proper placement of figures and tables (collectively called floats) on
  55. the page.
  56. Originally this was the documentation of the \textsf{fancyheadings}
  57. package. It did contain also other info, e.g. advanced use of marks.
  58. It has now been upgraded to include more, e.g. the handling of floats.
  59. The fancyheadings documentation has been upgraded to conform to version 2
  60. of this package\footnote{this version is due to be released Real Soon Now}. For reasons of compatibility with certain operating systems, the
  61. name of the package has been changed to \textsf{fancyhdr}.
  62. Although this paper uses \LaTeXe{} commands, most of the techniques can
  63. be used with older \LaTeX versions with appropriate changes.
  64. \end{abstract}
  65. \tableofcontents
  66. \section{Introduction}
  67. \label{sec:intro}
  68. A page in a \LaTeX{} document is built from various elements as shown in
  69. figure \ref{fig:layout}.
  70. \begin{figure}[htbp]
  71. \begin{center}
  72. \leavevmode
  73. \layout
  74. \vspace{3cm}
  75. \caption{Page elements. The values shown are those in effect in the current document, not the defaults.}
  76. \label{fig:layout}
  77. \end{center}
  78. \end{figure}
  79. %\thispagestyle{fancy}
  80. \thispagestyle{plain}
  81. The body contains the main text of the document
  82. together with the so called floats (tables and figures).
  83. The pages are constructed by \LaTeX's output routine, which is quite
  84. complicated and should therefore not be modified. Some of the packages
  85. described in this paper contains small modifications to the output routine
  86. to accomplish things that cannot be done in another way. You should use
  87. these packages to get the desired result rather than fiddling with the
  88. output routine yourself.
  89. There a a number of things that you must be aware of:
  90. \begin{enumerate}
  91. \item The margins on the left are not called \Cmd{leftmargin}, but
  92. \Cmd{evensidemargin} (on even-numbered pages) and \Cmd{oddsidemargin}
  93. (on odd-numbered pages). In one-sided documents
  94. \Cmd{oddsidemargin} is used for either. \Cmd{leftmargin} is also a valid
  95. \latex/ parameter but it has a different use (namely the indentation of
  96. lists).
  97. \item Most of the parameters should not be changed in the middle of a
  98. document. Some changes might work at a pagebreak. If you want to change
  99. the height of a single page, you can use the \Cmd{enlargethispage}
  100. command.
  101. \end{enumerate}
  102. The margin notes
  103. area contains small pieces of information created by the \Cmd{marginpar}
  104. command. On twosided documents the margin notes appear on the left and right
  105. alternatively. The margin notes are not on fixed places with respect to the
  106. paper but at approximately the same height as the paragraph in which they
  107. appear. Due to the algorithm used to decide the placement of margin notes,
  108. in a twosided document unfortunately
  109. they may appear on the wrong side if they are close
  110. to a page break.
  111. If you want to put information on fixed places in the margins you may use
  112. the technique described in sections \ref{sec:movie} and \ref{sec:thumb}.
  113. The first part of this paper describes how to change the header and footer
  114. areas. The last part describes how to get your floats at the desired place.
  115. \section{Page headers and footers}
  116. The page headers and footers in \LaTeX{} are defined by the
  117. \Cmd{pagestyle} and \Cmd{pagenumbering} commands. \Cmd{pagestyle}
  118. defines the general contents of the headers and footers (e.g. where the
  119. page number will be printed), while
  120. \Cmd{pagenumbering} defines the format of the page number.
  121. \LaTeX{} has four standard pagestyles:
  122. \begin{tabular}{>{\tt}lp{10cm}}
  123. empty & no headers or footers \\
  124. plain & no header, footer contains page number centered \\
  125. headings & no footer, header contains name of chapter/section and/or
  126. subsection and page number \\
  127. myheadings & no footer, header contains page number and user supplied information
  128. \end{tabular}
  129. Although these are useful styles, they are quite limited. Additional page
  130. styles can be defined by defining commands of the form \Cmd{ps@xxx}. This
  131. command is executed when a \Cmd{pagestyle\{xxx\}} is given in the document.
  132. The \Cmd{ps@xxx} command should define the following
  133. commands for the contents of the headers and footers:
  134. \noindent
  135. \begin{tabular}{lp{10cm}}
  136. \Cmd{@oddhead} & header on odd numbered pages in two-sided documents
  137. (on all pages in one-sided) \\
  138. \Cmd{@evenhead} & header on even numbered pages in two-sided documents \\
  139. \Cmd{@oddfoot} & footer on odd numbered pages in two-sided documents
  140. (on all pages in one-sided) \\
  141. \Cmd{@evenfoot} & footer on even numbered pages in two-sided documents \\
  142. \end{tabular}
  143. These are not user commands, but rather ``variables'' that are used by
  144. \latex/'s output routine.
  145. As the command names contain the character '\texttt{@}', they should
  146. be defined in a package file, or otherwise be sandwiched between the commands
  147. \Cmd{makeatletter} and \Cmd{makeatother}.
  148. The \Cmd{pagenumbering} command defines the layout of the page number. It
  149. has a parameter from the following list:
  150. \begin{tabular}{>{\tt}ll}
  151. arabic & arabic numerals \\
  152. roman & lower case roman numerals \\
  153. Roman & upper case roman numerals \\
  154. alph & lower case letter \\
  155. Alph & upper case letter
  156. \end{tabular}
  157. The \Cmd{pagenumbering\{xxx\}} defines the command \Cmd{thepage} to be the
  158. expansion of the page number in the given notation \texttt{xxx}.
  159. The pagestyle command
  160. then would include \Cmd{thepage} in the appropriate place. Additionally
  161. the \Cmd{pagenumbering} command resets the page number to~1.
  162. The \Cmd{pagestyle} and \Cmd{pagenumbering} apply to the page that is
  163. being constructed, so they should be used at a location where it is clear
  164. to what page they apply (see section \ref{sec:change}).
  165. \section{What is \textsf{fancyhdr}}
  166. The \textsf{fancyhdr} macro package allows you to customize
  167. in \latex/ your page headers and footers in an easy way. You can
  168. define:
  169. \begin{itemize}
  170. \item three-part headers and footers
  171. \item decorative lines in headers and footers
  172. \item headers and footers wider than the width of the text
  173. \item multi-line headers and footers
  174. \item separate headers and footers for even and odd pages
  175. \item different headers and footers for chapter pages
  176. \item different headers and footer on pages with floats
  177. \end{itemize}
  178. Of course, you also have complete control over fonts, uppercase
  179. and lowercase displays, etc.
  180. %%% \section{Where to get it?}\label{get}
  181. %%% You only need the file \verb|fancyhdr.sty| which you can find at the
  182. %%% \index{ftp}
  183. %%% CTAN sites: \verb|ftp.shsu.edu| (U.S.), \verb|ftp.tex.ac.uk| (U.K.), and
  184. %%% \verb|ftp.dante.de| (Germany).
  185. %%% You will find it in the directory\\
  186. %%% \verb|/tex-archive/macros/latex209/contrib/fancyhdr|. Although
  187. %%% \textsf{fancyhdr}
  188. %%% is a \latex/ 2.09 style file, it will work with \LaTeXe{}. Also this
  189. %%% \TTindex{fixmarks.sty}
  190. %%% \TTindex{extramarks.sty}
  191. %%% article and the two packages \texttt{fixmarks} and \texttt{extramarks} can
  192. %%% be found there. (The code for these packages given in this article is a
  193. %%% simplified version.)
  194. \section{Simple use of \textsf{fancyhdr}} To use this package in a
  195. \LaTeXe\ document, place the file \verb|fancyhdr.sty| in a
  196. directory/folder where \tex/ can find it (normally in the input
  197. directory/folder), and include in the preamble of your document
  198. after
  199. \begin{verbatim}
  200. \documentclass{...}
  201. \end{verbatim}
  202. the commands\footnote{For LaTeX2.09 you should specify \texttt{[fancyhdr]}
  203. in the \Cmd{documentstyle} instead of the \Cmd{usepackage}
  204. command.}:
  205. \PSindex{fancy}
  206. \begin{verbatim}
  207. \usepackage{fancyhdr}
  208. \pagestyle{fancy}
  209. \end{verbatim}
  210. We can visualize the page layout we can create with \textsf{fancyhdr}
  211. as follows:
  212. \begin{fblock}
  213. \noindent\makebox[\textwidth]{LeftHeader\hfill
  214. CenteredHeader\hfill RightHeader}
  215. \noindent\makebox[\textwidth]{\hrulefill}\\[\baselineskip]
  216. \noindent\makebox[\textwidth]{\hfill page body\hfill}\\[\baselineskip]
  217. \noindent\makebox[\textwidth]{\hrulefill}
  218. \noindent\makebox[\textwidth]{LeftFooter\hfill
  219. CenteredFooter\hfill RightFooter}
  220. \end{fblock}
  221. The LeftHeader and LeftFooter are left justified; the
  222. CenteredHeader and CenteredFooter are centered; the
  223. RightHeader and RightFooter are right justified.
  224. We define each of the six ``fields'' and the two decorative lines
  225. separately.
  226. \section{A simple example} K. Grant is writing a report to Dean
  227. A. Smith, on ``The performance of new graduates'' with the
  228. following page layout:
  229. \begin{fblock}
  230. \noindent\makebox[\textwidth]{\hfill\bfseries The performance of new
  231. graduates}
  232. \noindent\makebox[\textwidth]{\hrulefill}\\[\baselineskip]
  233. \noindent\makebox[\textwidth]{\hfill page body\hfill}\\[\baselineskip]
  234. \noindent\makebox[\textwidth]{\hrulefill}
  235. \noindent\makebox[\textwidth]{From: K. Grant\phantom{3}\hfill
  236. To: Dean A. Smith\hfill \phantom{From: K. Grant}3}
  237. \end{fblock}
  238. \noindent where ``3'' is the page number. The title: ``The
  239. performance of new graduates'' is bold.
  240. This is accomplished by these commands following \Cmd{pagestyle\{fancy\}}
  241. \footnote{Note that version 1 of fancyheadings used the
  242. \Cmd{setlength} command to change the \texttt{\bs...rulewidth} parameters.}:
  243. \CmdIndex{lhead}
  244. \CmdIndex{rhead}
  245. \CmdIndex{chead}
  246. \CmdIndex{lfoot}
  247. \CmdIndex{rfoot}
  248. \CmdIndex{cfoot}
  249. \CmdIndex{headrulewidth}
  250. \CmdIndex{footrulewidth}
  251. \begin{verbatim}
  252. \lhead{}
  253. \chead{}
  254. \rhead{\bfseries The performance of new graduates}
  255. \lfoot{From: K. Grant}
  256. \cfoot{To: Dean A. Smith}
  257. \rfoot{\thepage}
  258. \renewcommand{\headrulewidth}{0.4pt}
  259. \renewcommand{\footrulewidth}{0.4pt}
  260. \end{verbatim}
  261. (The \Cmd{thepage} macro displays the current page number.
  262. \Cmd{bfseries} is the \LaTeXe's way of selecting bold face.)
  263. This is now fine, except that the first page does not need
  264. all these headers and footers. To eliminate all but the
  265. centered page number, issue the command
  266. \CmdIndex{thispagestyle}
  267. \begin{verbatim}
  268. \thispagestyle{plain}
  269. \end{verbatim}
  270. after the
  271. \Cmd{begin\{document\}}
  272. and the
  273. \CmdIndex{maketitle}
  274. \Cmd{maketitle}
  275. commands.
  276. Alternatively, issue
  277. \begin{verbatim}
  278. \thispagestyle{empty}
  279. \end{verbatim}
  280. if you do not want any headers or footers.
  281. In fact the standard \latex/ classes have the command \Cmd{maketitle}
  282. defined in such a way that a \Cmd{thispagestyle\{plain\}} is automatically
  283. issued. So if you \emph{do} want the fancy layout on a page containing
  284. \Cmd{maketitle} you must issue a \Cmd{thispagestyle\{fancy\}} after the
  285. \Cmd{maketitle}.
  286. \section{An example of two-sided printing}\label{two-sided}
  287. \TTindex{twoside}
  288. Some document classes, such as \verb|book.cls|, print two-sided by default: the even
  289. pages and the odd pages have different layouts; other document classes
  290. use the \verb|twoside| option to print two-sided.
  291. Now let us print the report two-sided. Let the above page
  292. layout be used for the odd (right-side) pages, and the
  293. following for the even (left-side) pages:
  294. \begin{fblock}
  295. \noindent\makebox[\textwidth]{\bfseries The performance of new
  296. graduates\hfill}
  297. \noindent\makebox[\textwidth]{\hrulefill}\\[\baselineskip]
  298. \noindent\makebox[\textwidth]{\hfill page body\hfill}\\[\baselineskip]
  299. \noindent\makebox[\textwidth]{\hrulefill}
  300. \noindent\makebox[\textwidth]{4\phantom{To: Dean A. Smith}\hfill
  301. From: K. Grant\hfill \phantom{4}To: Dean A. Smith}
  302. \end{fblock}
  303. \noindent where ``4'' is the page number.
  304. Here are the commands:
  305. \begin{verbatim}
  306. \fancyhead{} % clear all fields
  307. \fancyhead[RO,LE]{\bfseries The performance of new graduates}
  308. \fancyfoot[LE,RO]{\thepage}
  309. \fancyfoot[LO,CE]{From: K. Grant}
  310. \fancyfoot[CO,RE]{To: Dean A. Smith}
  311. \renewcommand{\headrulewidth}{0.4pt}
  312. \renewcommand{\footrulewidth}{0.4pt}
  313. \end{verbatim}
  314. \CmdIndex{fancyhead}
  315. \CmdIndex{fancyfoot}
  316. We use the more general commands \Cmd{fancyhead} and \Cmd{fancyfoot}.
  317. These have an additional parameter between square brackets that specifies
  318. for which pages and/or parts of the header/footer they apply. The first
  319. \Cmd{fancyhead} command omits this parameter, and thus applies to all
  320. header fields. In general this is only useful to get rid of the defaults or
  321. a previous definition, as is done here. The selectors that can be used
  322. between the square brackets are given in figure~\ref{fig:sel}. Selectors can be combined so
  323. \Cmd{fancyhead[LE,RO]\{text\}}
  324. will define the field for both the left header on even pages and the right
  325. header on odd pages. If you don't give an E or O the definition applies to
  326. both. Similar for LRC. So the use of \Cmd{lhead} in the previous section
  327. is just an abbreviation for \Cmd{fancyhead[L]}.
  328. The selectors may be given as uppercase or lowercase letters.
  329. \CmdIndex{fancyhf}
  330. There is also a more general command \Cmd{fancyhf} that you can use to
  331. combine the specifications for headers and footers. This allows additional
  332. selectors H (header) and F (footer). In fact \Cmd{fancyhead} and
  333. \Cmd{fancyfoot} are just \Cmd{fancyhf} with H and F prespecified.
  334. \begin{figure}[tb]
  335. \begin{center}
  336. \leavevmode
  337. \begin{tabular}{|l|l|}
  338. \hline
  339. E & Even page \\
  340. O & Odd page \\
  341. \hline
  342. L & Left field \\
  343. C & Center field \\
  344. R & Right field \\
  345. \hline
  346. H & Header \\
  347. F & Footer \\
  348. \hline
  349. %% T & float at Top \\
  350. %% B & float at Bottom \\
  351. %% F & Float page \\
  352. %% \hline
  353. \end{tabular}
  354. \end{center}
  355. \caption{Selectors}
  356. \label{fig:sel}
  357. \end{figure}
  358. Again, you may
  359. use \Cmd{thispagestyle\{plain\}} for a simple page layout for
  360. page~1.
  361. \section{Redefining \texttt{plain} style}
  362. Some \latex/ commands, like \Cmd{chapter}, use the \Cmd{thispagestyle}
  363. command to automatically switch to the \texttt{plain} page style, thus
  364. ignoring the page style currently in effect.
  365. To customize even such pages you must redefine the \texttt{plain}
  366. pagestyle. As we indicated before you could do this by defining the
  367. \Cmd{ps@plain} command, but \textsf{fancyhdr} gives you an easier way
  368. with the \Cmd{fancypagestyle} command. This command can be used to
  369. redefine existing pagestyles (like \texttt{plain}) or to define new ones,
  370. e.g. if part of your document is to use a different pagestyle. This command
  371. has two parameters: one is the name of the pagestyle to be defined, the
  372. second consists of commands that change the headers and/or footers, i.e.
  373. \verb|fancyhead| etc. Also allowed are changes to \Cmd{headrulewidth} and
  374. \Cmd{footrulewidth}.
  375. As an example, let us
  376. redefine the \texttt{plain} style for the report in Section~\ref{two-sided} by
  377. making the page number bold.
  378. \PSindex{plain}
  379. \begin{verbatim}
  380. \fancypagestyle{plain}{%
  381. \fancyhf{} % clear all header and footer fields
  382. \fancyfoot[C]{\bfseries \thepage} % except the center
  383. \renewcommand{\headrulewidth}{0pt}
  384. \renewcommand{\footrulewidth}{0pt}}
  385. \end{verbatim}
  386. \section{The default layout}\label{default}
  387. Let us use the \verb|book.cls| documentclass and the default settings for
  388. \textsf{fancyhdr}; so we only issue the commands
  389. \begin{verbatim}
  390. \usepackage{fancyhdr}
  391. \pagestyle{fancy}
  392. \end{verbatim}
  393. and let \textsf{fancyhdr} take care of everything. On the
  394. pages where new chapters start, we get a centered page number in
  395. the footer; there is no header, and there are no decorative lines.
  396. On an even page, we get the layout:
  397. \begin{fblock}
  398. \noindent\makebox[\textwidth]{\sl 1.2 EVALUATION\hfill
  399. CHAPTER 1. INTRODUCTION}
  400. \noindent\makebox[\textwidth]{\hrulefill}\\[\baselineskip]
  401. \noindent\makebox[\textwidth]{\hfill page body\hfill}\\[\baselineskip]
  402. \noindent\makebox[\textwidth]{\hrulefill}
  403. \noindent\makebox[\textwidth]{\hfill4\hfill}
  404. \end{fblock}
  405. On an odd page, we get the layout:
  406. \begin{fblock}
  407. \noindent\makebox[\textwidth]{\sl CHAPTER 1. INTRODUCTION\hfill
  408. 1.2 EVALUATION}
  409. \noindent\makebox[\textwidth]{\hrulefill}\\[\baselineskip]
  410. \noindent\makebox[\textwidth]{\hfill page body\hfill}\\[\baselineskip]
  411. \noindent\makebox[\textwidth]{\hrulefill}
  412. \noindent\makebox[\textwidth]{\hfill
  413. 3\hfill }
  414. \end{fblock}
  415. \noindent where the header text is slanted uppercase.
  416. This default layout is produced by the following commands:
  417. \CmdIndex{rightmark}
  418. \CmdIndex{leftmark}
  419. \begin{verbatim}
  420. \fancyhead[LE,RO]{\slshape \rightmark}
  421. \fancyhead[LO,RE]{\slshape \leftmark}
  422. \fancyfoot[C]{\thepage}
  423. \end{verbatim}
  424. The following settings are used for the decorative lines:
  425. \begin{tabbing}
  426. \CmdIndex{headrulewidth}
  427. \noindent \Cmd{headrulewidth}\qquad \qquad \qquad \=0.4\=pt\\
  428. \CmdIndex{footrulewidth}
  429. \Cmd{footrulewidth}\>0\>pt
  430. \end{tabbing}
  431. The header text is turned into all uppercase in \verb|book.cls|.
  432. \section{The scoop on \latex/'s marks}\label{sec:custom}
  433. Usually, for documents of class \verb|book| and \verb|report|, you may
  434. want to use chapter and section information in the headings (chapter only
  435. for one-sided printing), and for documents of class \verb|article|,
  436. section and subsection information (section only for one-sided
  437. printing). \latex/ uses a marker mechanism to remember the chapter and
  438. section (section and subsection) information for a page; this is
  439. discussed in detail in the
  440. \latex/ \emph{Companion}, Section 4.3.1.
  441. There are two ways you can use and change the higher- and lower-level
  442. sectioning information available to you. The macros:
  443. \CmdIndex{rightmark}
  444. \CmdIndex{leftmark}
  445. \Cmd{leftmark}
  446. (higher-level) and \Cmd{rightmark} (lower-level) contain the information
  447. processed by \latex/, and you can use them directly as shown in
  448. Section~\ref{default}.
  449. The \Cmd{leftmark} contains the Left argument of the \emph{Last}
  450. \Cmd{markboth} on the page, the \Cmd{rightmark} contains the Right
  451. argument of the \emph{fiRst} \Cmd{markboth} or the only argument of the
  452. \emph{fiRst} \Cmd{markright} on the page. If no marks are present on a
  453. page they are ``inherited'' from the previous page.
  454. You can influence how chapter, section, and subsection
  455. information (only two of them!) is displayed by redefining the
  456. \CmdIndex{chaptermark}
  457. \Cmd{chaptermark},
  458. \CmdIndex{sectionmark}
  459. \CmdIndex{subsectionmark}
  460. \Cmd{sectionmark}, and \Cmd{subsectionmark} commands\footnote{There are
  461. similar commands for \texttt{paragraph} and \texttt{subparagraph} but
  462. they are seldom used.}. You must put the redefinition
  463. after the first call of \Cmd{pagestyle\{fancy\}} as this
  464. sets up the defaults.
  465. Let us illustrate this with chapter info. It is made up of three parts:
  466. \begin{itemize}
  467. \CmdIndex{thechapter}
  468. \item the number (say, 2), displayed by the macro \Cmd{thechapter}
  469. \item the name (in English, Chapter), displayed by the macro
  470. \CmdIndex{chaptername}
  471. \Cmd{chaptername}
  472. \item the title, contained in the argument of
  473. \Cmd{chaptermark}.
  474. \end{itemize}
  475. Figure~\ref{fig:markers} shows some variants for ``Chapter 2.\ Do it now''
  476. (the last example is appropriate in some non-English languages). The \%
  477. signs at the end of the lines are to prevent unwanted space. Normally you
  478. would continue the lines and remove these \% signs\footnote{the \texttt{\bs
  479. MakeUppercase} command is used in \LaTeXe{} to generate uppercase text,
  480. while in \LaTeX{} 2.09 \Cmd{uppercase} is used. The difference is
  481. that \Cmd{MakeUppercase} also deals with non-ASCII letters.
  482. Fancyhdr defines \Cmd{MakeUppercase} to be an alias for
  483. \Cmd{uppercase} if it isn't defined.}.
  484. \begin{figure}[tb]
  485. \CmdIndex{chaptermark}
  486. \CmdIndex{uppercase}
  487. \CmdIndex{MakeUppercase}
  488. \setlength{\columnsep}{20pt}\small
  489. \begin{multicols}{2}
  490. \noindent Code:\\
  491. \mbox{}\\
  492. \verb|\renewcommand{\chaptermark}[1]{%|\\
  493. \verb| \markboth{\chaptername|\\
  494. \verb| \ \thechapter.\ #1}{}}|\\
  495. \mbox{}\\
  496. \verb|\renewcommand{\chaptermark}[1]{%|\\
  497. \verb| \markboth{\MakeUppercase{%|\\
  498. \verb| \chaptername}\ \thechapter.%|\\
  499. \verb| \ #1}{}}|\\
  500. \mbox{}\\
  501. \verb|\renewcommand{\chaptermark}[1]{%|\\
  502. \verb| \markboth{\MakeUppercase{%|\\
  503. \verb| \chaptername\ \thechapter.%|\\
  504. \verb| \ #1}}{}}|\\
  505. \mbox{}\\
  506. \verb|\renewcommand{\chaptermark}[1]{%|\\
  507. \verb| \markboth{#1}{}}|\\
  508. \mbox{}\\
  509. \verb|\renewcommand{\chaptermark}[1]{%|\\
  510. \verb| \markboth{\thechapter.\ #1}{}}|\\
  511. \mbox{}\\
  512. \verb|\renewcommand{\chaptermark}[1]{%|\\
  513. \verb| \markboth{\thechapter.%|\\
  514. \verb| \ \chaptername.\ #1}{}}|\\
  515. Prints:\\
  516. \mbox{}\\
  517. Chapter 2.\ Do it now\\
  518. \mbox{}\\
  519. \mbox{}\\
  520. \mbox{}\\
  521. CHAPTER 2.\ Do it now\\
  522. \mbox{}\\
  523. \mbox{}\\
  524. \mbox{}\\
  525. \mbox{}\\
  526. CHAPTER 2.\ DO IT NOW\\
  527. \mbox{}\\
  528. \mbox{}\\
  529. \mbox{}\\
  530. \mbox{}\\
  531. Do it now\\
  532. \mbox{}\\
  533. \mbox{}\\
  534. 2.\ Do it now\\
  535. \mbox{}\\
  536. \mbox{}\\
  537. 2.\ Chapter.\ Do it now\\
  538. \mbox{}\\
  539. \end{multicols}
  540. \caption{Marker variants}\label{fig:markers}
  541. \end{figure}
  542. For the lower-level sectioning information, do the same with
  543. \Cmd{markright}.
  544. So if ``Section 2.2.\ First steps'' is the current section,
  545. then
  546. \begin{verbatim}
  547. \renewcommand{\sectionmark}[1]{\markright{\thesection.\ #1}}
  548. \end{verbatim}
  549. will give
  550. ``2.2.\ First steps''
  551. Redefining the \Cmd{chaptermark} and \Cmd{sectionmark} commands may
  552. not eliminate all uppercaseness. E.g.\ the bibliography will have a title
  553. \CmdIndex{uppercase}
  554. \index{BIBLIOGRAPHY}
  555. of \textsc{bibliography} in the header, as the \Cmd{MakeUppercase} is
  556. explicitly given in the definition of \Cmd{thebibliography}. Similar for
  557. \index{INDEX}
  558. \textsc{index} etc.\ If you don't want to redefine these commands, you can
  559. use the \Cmd{nouppercase} command that \textsf{fancyhdr} makes available in the header
  560. and footer fields. Note that this may screw other things, like uppercase
  561. roman numerals in your headers, so it should be used with care. Essentially
  562. this command typesets its argument in an environment where
  563. \Cmd{MakeUppercase} and \Cmd{uppercase} are changed into do-nothing operations.
  564. \begin{verbatim}
  565. \lhead{\nouppercase{\rightmark}}
  566. \rhead{\nouppercase{\leftmark}}
  567. \end{verbatim}
  568. It should be noted that the \latex/ marking mechanism works fine with
  569. chapters (which always start on a new page) and sections (which are
  570. reasonably long). It does not work quite as well with short sections and
  571. subsections. This is a problem with \latex/, not with
  572. \textsf{fancyhdr}.
  573. As an example let's take a page layout where the leftmarks are generated by
  574. the sections and the rightmarks by the subsections (as is default in the
  575. \texttt{article} class). Take a page with some short sections, e.g.
  576. \begin{samepage}
  577. \noindent Section 1.\\
  578. subsection 1.1\\
  579. subsection 1.2\\
  580. Section 2.
  581. \end{samepage}
  582. As the leftmark contains the \emph{last} mark of the page it will be
  583. ``Section 2.'', and the rightmark will be ``subsection 1.1'' as it will be
  584. the \emph{first} mark of the page. So the page header info will combine
  585. section 2 with subsection 1.1 which isn't very nice.
  586. The best you can do in these cases is use only the \Cmd{rightmark}s and
  587. redefine \Cmd{sectionmark} accordingly. A \latex/ command
  588. \CmdIndex{firstleftmark}
  589. \Cmd{firstleftmark} would also be a nice addition (see the
  590. \texttt{extramarks} package in section~\ref{sec:xmarks}).
  591. Another problem with the marks in the standard \latex/ classes is that the
  592. higher level sectioning commands (e.g. \Cmd{chapter}) call
  593. \Cmd{markboth} with an empty right argument. This means that on the first
  594. page of a chapter (or a section in article style) the \Cmd{rightmark}
  595. will be empty. If this is a problem you must manually insert extra
  596. \Cmd{markright} commands or redefine the \Cmd{chaptermark}
  597. (\Cmd{sectionmark}) commands to issue a \Cmd{markboth} command with
  598. two decent parameters.
  599. As a final remark you should also note that the \verb|*| forms of the
  600. \Cmd{chapter} etc.\ commands do \emph{not} call the mark commands. So if
  601. you want your preface to set the header info but not be numbered nor be put in
  602. the table of contents, you must issue the \Cmd{markboth} command
  603. yourself, e.g.
  604. \begin{verbatim}
  605. \chapter*{Preface\markboth{Preface}{}}
  606. \end{verbatim}
  607. Entering the \Cmd{markboth} command inside the \Cmd{chapter*} insures
  608. that the mark will not be separated from the title by a page break. Of
  609. course with \Cmd{chapter*} this wouldn't be a problem if you put the mark
  610. command after the chapter title, as the \Cmd{chapter*} command starts at a
  611. new page. However with a \Cmd{section*} it could be dangerous to say:
  612. \begin{verbatim}
  613. \section*{Preface}
  614. \markboth{Preface}{}
  615. \end{verbatim}
  616. as a page break may occur between the two commands.
  617. \section{Dictionary style headers}
  618. \index{dictionary}
  619. \index{concordance}
  620. Dictionaries and concordances usually have a header containing the first
  621. word defined on the page or both the first and the last words. This can
  622. easily be accomplished with \textsf{fancyhdr} and \latex/'s
  623. \texttt{mark} mechanism. Of course if you use the marks for dictionary
  624. style headers, you cannot use them for chapter and section information, so
  625. if there are also chapters and sections present, you must redefine the
  626. \Cmd{chaptermark} and \Cmd{sectionmark} to make them harmless:
  627. \begin{verbatim}
  628. \renewcommand{\chaptermark}[1]{}
  629. \renewcommand{\sectionmark}[1]{}
  630. \end{verbatim}
  631. Now you do a \Cmd{markboth\{\#1\}\{\#1\}} for each dictionary or concordance
  632. entry \verb|#1| and use \Cmd{rightmark} for the first entry defined on
  633. the page and \Cmd{leftmark} for the last one.
  634. If you want to use a header entry of the form \textsf{firstword--lastword}
  635. it would be nice if this would be reduced to just the form
  636. \textsf{firstword} if both are the same. This could happen if there is just
  637. one entry on the page. In this case a test must be made to check if the
  638. marks are the same. However, \tex/'s marks are strange beasts, which
  639. cannot be compared out of the box with the plain \tex/ \Cmd{if} commands.
  640. \TTindex{ifthen}
  641. Fortunately the \texttt{ifthen} package works well:
  642. \begin{verbatim}
  643. \newcommand{\mymarks}{
  644. \ifthenelse{\equal{\leftmark}{\rightmark}}
  645. {\rightmark} % if equal
  646. {\rightmark--\leftmark}} % if not equal
  647. \fancyhead[LE,RO]{\mymarks}
  648. \fancyhead[LO,RE]{\thepage}
  649. \end{verbatim}
  650. Dictionaries are often done with two columns. Unfortunately there is a bug
  651. in \latex/'s twocolumn option which causes some marks to be lost. If you
  652. use the package in figure~\ref{fig:fixmarks}, this will be
  653. solved\footnote{This is a simplification of the actual code. Get the real
  654. package from the CTAN sites.}\footnote{The
  655. \texttt{multicol} package uses a similar technique.}.
  656. \begin{figure}[tb]
  657. \small
  658. \TTindex{fixmarks.sty}
  659. \begin{verbatim}
  660. % fixmarks.sty:
  661. % Patch LaTeX's output routine to handle marks correctly with two columns.
  662. % Joe Pallas <[email protected]>
  663. % Corrected by Piet van Oostrum <[email protected]> on Feb 5, 1993, Oct 5, 1994
  664. \def\@outputdblcol{\if@firstcolumn \global\@firstcolumnfalse
  665. % Remember the marks from the first column
  666. \global\setbox\@leftcolumn\copy\@outputbox
  667. \splitmaxdepth=\maxdimen \cbaddness=10000
  668. \setbox\@outputbox\vsplit\@outputbox to\maxdimen
  669. \xdef\@firstcoltopmark{\topmark}%
  670. \xdef\@firstcolfirstmark{\splitfirstmark}%
  671. \ifx\@firstcolfirstmark\empty\global\let\@setmarks\relax\else
  672. \gdef\@setmarks{\let\firstmark\@firstcolfirstmark
  673. \let\topmark\@firstcoltopmark}%
  674. \fi
  675. % End of change
  676. \else \global\@firstcolumntrue
  677. \setbox\@outputbox\vbox{\hbox to\textwidth{\hbox to\columnwidth
  678. {\box\@leftcolumn \hss}\hfil \vrule width\columnseprule\hfil
  679. \hbox to\columnwidth{\box\@outputbox \hss}}}\@combinedblfloats
  680. % Override current first and top with those of first column if necessary
  681. \@setmarks
  682. % End of change
  683. \@outputpage \begingroup \@dblfloatplacement \@startdblcolumn
  684. \@whilesw\if@fcolmade \fi{\@outputpage\@startdblcolumn}\endgroup
  685. \fi}
  686. \end{verbatim}
  687. \caption{Fix marks in twocolumn style}
  688. \label{fig:fixmarks}
  689. \end{figure}
  690. \section{Fancy layouts}
  691. \index{multi-line}
  692. You can make a multi-line field with the \Cmd{\bs} command. It is also
  693. possible to put extra space in a field with the \Cmd{vspace} command.
  694. Note that if you do this you will probably have to increase the height of
  695. \CmdIndex{headheight}
  696. \CmdIndex{footskip}
  697. the header (\Cmd{headheight}) and/or of the footer (\Cmd{footskip}),
  698. \index{Overfull \verb+\vbox+ \ldots}
  699. otherwise you may get error messages ``Overfull \Cmd{vbox} \ldots has
  700. occurred while \Cmd{output} is active''%
  701. \footnote{If you use \texttt{11pt} or \texttt{12pt} you will probably also
  702. have to do this, because \LaTeX's defaults are quite small}.
  703. See Section 4.1 of the \latex/ \emph{Companion} for detail.
  704. For instance, the following code will place the section title and the
  705. subsection title of an article in two lines in the upper right hand
  706. corner:
  707. \begin{verbatim}
  708. \documentclass{article}
  709. \usepackage{fancyhdr}
  710. \pagestyle{fancy}
  711. \addtolength{\headheight}{\baselineskip}
  712. \renewcommand{\sectionmark}[1]{\markboth{#1}{}}
  713. \renewcommand{\subsectionmark}[1]{\markright{#1}}
  714. \rhead{\leftmark\\\rightmark}
  715. \end{verbatim}
  716. You can also customize the decorative lines. You can make the decorative
  717. line in the header quite thick with
  718. \CmdIndex{headrulewidth}
  719. \begin{verbatim}
  720. \renewcommand{\headrulewidth}{0.6pt}
  721. \end{verbatim}
  722. or you can make the decorative line in the footer disappear with
  723. \CmdIndex{footrulewidth}
  724. \begin{verbatim}
  725. \renewcommand{\footrulewidth}{0pt}
  726. \end{verbatim}
  727. The decorative lines, themselves, are defined in the two macros
  728. \Cmd{headrule} and \Cmd{footrule}. For instance,
  729. if you want a dotted line rather than a solid line in the header,
  730. redefine the command \Cmd{headrule}:
  731. \begin{verbatim}
  732. \renewcommand{\headrule}{\vbox to 0pt{\hbox
  733. to\headwidth{\dotfill}\vss}}
  734. \end{verbatim}
  735. \CmdIndex{footruleskip}
  736. There is one additional parameter that you can set: \Cmd{footruleskip}. It
  737. defines the distance between the decorative line in the footer and the top
  738. of the footer text line. By default it is set to 30\% of the normal line
  739. distance. You may want to adjust it if you use unusally large or small
  740. fonts in the footer. Change it with \Cmd{renewcommand}.
  741. \section{Two book examples}
  742. The following definitions give an approximation of the style
  743. used in L. Lamport's \latex/ book.
  744. Lamport's header overhangs the outside margin. This is done as follows.
  745. The width of headers and footers is \Cmd{headwidth}, which by default
  746. equals the width of the text: \Cmd{textwidth}. You can make the width
  747. \CmdIndex{headwidth}
  748. wider (or narrower) by redefining \Cmd{headwidth} with the
  749. \Cmd{setlength} and \Cmd{addtolength} commands.
  750. To overhang the outside margin where the marginal notes are
  751. \CmdIndex{marginparsep}
  752. \CmdIndex{marginparwidth}
  753. printed, add both \Cmd{marginparsep} and \Cmd{marginparwidth} to
  754. \Cmd{headwidth} with the commands:
  755. \begin{verbatim}
  756. \addtolength{\headwidth}{\marginparsep}
  757. \addtolength{\headwidth}{\marginparwidth}
  758. \end{verbatim}
  759. \begin{flushleft}
  760. You must issue these commands \emph{after} the first
  761. \Cmd{pagestyle\{fancy\}} command as this
  762. will establish the default for \Cmd{headwidth}\footnote{In version 2 of
  763. \textsf{fancyhdr} it is usually safe to do it before, but it is still
  764. advised to do after.}.
  765. \end{flushleft}
  766. And now a complete definition of Lamport's book style:
  767. \begin{verbatim}
  768. \documentclass{book}
  769. \usepackage{fancyhdr}
  770. \pagestyle{fancy}
  771. \addtolength{\headwidth}{\marginparsep}
  772. \addtolength{\headwidth}{\marginparwidth}
  773. \renewcommand{\chaptermark}[1]{\markboth{#1}{}}
  774. \renewcommand{\sectionmark}[1]{\markright{\thesection\ #1}}
  775. \fancyhf{}
  776. \fancyhead[LE,RO]{\bfseries\thepage}
  777. \fancyhead[LO]{\bfseries\rightmark}
  778. \fancyhead[RE]{\bfseries\leftmark}
  779. \fancypagestyle{plain}{%
  780. \fancyhead{} % get rid of headers
  781. \renewcommand{\headrulewidth}{0pt} % and the line
  782. }
  783. \end{verbatim}
  784. \PSindex{plain}
  785. Notice that the \Cmd{chaptermark} and \Cmd{sectionmark} commands have
  786. been redefined to eliminate the chapter numbers and the uppercaseness.
  787. For the second example, we take the \amslatex/ book.% \cite{gG93B}.
  788. % George Gratzer, Math into LaTeX, An Introduction to LaTeX and AMS-LaTeX,
  789. % Birkhauser Boston, ISBN 0-8176-3805-9
  790. Chapter pages have no headers or footers. So we declare
  791. \begin{verbatim}
  792. \thispagestyle{empty}
  793. \end{verbatim}
  794. for every chapter page, and we do not need to redefine plain.
  795. Chapter and section titles appear in the form: 2.\ DO IT NOW, so we have
  796. to redefine \Cmd{chaptermark} and \Cmd{sectionmark} as follows (see
  797. Section~\ref{sec:custom}):
  798. \begin{verbatim}
  799. \renewcommand{\chaptermark}[1]%
  800. {\markboth{\MakeUppercase{\thechapter.\ #1}}{}}
  801. \renewcommand{\sectionmark}[1]%
  802. {\markright{\MakeUppercase{\thesection.\ #1}}}
  803. \end{verbatim}
  804. In an even-header, the page number is printed as the LeftHeader and
  805. the chapter info as the RightHeader; in an odd-header, the section info
  806. is printed as the LeftHeader and the page number as the RightHeader. The
  807. CenteredHeaders are empty. There are no footers.
  808. There is a decorative line in the header. It is 0.5pt wide, so we need
  809. the commands:
  810. \begin{verbatim}
  811. \renewcommand{\headrulewidth}{0.5pt}
  812. \renewcommand{\footrulewidth}{0pt}
  813. \end{verbatim}
  814. The font used in the headers is 9 pt bold Helvetica. The \PSNFSS\ system
  815. by Sebastian Rahtz uses the short (Karl Berry) name \verb|phv| for
  816. Helvetica, so this font is selected with the commands:
  817. \begin{verbatim}
  818. \fontfamily{phv}\fontseries{b}\fontsize{9}{11}\selectfont
  819. \end{verbatim}
  820. (See Sections 7.6.1 and 11.9.1 of the \latex/ \emph{Companion}.)
  821. Let us define a shorthand for this:
  822. \begin{verbatim}
  823. \newcommand{\helv}{%
  824. \fontfamily{phv}\fontseries{b}\fontsize{9}{11}\selectfont}
  825. \end{verbatim}
  826. Now we are ready for the page layout:
  827. \begin{verbatim}
  828. \documentclass{book}
  829. \usepackage{fancyhdr}
  830. \pagestyle{fancy}
  831. \renewcommand{\chaptermark}[1]%
  832. {\markboth{\MakeUppercase{\thechapter.\ #1}}{}}
  833. \renewcommand{\sectionmark}[1]%
  834. {\markright{\MakeUppercase{\thesection.\ #1}}}
  835. \renewcommand{\headrulewidth}{0.5pt}
  836. \renewcommand{\footrulewidth}{0pt}
  837. \newcommand{\helv}{%
  838. \fontfamily{phv}\fontseries{b}\fontsize{9}{11}\selectfont}
  839. \fancyhf{}
  840. \fancyhead[LE,RO]{\helv \thepage}
  841. \fancyhead[LO]{\helv \rightmark}
  842. \fancyhead[RE]{\helv \leftmark}
  843. \end{verbatim}
  844. \section{Special page layout for float pages}
  845. \label{sec:float}
  846. \index{float page}
  847. Some people want to have a special layout for float pages (pages only
  848. containing floats). As these pages
  849. are generated autonomically by \latex/, the user doesn't have any control
  850. over them. There is no \Cmd{thispagestyle} for float pages and any change
  851. of the page style will at least also affect the page before the float page.
  852. With \textsf{fancyhdr}, however, you can specify in each of
  853. the header- or footer fields
  854. \Cmd{iffloatpage\{}value for float page\verb|}{|value for other pages\verb|}|
  855. You can even use this to get rid of the decorative line on float pages only
  856. by defining:
  857. \begin{verbatim}
  858. \renewcommand{\headrulewidth}{\iffloatpage{0pt}{0.4pt}}
  859. \end{verbatim}
  860. Sometimes you may want to change the layout also for pages that contain a
  861. float on the top of the page or a float on the bottom of the page.
  862. \textsf{fancyhdr} gives you the commands \Cmd{iftopfloat} and \Cmd{ifbotfloat}
  863. similar to
  864. \Cmd{iffloatpage}.
  865. Note: Marks in floats will not be visible in \latex/'s output routine, so
  866. it is not useful to put marks in floats. So there is currently no way to
  867. let a float (e.g.\ a figure caption) influence the page header or footer.
  868. \section{Those blank pages}
  869. \label{sec:blank}
  870. In the \texttt{book} class when the \texttt{openany} option is not given or
  871. in the \texttt{report} class when the \texttt{openright} option is given,
  872. chapters start at odd-numbered pages, half of the time causing a blank page
  873. to be inserted. Some people prefer this page to be completely empty, i.e.\
  874. without headers and footers. This cannot be done with \Cmd{thispagestyle}
  875. as this command would have to be issued on the \emph{previous} page. There
  876. is, however, no magic necessary to get this done:
  877. \PSindex{empty}
  878. \CmdIndex{clearpage}
  879. \CmdIndex{cleardoublepage}
  880. \begin{verbatim}
  881. \clearpage{\pagestyle{empty}\cleardoublepage}
  882. \end{verbatim}
  883. As the \Cmd{pagestyle\{empty\}} is enclosed in a group it only affects the
  884. page that may be generated by the \Cmd{cleardoublepage}. You can of
  885. course put the above in a private command. If you want to have this done
  886. automatically at each chapter start or when you want some other text on the
  887. page then you must redefine the \Cmd{cleardoublepage} command.
  888. \index{blank page}
  889. \begin{verbatim}
  890. \makeatletter
  891. \def\cleardoublepage{\clearpage\if@twoside \ifodd\c@page\else
  892. \hbox{}
  893. \vspace*{\fill}
  894. \begin{center}
  895. This page intentionally contains only this sentence.
  896. \end{center}
  897. \vspace{\fill}
  898. \thispagestyle{empty}
  899. \newpage
  900. \if@twocolumn\hbox{}\newpage\fi\fi\fi}
  901. \makeatother
  902. \end{verbatim}
  903. \section{\textsf{N} of \textsf{M} style page numbers}
  904. \label{sec:nofm}
  905. Some document writers prefer the pages to be numbered as \textsf{n} of
  906. \textsf{m} where \textsf{m} is the number of pages in the document. There
  907. \TTindex{nofm.sty}
  908. is a package \texttt{nofm.sty} available, but some versions of it are
  909. defective, and most don't work with fancyhdr because they take over the
  910. \TTindex{lastpage.sty}
  911. complete page layout. For \LaTeXe{} there is a package \texttt{lastpage}
  912. available which you can use with \textsf{fancyhdr} as follows:
  913. \begin{verbatim}
  914. \usepackage{lastpage}
  915. ...
  916. \cfoot{\thepage\ of \pageref{LastPage}}
  917. \end{verbatim}
  918. If you are still using \latex/2.09 and you are not able to switch to
  919. \LaTeXe{} you can use the \latex/2.09 compatible \texttt{lastpage209.sty}.
  920. which is defined as
  921. follows:
  922. \begin{verbatim}
  923. \let\origenddocument=\enddocument
  924. \def\enddocument{\clearpage\if@filesw
  925. {\addtocounter{page}{-1} \immediate\write\@mainaux
  926. {\string\newlabel{LastPage}{{}{\thepage}}}}\origenddocument}
  927. \end{verbatim}
  928. The value of the \texttt{LastPage} label can be used to
  929. make different headers or footers on the last page of a document. E.g.\ if
  930. you want the footer of every odd page, except if it is the last one, to
  931. contain the text ``please turn over'', this can be done as
  932. follows\footnote{This requires a reasonably recent version of the
  933. \texttt{ifthen} package.}:
  934. \begin{verbatim}
  935. \usepackage{lastpage}
  936. \usepackage{ifthen}
  937. ...
  938. \rfoot{\ifthenelse{\isodd{\value{page}} \and \not
  939. \value{page}=\pageref{LastPage}{please turn over}{}}
  940. \end{verbatim}
  941. \section{Chapter or section related page numbers}
  942. In technical documentation very often page numbers are used of the form
  943. 2-10 where the first number is the chapter number and the second is the
  944. pagenumber relative to the chapter. Sometimes section is used rather than
  945. chapter. The package \textsf{chappg} can be used to get this format. If you
  946. want to change the layout e.g. use a dot rather than a dash, or a section
  947. rather than a chapter, you should make a private copy and edit it.
  948. This package redefines \Cmd{thepage} as
  949. \Cmd{arabic\{chapter\}-}\Cmd{arabic\{page\}}.
  950. Unfortunately this gives numbers rather than letters for appendices. A
  951. better definition would be \Cmd{thechapter-}\Cmd{arabic\{page\}} but you can
  952. give this definition yourself after the \Cmd{usepackage\{chappg\}} command.
  953. What the package also does is reset the page number to 1 at the beginning
  954. of each chapter.
  955. There is a fundamental difference between the page numbering of the style
  956. ``\emph{m} of \emph{n}'' as described in the previous section and the
  957. current one. The \emph{m} of \emph{n} style is only used in the page header
  958. or footer, but not in the table of contents, index, or references like
  959. ``\emph{See page \emph{xx}}''. Therefore it does not change the command
  960. \Cmd{thepage}. The page numbering style ``2-10'', however should be used
  961. in all references to the page number, therefore it must be done by redefining
  962. \Cmd{thepage}.
  963. \section{When to change the headers and footers?}
  964. \label{sec:change}
  965. Sometimes you want to change the header or footer layout in the course of a
  966. document. Some of these changes can be accomplished by using the mark
  967. mechanism as may be seen in section~\ref{sec:custom} and \ref{sec:xmarks}.
  968. However, sometimes we want a more drastic change, e.g to change the page
  969. numbering from roman to arabic (with \Cmd{pagenumbering}), to change one of
  970. \index{page style!changes}
  971. the \textsf{fancyhdr} fields or to change to another page style.
  972. Sometimes you may be surprised to find the change to occur too early. In
  973. general the above mentioned changes take effect immediately, i.e.\ on the
  974. page that is currently being built. If you want the change to take effect
  975. at the next page you must make sure that the current page is finished. In
  976. \CmdIndex{clearpage}
  977. most cases this can be done by issuing a \Cmd{clearpage} command before
  978. any of the above mentioned changes. If this is not possible you can use the
  979. \TTindex{afterpage.sty}
  980. \texttt{afterpage} package with:\\
  981. \Cmd{afterpage\{}\Cmd{lhead\{new value\}\}} or
  982. \CmdIndex{pagenumbering}
  983. \Cmd{afterpage\{}\Cmd{pagenumbering\{roman\}}. You cannot use \Cmd{afterpage}
  984. to change the \Cmd{pagestyle} as the commands issued by \Cmd{afterpage}
  985. are local in a group, and the \Cmd{pagestyle} command makes only local
  986. changes. The \Cmd{pagenumbering} and the \textsf{fancyhdr} commands
  987. make global changes so they will work, as will the \Cmd{thispagestyle}
  988. command.
  989. It should be noted that although the \textsf{fancyhdr} commands like
  990. \Cmd{fancyhead} take effect immediately, this does not mean that any
  991. ``variables'' used in these commands get the value they have at the place
  992. where these commands are given. E.g.\ if \Cmd{fancyfoot[C]\{}\Cmd{thepage\}} is given
  993. the page number that will be inserted in the footer is not the page number
  994. of the page where this command is given, but rather the page number of
  995. the actual page where the footer is constructed. Of course for the page
  996. number this is what you expect, but it is also true for other commands.
  997. So if you have a book where each chapter is written by a different author
  998. and you want the name of the author in the lower left-hand corner you can
  999. use the following commands:
  1000. \begin{verbatim}
  1001. \newcommand{\TheAuthor}{}
  1002. \newcommand{\Author}[1]{\renewcommand{\TheAuthor}{#1}}
  1003. \lfoot{\TheAuthor}
  1004. \end{verbatim}
  1005. \noindent and start each chapter with the command
  1006. \Cmd{Author\{Real Name\}}.
  1007. If however, the author name would be changed before a page is
  1008. completed the wrong author could come in the footer. This would be the case
  1009. if you gave the above command \emph{before} the \Cmd{chapter} command
  1010. rather than after it.
  1011. Another source of problems is the fact that \tex/'s output routine processes
  1012. commands ahead, so it may already have processed some commands that produce
  1013. text that will appear on the next page. See the next section for an example.
  1014. \section{Headers and footers induced by the text}
  1015. \label{sec:xmarks}
  1016. We have seen how we can use \LaTeX's marks to get information from the
  1017. document contents to the headers and footers. The marks mechanism is the
  1018. only reliable mechanism that you can use to get changing information to the
  1019. headers or footers. This is because \latex/ may be processing your document
  1020. ahead before deciding to break the page.
  1021. Sometimes the two marks that \latex/ offers are not enough. An example is
  1022. the following:
  1023. \begin{quote}
  1024. If a solution to an exercise goes across a page break, then I would like
  1025. to have ``(Continued on next page\ldots)'' at the bottom of the
  1026. \index{Continued\ldots}
  1027. first page and ``(Continued\ldots)'' at the top in the margin of the next page.
  1028. \end{quote}
  1029. You cannot use \latex/'s mark mechanisms for this if you also want to use
  1030. chapter and section information.
  1031. The code from figure~\ref{fig:xmarks} constitutes a package that gives you
  1032. two extra marks that can be used in this situation\footnote{After I made
  1033. this package I discovered a package \texttt{secret.sty} that does a
  1034. similar thing to mark confidential paragraphs if they cross a page
  1035. boundary. It does it, however, by changing the output routine.}.
  1036. \begin{figure}[tb]\small
  1037. \CmdIndex{extramarks}
  1038. \begin{verbatim}
  1039. % extramarks.sty
  1040. \def\@leftmark#1#2#3#4{#1}
  1041. \def\@rightmark#1#2#3#4{#2}
  1042. \def\markboth#1#2{{\def\protect{\noexpand\protect\noexpand}
  1043. \let\label\relax \let\index\relax \let\glossary\relax
  1044. \expandafter\@markboth\@themark{#1}{#2}
  1045. \mark{\@themark}}\if@nobreak\ifvmode\nobreak\fi\fi}
  1046. \def\markright#1{{\def\protect{\noexpand\protect\noexpand}
  1047. \let\label\relax \let\index\relax \let\glossary\relax
  1048. \expandafter\@markright\@themark
  1049. {#1}\mark{\@themark}}\if@nobreak\ifvmode\nobreak\fi\fi}
  1050. \def\@markright#1#2#3#4#5{\gdef\@themark{{#1}{#5}{#3}{#4}}}
  1051. \def\@markboth#1#2#3#4#5#6{\gdef\@themark{{#5}{#6}{#3}{#4}}}
  1052. \def\leftmark{\expandafter\@leftmark\botmark{}{}{}{}}
  1053. \def\rightmark{\expandafter\@rightmark\firstmark{}{}{}{}}
  1054. \def\firstleftmark{\expandafter\@leftmark\firstmark{}{}{}{}}
  1055. \def\lastrightmark{\expandafter\@rightmark\botmark{}{}{}{}}
  1056. \def\@themark{{}{}{}{}}
  1057. \def\extramarks#1#2{{\def\protect{\noexpand\protect\noexpand}
  1058. \let\label\relax \let\index\relax \let\glossary\relax
  1059. \expandafter\@markextra\@themark{#1}{#2}
  1060. \mark{\@themark}}\if@nobreak\ifvmode\nobreak\fi\fi}
  1061. \def\@markextra#1#2#3#4#5#6{\gdef\@themark{{#1}{#2}{#5}{#6}}}
  1062. \def\firstxmark{\expandafter\@firstxmark\firstmark{}{}{}{}}
  1063. \def\topxmark{\expandafter\@firstxmark\topmark{}{}{}{}}
  1064. \def\lastxmark{\expandafter\@lastxmark\botmark{}{}{}{}}
  1065. \def\@firstxmark#1#2#3#4{#3}
  1066. \def\@lastxmark#1#2#3#4{#4}
  1067. \end{verbatim}
  1068. \caption{Package for extra marks in \latex/}
  1069. \label{fig:xmarks}
  1070. \end{figure}
  1071. Here is a way to use this package:
  1072. \begin{verbatim}
  1073. \usepackage{extramarks}
  1074. ...
  1075. \pagestyle{fancy}
  1076. \lhead{\firstxmark}
  1077. \rfoot{\lastxmark}
  1078. ...
  1079. \extramarks{}{Continued on next page\ldots}
  1080. Some text that may or may not cross a page boundary...
  1081. \extramarks{Continued\ldots}{}
  1082. \end{verbatim}
  1083. \CmdIndex{extramarks}
  1084. Note that the \Cmd{extramarks} command must be close to the text, i.e no
  1085. empty lines (paragraph boundaries) should intervene. Otherwise the page may
  1086. be broken at that boundary and the extramarks would come on the wrong page.
  1087. There are two new marks that can be used in the page layout with this
  1088. package: If commands of the form
  1089. \verb|\extramarks{|$m_1$\verb|}{|$m_2$\verb|}| are given
  1090. \CmdIndex{firstxmark}
  1091. \CmdIndex{lastxmark}
  1092. \Cmd{firstxmark} gives you the first $m_1$ value and
  1093. \Cmd{lastxmark} gives you the last $m_2$ value
  1094. of the current page.
  1095. \CmdIndex{firstleftmark}
  1096. \CmdIndex{lastrightmark}
  1097. It also gives you the \Cmd{firstleftmark} and \Cmd{lastrightmark}
  1098. commands that complement the standard \latex/ marks.
  1099. To stress the point that marks are the correct way to do this, let me
  1100. give you a ``solution'' that will not work\footnote{Actually there is
  1101. another way but it requires two \latex/ passes: you can put \Cmd{label}
  1102. commands before and after the text and compare the \Cmd{pageref}s.}:
  1103. \begin{verbatim}
  1104. \lhead{Continued}
  1105. \rfoot{Continued on next page\ldots}
  1106. Some text that may or may not cross a page boundary...
  1107. \lhead{}
  1108. \rfoot{}
  1109. \end{verbatim}
  1110. You may be tempted to think that the first \Cmd{lhead} and \Cmd{rfoot}
  1111. will be in effect when \tex/ breaks the page in the middle of the text,
  1112. and the last ones when the page breaks after the text. This is not true as
  1113. the whole paragraph (including the last definitions) will be processed
  1114. \index{page break}
  1115. before \tex/ considers the page break, so at the time of the page break the
  1116. last definitions are effective, whether the page break occurs inside the text
  1117. or outside of it. Putting a paragraph boundary between the
  1118. text and the last definitions will not work either, because you don't want
  1119. the first definitions to be in effect when \tex/ decides to break the page
  1120. exactly at this boundary. Actually the marks mechanism was invented to get
  1121. rid of these kinds of problems.
  1122. In the above example the text ``Continued'' appears in the page header. It
  1123. \index{margin}
  1124. may be nicer to put it in the margin. This can be easily
  1125. accomplished by positioning it at a fixed place relative to the page header.
  1126. In plain \tex/ you would use a concoction of \Cmd{hbox to 0pt},
  1127. \Cmd{vbox to 0pt}, \Cmd{hskip},\Cmd{vskip}, \Cmd{hss} and
  1128. \Cmd{vss} but fortunately
  1129. \latex/'s \texttt{picture} environment gives a much cleaner way to do this.
  1130. In order not to disturb the normal header layout we put the text in a zero-sized
  1131. \texttt{picture}. Generally this is the best way to position things on fixed
  1132. places on the page. You can then also use the normal headings. See also
  1133. section~\ref{sec:thumb} for another example of
  1134. this technique.
  1135. \TTindex{picture}
  1136. \begin{verbatim}
  1137. \lhead{\setlength{\unitlength}{\baselineskip}%
  1138. \begin{picture}(0,0)
  1139. \put(-2,-3){\makebox(0,0)[r]{\firstxmark}}
  1140. \end{picture}\leftmark}
  1141. \end{verbatim}
  1142. This solution can of course also be used for the footer. Make sure you put the
  1143. \texttt{picture} as the first thing in left-handside entries and last in right-handside
  1144. ones.
  1145. Finally you may want to put ``(Continued\ldots)'' in the \emph{text}
  1146. rather than in the header or the margin. Then you have to use the
  1147. \TTindex{afterpage.sty}
  1148. \texttt{afterpage} package. We also decide to make a separate
  1149. environment for it.
  1150. \begin{verbatim}
  1151. \newenvironment{continued}{\par
  1152. \extramarks{}{Continued on next page\ldots}
  1153. \afterpage{\noindent\firstxmark\vspace{1ex}}
  1154. }{\extramarks{(Continued\ldots)}{}\par}
  1155. \end{verbatim}
  1156. It is a bit dangerous to use \Cmd{firstxmark} outside the page layout
  1157. routine, but apparently with \Cmd{afterpage} this works. If you would
  1158. need the information further on in the page you must remember the state of
  1159. the marks in your own variable. You can set this in one of the
  1160. \textsf{fancyhdr} fields. For example if you want to add something
  1161. \emph{after} the broken piece of text you can use the following:
  1162. \begin{verbatim}
  1163. \newcommand{\mysaved}{}
  1164. \newenvironment{continued}{\par
  1165. \extramarks{}{Continued on next page\ldots}
  1166. }{\extramarks{(Continued\ldots)}{}\par\vspace{1ex}\mysaved}
  1167. \lhead{\leftmark}
  1168. \chead{\ifthenelse{\equal{\lastxmark}{}}
  1169. {\gdef\mysaved{}}
  1170. {\gdef\mysaved{\noindent[Continued from previous page]}}}
  1171. \end{verbatim}
  1172. If you want to include one of the marks or other varying information in the
  1173. saved text, you must use \Cmd{xdef} rather than \Cmd{gdef}.
  1174. \section{A movie}
  1175. \label{sec:movie}
  1176. \index{movie}
  1177. \TTindex{picture}
  1178. If you put at each page on the same place a picture that slightly changes
  1179. from page to page you can get a movie-like effect by flipping through the
  1180. pages. You can create such a movie easily with fancyhdr. For
  1181. simplicity we assume that each picture is in a postscript (EPS) file called
  1182. \texttt{pic}$\langle n\rangle$.\texttt{ps} where $\langle n\rangle$ is the page number and that we use
  1183. the \texttt{graphics} or \texttt{graphicx} package\footnote{If you use an older version of
  1184. \LaTeX{} you could use the \texttt{epsf} or \texttt{epsfig} package.}.
  1185. \TTindex{graphics}
  1186. \TTindex{graphicx}
  1187. \TTindex{epsf}
  1188. \TTindex{epsfig}
  1189. To put the movie in the righthandside bottom corner the following will work:
  1190. \begin{verbatim}
  1191. \rfoot{\setlength{\unitlength}{1mm}
  1192. \begin{picture}(0,0)
  1193. \put(5,0){\includegraphics{pic\thepage.ps}}
  1194. \end{picture}}
  1195. \end{verbatim}
  1196. Notice that the \Cmd{unitlength} parameter should
  1197. be set locally in the fancyhdr field in order to avoid unwanted
  1198. interference with its value in the text.
  1199. \section{Thumb-indexes}
  1200. \label{sec:thumb}
  1201. \index{bible}
  1202. Some railroad guides and expensive bibles have so called
  1203. \index{thumb-index}
  1204. \emph{thumb-indexes}, i.e.\ there are marks on the sides of the pages that
  1205. indicate where the chapters are. You can create these by printing black
  1206. blobs in the margin of the pages. The vertical position should be
  1207. determined by the chapter number or some other counter. As the position is
  1208. independent of the contents of the page, we print these blobs as part of
  1209. the header in a zero-sized \texttt{picture} as described in the previous
  1210. section.
  1211. Of course we have to take care of two-sided printing, and we may want to
  1212. have an index page with all the blobs in the correct position. The solution
  1213. requires some hand-tuning to get the blobs nicely spaced out vertically.
  1214. For the application that I had there were 12 sections, so I made the blobs
  1215. 18 mm apart, i.e. 9~mm blob separated by 9~mm whitespace. In order to avoid
  1216. calculations they are set in a \texttt{picture} environment with the
  1217. \Cmd{unitlength} set to 18~mm. Page numbers are set in the headers at the
  1218. outer sides, and the blobs are attached to these. In this example the
  1219. section numbers are used to position the blobs, but you can replace this
  1220. with any numeric value.
  1221. See figure~\ref{fig:overview} for the resulting
  1222. overview page and figure~\ref{fig:thumb} for the code.
  1223. \begin{figure}[htbp]
  1224. \setlength{\unitlength}{9mm}
  1225. \newcommand{\blob}{\rule[-.2\unitlength]{1\unitlength}{.5\unitlength}}
  1226. \newcounter{line}
  1227. \newcommand{\secname}[1]{\addtocounter{line}{1}%
  1228. \put(1,-\value{line}){\blob}
  1229. \put(-7.5,-\value{line}){\arabic{line}}
  1230. \put(-7,-\value{line}){#1}}
  1231. \newcommand{\overview}{1
  1232. \begin{picture}(0,0)
  1233. \secname{Introduction}
  1234. \secname{The first year}
  1235. \secname{Specialisation}
  1236. \end{picture}}
  1237. \begin{center}
  1238. \leavevmode
  1239. \begin{picture}(11.3,5)
  1240. \put(0,0){\framebox(11.3,5)[tr]{}}
  1241. \put(9,4.5){\overview}
  1242. \end{picture}
  1243. \end{center}
  1244. \caption{Thumb-index overview page}
  1245. \label{fig:overview}
  1246. \end{figure}
  1247. \begin{figure}[hp]\small
  1248. \begin{verbatim}
  1249. \setlength{\unitlength}{18mm}
  1250. \newcommand{\blob}{\rule[-.2\unitlength]{2\unitlength}{.5\unitlength}}
  1251. \newcommand\rblob{\thepage
  1252. \begin{picture}(0,0)
  1253. \put(1,-\value{section}){\blob}
  1254. \end{picture}}
  1255. \newcommand\lblob{%
  1256. \begin{picture}(0,0)
  1257. \put(-3,-\value{section}){\blob}
  1258. \end{picture}%
  1259. \thepage}
  1260. \pagestyle{fancy}
  1261. \cfoot{}
  1262. \newcounter{line}
  1263. \newcommand{\secname}[1]{\addtocounter{line}{1}%
  1264. \put(1,-\value{line}){\blob}
  1265. \put(-7.5,-\value{line}){\Large \arabic{line}}
  1266. \put(-7,-\value{line}){\Large #1}}
  1267. \newcommand{\overview}{\thepage
  1268. \begin{picture}(0,0)
  1269. \secname{Introduction}
  1270. \secname{The first year}
  1271. \secname{Specialisation}
  1272. ...etc...
  1273. \end{picture}}
  1274. \begin{document}
  1275. \fancyhead[R]{\overview}\mbox{}\newpage % This produces the overview page
  1276. \fancyhead[R]{} % Front matter may follow here
  1277. \clearpage
  1278. \fancyhead[RE]{\rightmark}
  1279. \fancyhead[RO]{\rblob}
  1280. \fancyhead[LE]{\lblob}
  1281. \fancyhead[LO]{{\leftmark}
  1282. ...
  1283. \end{verbatim}
  1284. \caption{Thumb-index code}
  1285. \label{fig:thumb}
  1286. \end{figure}
  1287. \section{Float placement}
  1288. Floats are page elements that float with respect to the rest of the
  1289. document. Standard floats are tables and figures, but with the
  1290. \textsf{float} package you can easily make new ones, like algorithms. Most
  1291. of the time floats work satisfactory, but sometimes \LaTeX{} seems too
  1292. stubborn to do what you want. This section describes how you can influence
  1293. \LaTeX{} so that it will do most of the time what you want. There might,
  1294. however be some pathological cases where it is impossible to convince
  1295. \LaTeX{} to do things your way. In the following we will use figures as an
  1296. example but everything applies to other floats as well.
  1297. The most encountered problems with floats are:
  1298. \begin{enumerate}
  1299. \item You want a float at a certain position in the text, but \LaTeX{}
  1300. moves it, usually to the next page.
  1301. \item From a certain point, \LaTeX{} moves all your floats to the end of
  1302. the document or the end of a chapter.
  1303. \item \LaTeX{} complains about ``Too many floats''.
  1304. \end{enumerate}
  1305. In the first two cases you must first check if you have given the correct
  1306. ``placement'' parameter to you float, e.g. \Cmd{begin\{figure\}[htp]}
  1307. specifies that your figure may be placed either: Here (i.e. in the text
  1308. position where the command is given), on the Top of a page (which may be
  1309. the page where you put the command), or on a separate Page of floats. You
  1310. could also have specified ``b'' for Bottom of the page. The order of the
  1311. letters is insignificant, you cannot force \LaTeX{} to try Bottom first
  1312. and then Top by specifying \texttt{[bt]}.
  1313. If \LaTeX{} doesn't put the float at the place where you expected it, it is
  1314. usually caused by the following:
  1315. \begin{enumerate}
  1316. \item The float didn't fit on the page. In this case it has to move to the
  1317. next page or even further. If you didn't specify either \texttt{[t]} or
  1318. \texttt{[b]} in the position parameter, \latex/ must save it until it has
  1319. enough for a page of floats. So don't specify only \texttt{[h]}. If you
  1320. want to give \latex/ a chance to put the float on a page of floats, you
  1321. must also specify ``p''.
  1322. \item The placement would violate the constraints imposed by \latex/'s
  1323. float placement parameters. This is one of the most occurring causes and
  1324. it can easily be corrected by changing the parameters. Here is a list of
  1325. them:
  1326. \end{enumerate}
  1327. \begin{center}
  1328. \CmdIndex{topfraction}
  1329. \CmdIndex{bottomfraction}
  1330. \CmdIndex{textfraction}
  1331. \CmdIndex{floatpagefraction}
  1332. \TTindex{topnumber}
  1333. \TTindex{bottomnumber}
  1334. \TTindex{totalnumber}
  1335. \begin{tabular}{>{\tt}llc}
  1336. \hline
  1337. \multicolumn{3}{c}{Counters -- change with \Cmd{setcounter}}\\
  1338. \hline
  1339. topnumber & max. number of floats at top of page & 2 \\
  1340. bottomnumber & max. number of floats at bottom of page & 1 \\
  1341. totalnumber & max. number of floats on a page & 3\\
  1342. \hline
  1343. \multicolumn{3}{c}{Other -- change with \Cmd{renewcommand}}\\
  1344. \hline
  1345. \bs topfraction & max fraction of page for floats at top & 0.7 \\
  1346. \bs bottomfraction & max fraction of page for floats at bottom & 0.3 \\
  1347. \bs textfraction & min fraction of page for text & 0.2 \\
  1348. \bs floatpagefraction & min fraction of floatpage that should have
  1349. floats & 0.5 \\
  1350. \hline
  1351. \end{tabular}
  1352. \end{center}
  1353. There are also some others for double column floats in two-column documents.
  1354. The values in the righthand column are the defaults for the standard
  1355. \latex/ classes. Other classes could use different defaults. As you see
  1356. with the default values a float will not be put in the bottom of a page if
  1357. its height is more than 30\% of the page height. So if you specify [hb] for
  1358. a float which is taller it has to move to a float page. But if it is less
  1359. than 50\% of the page height it will have to wait until some more floats
  1360. are given before a float page can be filled to satisfy the
  1361. \Cmd{floatpagefraction} parameter. If you have this kind of behaviour you
  1362. can easily adapt the parameters, e.g. with:
  1363. \begin{verbatim}
  1364. \renewcommand{\textfraction}{0.05}
  1365. \renewcommand{\topfraction}{0.95}
  1366. \renewcommand{\bottomfraction}{0.95}
  1367. \renewcommand{\floatpagefraction}{0.35}
  1368. \setcounter{totalnumber}{5}
  1369. \end{verbatim}
  1370. You may want to be careful not to make \Cmd{floatpagefraction} too small,
  1371. otherwise you may get too many small floatpages.
  1372. You can force \latex/ to ignore most of the parameters for one specific float
  1373. occurrence by
  1374. including an exclamation mark (!) in the placement parameters, e.g.
  1375. \begin{verbatim}
  1376. \begin{figure}[!htb]
  1377. \end{verbatim}
  1378. Floats which contain a ``\texttt{t}'' in the position parameter could be
  1379. placed before the place where they are referenced (but on the same page).
  1380. This is normal behaviour for \latex/ but some people just don't like it.
  1381. There are a number of ways to prevent this:
  1382. \begin{enumerate}
  1383. \item Of course deleting the ``\texttt{t}'' will help, but in general this is
  1384. undesirable, as you may want the float to be placed at the top of the
  1385. next page.
  1386. \item use the \texttt{flafter} package which causes floats never to be
  1387. placed ``backwards''.
  1388. \item use the command \Cmd{suppressfloats[t]}\footnote{This command and the
  1389. ``!'' placement parameter are not defined in \LaTeX2.09.}. This
  1390. command will cause floats for the top position \emph{on this page} to be
  1391. moved to the next page. This can also be done with \texttt{[b]} or
  1392. without parameter for all floats on this page.
  1393. \end{enumerate}
  1394. If in spite of all your attempts \latex/ still moves your floats to the end
  1395. of the document or the end of a chapter, you can insert a \Cmd{clearpage}
  1396. command. This will start a new page and insert all pending floats before
  1397. continueing. If it is undesirable to have a pagebreak you can use the
  1398. \textsf{afterpage} package and the following command:
  1399. \begin{verbatim}
  1400. \afterpage{clearpage}
  1401. \end{verbatim}
  1402. This will wait until the current page is finished and then flush all
  1403. outstanding floats. In some pathological circumstances \texttt{afterpage}
  1404. may give strange results, however.
  1405. Finally, if you want a float only at the place where you define it, without
  1406. \latex/ moving it whatsoever, you can use the \textsf{float} package and
  1407. give the command:
  1408. \begin{verbatim}
  1409. \restylefloat{figure}
  1410. \end{verbatim}
  1411. in the preamble. Now you will be able to
  1412. specify \texttt{[H]} as the position parameter, which will mean ``HERE and only
  1413. HERE''. This may cause an unwanted page break however\footnote{There used
  1414. to be a \texttt{here.sty} with the same effect, but this is incompatible
  1415. with \LaTeXe}. If you want to avoid the unwanted pagebreak, i.e. let
  1416. \LaTeX{} move the float only if it doesn't fit on the page, the use the
  1417. \textsf{afterpage} package with:
  1418. \begin{verbatim}
  1419. \afterpage{\clearpage \begin{figure}[H] ... \end{figure}}
  1420. \end{verbatim}
  1421. Complaints from \latex/ about ``Too many floats'' are usually caused by one
  1422. of the above problems: floats not being able to be placed and \latex/
  1423. collecting too many of them. The solutions given above, especially those
  1424. with \Cmd{clearpage} in them will usually help. In some cases there
  1425. really are too many floats, as \latex/ has a limited number of ``boxes'' to
  1426. store the floats. The package \textsf{morefloats} can be used to increase
  1427. this number. If you need still more then you must edit a private copy of
  1428. this file, but even then there will be some limit that you cannot pass.
  1429. Then your only resort will be to change your document.
  1430. \section{Multipage Floats}
  1431. \LaTeX's floats cannot be split across pages. Sometimes, however, you want
  1432. to have a table or figure that doesn't fit on one page. The easiest way is
  1433. to split these into multiple table or figure environments, but this has a
  1434. number of undesirable effects:
  1435. \begin{itemize}
  1436. \item Where do you split it? This is in general more difficult for tables
  1437. than for figures.
  1438. \item How do you keep them together?
  1439. \item You don't want more than one entry in the list of figures/tables.
  1440. \end{itemize}
  1441. Although these problems are not fully solvable in all cases, here are a
  1442. couple of suggestions:
  1443. \subsection{Tables}
  1444. For tables longer than a page you can use the \texttt{longtable} package.
  1445. \TTindex{longtable}
  1446. This package defines a \texttt{longtable} environment that is a kind of
  1447. amalgamation of \texttt{table} and \texttt{tabular}. It has approximately
  1448. the same syntax as the \texttt{tabular} environment, but it adds some
  1449. features of \texttt{table}, like captions. Longtables will be automatically
  1450. split when they don't fit on the page. And they will be entered in the list
  1451. of tables when a caption is given. They will not float, however, and cannot
  1452. be used inside a float environment. This could mean that another
  1453. \texttt{table} environment, which was defined before the
  1454. \texttt{longtable}, will float past it, and therefore the numbers may get
  1455. out of order. Another problem could be that the \texttt{longtable} starts
  1456. rather far down the page, which isn't a pleasant sight. If you want the
  1457. \texttt{longtable} to start at the top of the page, the best thing to do is
  1458. to include it in an \Cmd{afterpage} command (using the \texttt{afterpage}
  1459. package). As a \texttt{longtable} is by definition large, it is best to put
  1460. it in a separate file, and \Cmd{input} it in the \Cmd{afterpage} command:
  1461. \CmdIndex{afterpage}
  1462. \begin{verbatim}
  1463. \afterpage{\input{mytable}}
  1464. \end{verbatim}
  1465. \begin{verbatim}
  1466. \afterpage{\clearpage\input{mytable}}
  1467. \end{verbatim}
  1468. The last form has the additional advantage that most of the outstanding
  1469. floats will be printed first.
  1470. \subsection{Figures}
  1471. There isn't an equivalent \texttt{longfigure} solution, so for figures you
  1472. will usually have to split it yourself. In general this is less of a
  1473. problem. However, the problem you get now is how to keep them together,
  1474. i.e. how to get the parts on subsequent pages, and how to get a single
  1475. entry in the list of figures.
  1476. You will have to split the figure into pieces and put each part
  1477. in a separate \texttt{figure} environment. To keep them together it is best
  1478. to use only the \texttt{[p]} placement, so that they will be put on
  1479. floatpages. As they are bigger than a page this is appropriate. The first
  1480. part would then get a \Cmd{caption}, the subsequent parts would be used
  1481. without a caption. If you want to add a caption-like text, enter it as
  1482. normal text rather than a \Cmd{caption}, so that it will not be entered in
  1483. the list of figures. It may also be desirable to issue a \Cmd{clearpage}
  1484. first, just like we did for the \texttt{longtable}, and to encapsulate this
  1485. in the \Cmd{afterpage} command. E.g.
  1486. \begin{verbatim}
  1487. \afterpage{\clearpage\input{myfigure}}
  1488. \end{verbatim}
  1489. where \texttt{myfigure.tex} contains:
  1490. \begin{verbatim}
  1491. \begin{figure}[p]
  1492. \includegraphics{myfig1.eps}
  1493. \caption{This is a multipage figure}
  1494. \label{fig:xxx}
  1495. \end{figure}
  1496. \begin{figure}[p]
  1497. \includegraphics{myfig2.eps}
  1498. \begin{center}
  1499. Figure~\ref{fig:xxx} (continued)
  1500. \end{center}
  1501. \end{figure}
  1502. \end{verbatim}
  1503. You have to make sure that the last part is big enough, otherwise \LaTeX{}
  1504. could decide to postpone it until it has collected some more floats. This
  1505. can be done either by making the figure big enough (e.g. by adding some
  1506. \Cmd{vspace}), or by tweaking the \Cmd{floatpagefraction}
  1507. \CmdIndex{floatpagefraction} parameter.
  1508. If you want your multipage figure to start at a lefthand-side
  1509. (even-numbered) page you can use a test in the \Cmd{afterpage} command
  1510. (using the \texttt{ifthen} package):
  1511. \begin{verbatim}
  1512. \afterpage{\clearpage
  1513. \ifthenelse{\isodd{\value{page}}{\afterpage{\input{myfigure}}} % odd page
  1514. {\input{myfigure}}}} % even page
  1515. \end{verbatim}
  1516. If there are too many floats on the skipped page, this may still fail to
  1517. start your multipage figure on an even page, however.
  1518. \section{Contact information}
  1519. Piet van Oostrum\\
  1520. Dept.\ of Computer Science\\
  1521. Utrecht University\\
  1522. P.O. Box 80.089\\
  1523. 3508 TB Utrecht, The Netherlands\\
  1524. Telephone: +31 30 2531806 Telefax: +31 30 2513791\\
  1525. E-mail: [email protected] \\
  1526. WWW: http://www.cs.ruu.nl/people/piet
  1527. \printindex
  1528. \end{document}
  1529. % Local Variables:
  1530. % mode: latex
  1531. % TeX-master: t
  1532. % End: