fancyhdr.sty 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. % fancyhdr.sty version 1.99c
  2. % Fancy headers and footers.
  3. % Piet van Oostrum, Dept of Computer Science, University of Utrecht
  4. % Padualaan 14, P.O. Box 80.089, 3508 TB Utrecht, The Netherlands
  5. % Telephone: +31 30 2532180. Email: [email protected]
  6. % Sep 16, 1994
  7. % version 1.4: Correction for use with \reversemargin
  8. % Sep 29, 1994:
  9. % version 1.5: Added the \iftopfloat, \ifbotfloat and \iffloatpage commands
  10. % Oct 4, 1994:
  11. % version 1.6: Reset single spacing in headers/footers for use with
  12. % setspace.sty or doublespace.sty
  13. % Oct 4, 1994:
  14. % version 1.7: changed \let\@mkboth\markboth to
  15. % \def\@mkboth{\protect\markboth} to make it more robust
  16. % Dec 5, 1994:
  17. % version 1.8: corrections for amsbook/amsart: define \@chapapp and (more
  18. % importantly) use the \chapter/sectionmark definitions from ps@headings if
  19. % they exist (which should be true for all standard classes).
  20. % May 31, 1995:
  21. % version 1.9: The proposed \renewcommand{\headrulewidth}{\iffloatpage...
  22. % construction in the doc did not work properly with the fancyplain style.
  23. % June 1, 1995:
  24. % version 1.91: The definition of \@mkboth wasn't restored on subsequent
  25. % \pagestyle{fancy}'s.
  26. % June 1, 1995:
  27. % version 1.92: The sequence \pagestyle{fancyplain} \pagestyle{plain}
  28. % \pagestyle{fancy} would erroneously select the plain version.
  29. % June 1, 1995:
  30. % version 1.93: \fancypagestyle command added.
  31. % Dec 11, 1995:
  32. % version 1.94: suggested by Conrad Hughes <[email protected]>
  33. % CJCH, Dec 11, 1995: added \footruleskip to allow control over footrule
  34. % position (old hardcoded value of .3\normalbaselineskip is far too high
  35. % when used with very small footer fonts).
  36. % Jan 31, 1996:
  37. % version 1.95: call \@normalsize in the reset code if that is defined,
  38. % otherwise \normalsize.
  39. % this is to solve a problem with ucthesis.cls, as this doesn't
  40. % define \@currsize. Unfortunately for latex209 calling \normalsize doesn't
  41. % work as this is optimized to do very little, so there \@normalsize should
  42. % be called. Hopefully this code works for all versions of LaTeX known to
  43. % mankind.
  44. % April 25, 1996:
  45. % version 1.96: initialize \headwidth to a magic (negative) value to catch
  46. % most common cases that people change it before calling \pagestyle{fancy}.
  47. % Note it can't be initialized when reading in this file, because
  48. % \textwidth could be changed afterwards. This is quite probable.
  49. % We also switch to \MakeUppercase rather than \uppercase and introduce a
  50. % \nouppercase command for use in headers. and footers.
  51. % May 3, 1996:
  52. % version 1.97: Two changes:
  53. % 1. Undo the change in version 1.8 (using the pagestyle{headings} defaults
  54. % for the chapter and section marks. The current version of amsbook and
  55. % amsart classes don't seem to need them anymore. Moreover the standard
  56. % latex classes don't use \markboth if twoside isn't selected, and this is
  57. % confusing as \leftmark doesn't work as expected.
  58. % 2. include a call to \ps@empty in ps@@fancy. This is to solve a problem
  59. % in the amsbook and amsart classes, that make global changes to \topskip,
  60. % which are reset in \ps@empty. Hopefully this doesn't break other things.
  61. % May 7, 1996:
  62. % version 1.98:
  63. % Added % after the line \def\nouppercase
  64. % May 7, 1996:
  65. % version 1.99: This is the alpha version of fancyhdr 2.0
  66. % Introduced the new commands \fancyhead, \fancyfoot, and \fancyhf.
  67. % Changed \headrulewidth, \footrulewidth, \footruleskip to
  68. % macros rather than length parameters, In this way they can be
  69. % conditionalized and they don't consume length registers. There is no need
  70. % to have them as length registers unless you want to do calculations with
  71. % them, which is unlikely. Note that this may make some uses of them
  72. % incompatible (i.e. if you have a file that uses \setlength or \xxxx=)
  73. % May 10, 1996:
  74. % version 1.99a:
  75. % Added a few more % signs
  76. % May 10, 1996:
  77. % version 1.99b:
  78. % Changed the syntax of \f@nfor to be resistent to catcode changes of :=
  79. % Removed the [1] from the defs of \lhead etc. because the parameter is
  80. % consumed by the \@[xy]lhead etc. macros.
  81. % June 24, 1997:
  82. % version 1.99c:
  83. % corrected \nouppercase to also include the protected form of \MakeUppercase
  84. % \global added to manipulation of \headwidth.
  85. % \iffootnote command added.
  86. % Some comments added about \@fancyhead and \@fancyfoot.
  87. \let\fancy@def\gdef
  88. \def\if@mpty#1#2#3{\def\temp@ty{#1}\ifx\@empty\temp@ty #2\else#3\fi}
  89. % Usage: \@forc \var{charstring}{command to be executed for each char}
  90. % This is similar to LaTeX's \@tfor, but expands the charstring.
  91. \def\@forc#1#2#3{\expandafter\f@rc\expandafter#1\expandafter{#2}{#3}}
  92. \def\f@rc#1#2#3{\def\temp@ty{#2}\ifx\@empty\temp@ty\else
  93. \f@@rc#1#2\f@@rc{#3}\fi}
  94. \def\f@@rc#1#2#3\f@@rc#4{\def#1{#2}#4\f@rc#1{#3}{#4}}
  95. % Usage: \f@nfor\name:=list\do{body}
  96. % Like LaTeX's \@for but an empty list is treated as a list with an empty
  97. % element
  98. \newcommand{\f@nfor}[3]{\edef\@fortmp{#2}%
  99. \expandafter\@forloop#2,\@nil,\@nil\@@#1{#3}}
  100. % Usage: \def@ult \cs{defaults}{argument}
  101. % sets \cs to the characters from defaults appearing in argument
  102. % or defaults if it would be empty. All characters are lowercased.
  103. \newcommand\def@ult[3]{%
  104. \edef\temp@a{\lowercase{\edef\noexpand\temp@a{#3}}}\temp@a
  105. \def#1{}%
  106. \@forc\tmpf@ra{#2}%
  107. {\expandafter\if@in\tmpf@ra\temp@a{\edef#1{#1\tmpf@ra}}{}}%
  108. \ifx\@empty#1\def#1{#2}\fi}
  109. %
  110. % \if@in <char><set><truecase><falsecase>
  111. %
  112. \newcommand{\if@in}[4]{%
  113. \edef\temp@a{#2}\def\temp@b##1#1##2\temp@b{\def\temp@b{##1}}%
  114. \expandafter\temp@b#2#1\temp@b\ifx\temp@a\temp@b #4\else #3\fi}
  115. \newcommand{\fancyhead}{\@ifnextchar[{\f@ncyhf h}{\f@ncyhf h[]}}
  116. \newcommand{\fancyfoot}{\@ifnextchar[{\f@ncyhf f}{\f@ncyhf f[]}}
  117. \newcommand{\fancyhf}{\@ifnextchar[{\f@ncyhf {}}{\f@ncyhf {}[]}}
  118. % The header and footer fields are stored in command sequences with
  119. % names of the form: \f@ncy<x><y><z> with <x> for [eo], <y> form [lcr]
  120. % and <z> from [hf].
  121. \def\f@ncyhf#1[#2]#3{%
  122. \def\temp@c{}%
  123. \@forc\tmpf@ra{#2}%
  124. {\expandafter\if@in\tmpf@ra{eolcrhf,EOLCRHF}%
  125. {}{\edef\temp@c{\temp@c\tmpf@ra}}}%
  126. \ifx\@empty\temp@c\else
  127. \ifx\PackageError\undefined
  128. \errmessage{Illegal char `\temp@c' in fancyhdr argument:
  129. [#2]}\else
  130. \PackageError{Fancyhdr}{Illegal char `\temp@c' in fancyhdr argument:
  131. [#2]}{}\fi
  132. \fi
  133. \f@nfor\temp@c{#2}%
  134. {\def@ult\f@@@eo{eo}\temp@c
  135. \def@ult\f@@@lcr{lcr}\temp@c
  136. \def@ult\f@@@hf{hf}{#1\temp@c}%
  137. \@forc\f@@eo\f@@@eo
  138. {\@forc\f@@lcr\f@@@lcr
  139. {\@forc\f@@hf\f@@@hf
  140. {\expandafter\fancy@def\csname
  141. f@ncy\f@@eo\f@@lcr\f@@hf\endcsname
  142. {#3}}}}}}
  143. % Fancyheadings version 1 commands. These are more or less depracated,
  144. % but they continue to work.
  145. \newcommand{\lhead}{\@ifnextchar[{\@xlhead}{\@ylhead}}
  146. \def\@xlhead[#1]#2{\fancy@def\f@ncyelh{#1}\fancy@def\f@ncyolh{#2}}
  147. \def\@ylhead#1{\fancy@def\f@ncyelh{#1}\fancy@def\f@ncyolh{#1}}
  148. \newcommand{\chead}{\@ifnextchar[{\@xchead}{\@ychead}}
  149. \def\@xchead[#1]#2{\fancy@def\f@ncyech{#1}\fancy@def\f@ncyoch{#2}}
  150. \def\@ychead#1{\fancy@def\f@ncyech{#1}\fancy@def\f@ncyoch{#1}}
  151. \newcommand{\rhead}{\@ifnextchar[{\@xrhead}{\@yrhead}}
  152. \def\@xrhead[#1]#2{\fancy@def\f@ncyerh{#1}\fancy@def\f@ncyorh{#2}}
  153. \def\@yrhead#1{\fancy@def\f@ncyerh{#1}\fancy@def\f@ncyorh{#1}}
  154. \newcommand{\lfoot}{\@ifnextchar[{\@xlfoot}{\@ylfoot}}
  155. \def\@xlfoot[#1]#2{\fancy@def\f@ncyelf{#1}\fancy@def\f@ncyolf{#2}}
  156. \def\@ylfoot#1{\fancy@def\f@ncyelf{#1}\fancy@def\f@ncyolf{#1}}
  157. \newcommand{\cfoot}{\@ifnextchar[{\@xcfoot}{\@ycfoot}}
  158. \def\@xcfoot[#1]#2{\fancy@def\f@ncyecf{#1}\fancy@def\f@ncyocf{#2}}
  159. \def\@ycfoot#1{\fancy@def\f@ncyecf{#1}\fancy@def\f@ncyocf{#1}}
  160. \newcommand{\rfoot}{\@ifnextchar[{\@xrfoot}{\@yrfoot}}
  161. \def\@xrfoot[#1]#2{\fancy@def\f@ncyerf{#1}\fancy@def\f@ncyorf{#2}}
  162. \def\@yrfoot#1{\fancy@def\f@ncyerf{#1}\fancy@def\f@ncyorf{#1}}
  163. \newdimen\headwidth
  164. \newcommand{\headrulewidth}{0.4pt}
  165. \newcommand{\footrulewidth}{\z@skip}
  166. \newcommand{\footruleskip}{.3\normalbaselineskip}
  167. % Fancyplain stuff shouldn't be used anymore (rather
  168. % \fancypagestyle{plain} should be used), but it must be present for
  169. % compatibility reasons.
  170. \newcommand{\plainheadrulewidth}{\z@skip}
  171. \newcommand{\plainfootrulewidth}{\z@skip}
  172. \newif\if@fancyplain \@fancyplainfalse
  173. \def\fancyplain#1#2{\if@fancyplain#1\else#2\fi}
  174. \headwidth=-123456789sp %magic constant
  175. % Command to reset various things in the headers:
  176. % a.o. single spacing (taken from setspace.sty)
  177. % and the catcode of ^^M (so that epsf files in the header work if a
  178. % verbatim crosses a page boundary)
  179. % It also defines a \nouppercase command that disables \uppercase and
  180. % \Makeuppercase. It can only be used in the headers and footers.
  181. \def\fancy@reset{\restorecr
  182. \def\baselinestretch{1}%
  183. \def\nouppercase##1{{\let\uppercase\relax\let\MakeUppercase\relax
  184. \expandafter\let\csname MakeUppercase \endcsname\relax##1}}%
  185. \ifx\undefined\@newbaseline% NFSS not present; 2.09 or 2e
  186. \ifx\@normalsize\undefined \normalsize % for ucthesis.cls
  187. \else \@normalsize \fi
  188. \else% NFSS (2.09) present
  189. \@newbaseline%
  190. \fi}
  191. % Initialization of the head and foot text.
  192. % The default values still contain \fancyplain for compatibility.
  193. \fancyhf{} % clear all
  194. % lefthead empty on ``plain'' pages, \rightmark on even, \leftmark on odd pages
  195. % evenhead empty on ``plain'' pages, \leftmark on even, \rightmark on odd pages
  196. \fancyhead[el,or]{\fancyplain{}{\sl\rightmark}}
  197. \fancyhead[er,ol]{\fancyplain{}{\sl\leftmark}}
  198. \fancyfoot[c]{\rm\thepage} % page number
  199. % Put together a header or footer given the left, center and
  200. % right text, fillers at left and right and a rule.
  201. % The \lap commands put the text into an hbox of zero size,
  202. % so overlapping text does not generate an errormessage.
  203. % These macros have 5 parameters:
  204. % 1. \@lodd or \@rodd % This determines at which side the header will stick
  205. % out.
  206. % 2. \f@ncyolh, \f@ncyelh, \f@ncyolf or \f@ncyelf. This is the left component.
  207. % 3. \f@ncyoch, \f@ncyech, \f@ncyocf or \f@ncyecf. This is the middle comp.
  208. % 4. \f@ncyorh, \f@ncyerh, \f@ncyorf or \f@ncyerf. This is the right component.
  209. % 5. \@lodd or \@rodd % This determines at which side the header will stick
  210. % out. This is the reverse of parameter nr. 1. One of them is always
  211. % \relax and the other one is \hss (after expansion).
  212. \def\@fancyhead#1#2#3#4#5{#1\hbox to\headwidth{\fancy@reset\vbox{\hbox
  213. {\rlap{\parbox[b]{\headwidth}{\raggedright#2\strut}}\hfill
  214. \parbox[b]{\headwidth}{\centering#3\strut}\hfill
  215. \llap{\parbox[b]{\headwidth}{\raggedleft#4\strut}}}\headrule}}#5}
  216. \def\@fancyfoot#1#2#3#4#5{#1\hbox to\headwidth{\fancy@reset\vbox{\footrule
  217. \hbox{\rlap{\parbox[t]{\headwidth}{\raggedright#2\strut}}\hfill
  218. \parbox[t]{\headwidth}{\centering#3\strut}\hfill
  219. \llap{\parbox[t]{\headwidth}{\raggedleft#4\strut}}}}}#5}
  220. \def\headrule{{\if@fancyplain\let\headrulewidth\plainheadrulewidth\fi
  221. \hrule\@height\headrulewidth\@width\headwidth \vskip-\headrulewidth}}
  222. \def\footrule{{\if@fancyplain\let\footrulewidth\plainfootrulewidth\fi
  223. \vskip-\footruleskip\vskip-\footrulewidth
  224. \hrule\@width\headwidth\@height\footrulewidth\vskip\footruleskip}}
  225. \def\ps@fancy{%
  226. \@ifundefined{@chapapp}{\let\@chapapp\chaptername}{}%for amsbook
  227. %
  228. % Define \MakeUppercase for old LaTeXen.
  229. % Note: we used \def rather than \let, so that \let\uppercase\relax (from
  230. % the version 1 documentation) will still work.
  231. %
  232. \@ifundefined{MakeUppercase}{\def\MakeUppercase{\uppercase}}{}%
  233. \@ifundefined{chapter}{\def\sectionmark##1{\markboth
  234. {\MakeUppercase{\ifnum \c@secnumdepth>\z@
  235. \thesection\hskip 1em\relax \fi ##1}}{}}%
  236. \def\subsectionmark##1{\markright {\ifnum \c@secnumdepth >\@ne
  237. \thesubsection\hskip 1em\relax \fi ##1}}}%
  238. {\def\chaptermark##1{\markboth {\MakeUppercase{\ifnum \c@secnumdepth>\m@ne
  239. \@chapapp\ \thechapter. \ \fi ##1}}{}}%
  240. \def\sectionmark##1{\markright{\MakeUppercase{\ifnum \c@secnumdepth >\z@
  241. \thesection. \ \fi ##1}}}}%
  242. %\csname ps@headings\endcsname % use \ps@headings defaults if they exist
  243. \ps@@fancy
  244. \gdef\ps@fancy{\@fancyplainfalse\ps@@fancy}%
  245. % Initialize \headwidth if the user didn't
  246. %
  247. \ifdim\headwidth<0sp
  248. %
  249. % This catches the case that \headwidth hasn't been initialized and the
  250. % case that the user added something to \headwidth in the expectation that
  251. % it was initialized to \textwidth. We compensate this now. This loses if
  252. % the user intended to multiply it by a factor. But that case is more
  253. % likely done by saying something like \headwidth=1.2\textwidth.
  254. % The doc says you have to change \headwidth after the first call to
  255. % \pagestyle{fancy}. This code is just to catch the most common cases were
  256. % that requirement is violated.
  257. %
  258. \global\advance\headwidth123456789sp\global\advance\headwidth\textwidth
  259. \fi}
  260. \def\ps@fancyplain{\ps@fancy \let\ps@plain\ps@plain@fancy}
  261. \def\ps@plain@fancy{\@fancyplaintrue\ps@@fancy}
  262. \def\ps@@fancy{%
  263. \ps@empty % This is for amsbook/amsart, which do strange things with \topskip
  264. \def\@mkboth{\protect\markboth}%
  265. \def\@oddhead{\@fancyhead\@lodd\f@ncyolh\f@ncyoch\f@ncyorh\@rodd}%
  266. \def\@oddfoot{\@fancyfoot\@lodd\f@ncyolf\f@ncyocf\f@ncyorf\@rodd}%
  267. \def\@evenhead{\@fancyhead\@rodd\f@ncyelh\f@ncyech\f@ncyerh\@lodd}%
  268. \def\@evenfoot{\@fancyfoot\@rodd\f@ncyelf\f@ncyecf\f@ncyerf\@lodd}%
  269. }
  270. \def\@lodd{\if@reversemargin\hss\else\relax\fi}
  271. \def\@rodd{\if@reversemargin\relax\else\hss\fi}
  272. \newif\iffootnote
  273. \let\latex@makecol\@makecol
  274. \def\@makecol{\ifvoid\footins\footnotetrue\else\footnotefalse\fi
  275. \let\topfloat\@toplist\let\botfloat\@botlist\latex@makecol}
  276. \def\iftopfloat#1#2{\ifx\topfloat\empty #2\else #1\fi}
  277. \def\ifbotfloat#1#2{\ifx\botfloat\empty #2\else #1\fi}
  278. \def\iffloatpage#1#2{\if@fcolmade #1\else #2\fi}
  279. \newcommand{\fancypagestyle}[2]{%
  280. \@namedef{ps@#1}{\let\fancy@def\def#2\relax\ps@fancy}}