-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestfancyhfinit.tex
137 lines (112 loc) · 4.61 KB
/
testfancyhfinit.tex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
\documentclass[openany]{book}
\usepackage{lipsum}
\usepackage{xcolor}
\usepackage{boxedminipage}
\usepackage{ifthen}
\usepackage{refcount}
\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhead[LE,RO]{\textsl{\rightmark}}
\fancyhead[LO,RE]{\textsl{\leftmark}}
\fancyfoot[C]{\thepage}
\renewcommand{\chaptermark}[1]{\markboth{#1}{}}
\renewcommand{\sectionmark}[1]{\markright{#1}}
\fancypagestyle{myfancy}{
\fancyhead[LE,RO]{MYFANCY SECTION}
% The rest is inherited from fancy
}
\fancypagestyle{switch}{
\fancyheadinit{%
\ifthenelse{\value{page}=\getpagerefnumber{otherpagestyle}}
{\color{red}\Large
\renewcommand{\headruleskip}{6pt}}
{}
}
\fancyfootinit{%
\ifthenelse{\value{page}=\getpagerefnumber{otherpagestyle}}
{\color{blue}%
\renewcommand{\footrulewidth}{4pt}%
}
{}
}
\fancyhead[LE,RO]{%
\ifthenelse{\value{page}=\getpagerefnumber{otherpagestyle}}
{MYFANCY SECTION}
{\textsl{\rightmark}}}
% The rest is inherited from fancy
}
\begin{document}
\tableofcontents
\newpage
\thispagestyle{plain}
\noindent
\begin{boxedminipage}{\textwidth}
This is a test for \verb|\fancyheadinit| and \verb|\fancyfootinit|
It demonstrates \verb|\thispagestyle|, both a good and a bad example.
It also gives a better solution to change the page style in the middle of a text.
The normal page style in this document is:
\begin{verbatim}
\pagestyle{fancy}
\fancyhead[LE,RO]{\textsl{\rightmark}}
\fancyhead[LO,RE]{\textsl{\leftmark}}
\fancyfoot[C]{\thepage}
\renewcommand{\chaptermark}[1]{\markboth{#1}{}}
\renewcommand{\sectionmark}[1]{\markright{#1}}
\end{verbatim}
We use the following additional page style \texttt{myfancy}. It just puts ``MYFANCY SECTION'' instead of the section title:
\begin{verbatim}
\fancypagestyle{myfancy}{
\fancyhead[LE,RO]{MYFANCY SECTION}
% The rest is inherited from fancy
}
\end{verbatim}
And for selecting a different page style, for the page where a specific text appears we put a label \texttt{otherpagestyle} in the text and then use a definition in the header that chosses either the normal value or the special value depending on the page number:
\begin{verbatim}
\fancypagestyle{switch}{
\fancyhead[LE,RO]{%
\ifthenelse{\value{page}=\getpagerefnumber{otherpagestyle}}
{MYFANCY SECTION}
{\textsl{\rightmark}}}
% The rest is inherited from fancy
}
\end{verbatim}
\end{boxedminipage}
\chapter{Introduction}
\lipsum[1]
\section{The Problem}
\label{sec:problem}
\lipsum[2-3]
\section{Evaluation}
\lipsum[3-5]
\newpage
\section{Using conditional code in the page style}
In this section we a better solution to the \verb|\thispagestyle{myfancy}| in the middle of the text. We set a label in the text that we want the different page style. Then we will use a test in our page style, and use different values depending on the page number. If the page number is equal to the page number of the label we use \verb|\pagestyle{myfancy}|, otherwise the default \verb|\pagestyle{fancy}|.
But it appears that this text is close to the end of the page. So in this case \TeX{} is still on the current page when it encounters the \verb|\thispagestyle{myfancy}| command. That means \texttt{this page will get the myfancy header}. However, during the page breaking \TeX{} decides that the text will not fit on the page and moves it to the next page. Now the different header is no longer at the page where it says it should be.
\bigskip
\lipsum[6-8]
\bigskip
Suspendisse vel felis. Ut lorem lorem, interdum eu, tincidunt sit amet, laoreet vitae, arcu. Aenean faucibus pede eu ante. Praesent enim elit, rutrum at, molestie non, nonummy vel, nisl. Ut lectus eros, malesuada sit amet, fer- mentum eu, sodales cursus, magna. Donec eu purus. Quisque vehicula, urna sed ultricies auctor, pede lorem egestas dui, et convallis elit erat sed nulla. Donec luctus. Curabitur et nunc.
\pagestyle{switch}
\noindent
\begin{minipage}{\linewidth}
\textbf{This page should have a different header than the surrounding
pages.
\label{otherpagestyle}
It is done with the} \verb|\pagestyle{switch}| \textbf{command, that
has tests in the header field definitions. This chooses the actual
header depending on the page number.}
\end{minipage}
\nopagebreak
% Set the header height because this particular header is taller
\addtolength{\headheight}{12.0pt}%
\addtolength{\topmargin}{-12.0pt}%
\bigskip
\lipsum[8-9]
\newpage
% Reset header height to its original value
\addtolength{\headheight}{-12.0pt}%
\addtolength{\topmargin}{12.0pt}%
\textbf{This page should get the normal header again because the page number is different than the page number in the label.}
\bigskip
\lipsum[10]
\end{document}