-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample35.tex
329 lines (250 loc) · 10.1 KB
/
example35.tex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
\documentclass{article}
\usepackage{lipsum}
\usepackage{boxedminipage}
\usepackage{ifthen}
\usepackage{afterpage}
\usepackage{fancyhdr}
\fancyhead{}
\fancyfoot[C]{\thepage}
\fancypagestyle*{intro}{%
\renewcommand{\headrule}{}
\fancyhead[R]{INTRODUCTION}
}
\fancypagestyle{contents}[intro]{\fancyhead[R]{CONTENTS}}
\fancypagestyle*{fancy}{%
\fancyhead[L]{\FirstMark{2e-left}}
\fancyhead[R]{\FirstMark{2e-right-nonempty}}
\fancyfoot[C]{\thepage}
}
\renewcommand{\sectionmark}[1]{%
\markboth{\thesection. #1}{}}
\renewcommand{\subsectionmark}[1]{%
\markright{\thesubsection. #1}}
% This is for the second half of the document.
\NewMarkClass{section}
\NewMarkClass{subsection}
\NewMarkClass{which}
\InsertMark{which}{0}
\NewMarkClass{whichpage}
\newcommand\whichpage{}
\newcommand{\DEBUG}[1]{\message{#1}\ignorespaces} % Choose this for debugging
%\newcommand{\DEBUG}[1]{} % Choose this for no debugging
\begin{document}
\pagestyle{intro}
\thispagestyle{contents}
\tableofcontents
\bigskip
\noindent
\begin{boxedminipage}{\textwidth}
This is example 35 in the \textsf{fancyhdr} documentation.
In this document we put the first section title (left) and the first subsection title (right) in the page headers. This is hard (probably impossible) to achieve with the traditional \LaTeX{} marks.
\textbf{NOTE: This requires a \LaTeX{} kernel that has the new marks mechanism (November 2022 or later).}
\medskip
This is the first attempt:
\begin{verbatim}
\usepackage{fancyhdr}
\fancypagestyle*{fancy}{%
\fancyhead[L]{\FirstMark{2e-left}}
\fancyhead[R]{\FirstMark{2e-right-nonempty}}
\fancyfoot[C]{\thepage}
}
\pagestyle{fancy}
\renewcommand{\sectionmark}[1]{%
\markboth{\thesection. #1}{}}
\renewcommand{\subsectionmark}[1]{%
\markright{\thesubsection. #1}}
\end{verbatim}
\end{boxedminipage}
\noindent
\begin{boxedminipage}{\textwidth}
However, in section~\ref{sec:ugly_inheritance}, page~\pageref{sec:ugly_inheritance} we have the section title from that section~\ref{sec:ugly_inheritance}, but a subsection title from the previous section. This is ugly. In order to solve this we first change the code to use our own marks, so that we have more control. We need a mark for the section title, and one for the subsection title.
\medskip
Interim code:
\begin{verbatim}
\NewMarkClass{section}
\NewMarkClass{subsection}
\fancypagestyle*{fancy2}{%
\fancyhead[L]{\FirstMark{section}}
\fancyhead[R]{\FirstMark{subsection}}
}
\pagestyle{fancy2}
\renewcommand{\sectionmark}[1]{%
\InsertMark{section}{\thesection. #1}}
\renewcommand{\subsectionmark}[1]{%
\InsertMark{subsection}{\thesubsection. #1}}
\end{verbatim}
Now we have to check whether there is a \verb|\section| command on the page that is not followed by a \verb|\subsection| command on the same page. This can't be checked with the marks that we currently have. So we define an additional mark `\texttt{which}' that contains the level of the last [sub]section command. We redefine \verb|\sectionmark| and \verb\subsectionmark\ to put ``0'' and ``1'', resp. in this mark, and then we check in the right header whether there is a subsection title on the page, and if the the last value of `\texttt{which}' on the page is ``0'' or ``1''.
\begin{verbatim}
\NewMarkClass{which}
\InsertMark{which}{0}% Initialize
\renewcommand{\subsectionmark}[1]{%
\InsertMark{subsection}{\thesubsection. #1}%
\InsertMark{which}{1}%
}
% \renewcommand{\sectionmark}, see below
\end{verbatim}
\end{boxedminipage}
\noindent
\begin{boxedminipage}{\textwidth}
In section~\ref{sec:missing} we use the extra code to check this `\texttt{which}' mark to select the proper subsection title.
In that section we also communicate to the header whether the section starts at or very near to the top of the page.
We also check whether the the section was pushed to the next page.
The mark \texttt{whichpage} contains the page number where the \verb|\section|
command was processed, whereas \verb|\thepage| is the page where the section
command actually appears. If it was pushed to a following page, these two are different.
Both these cases are stored in the variable \verb|\whichpage|, as a value that differs from \verb|\thepage| of the page where the section title appears.
See the \textsf{fancyhdr} documentation for the explanation.
\MakeLinkTarget{}\label{good_code}
\begin{verbatim}
\NewMarkClass{whichpage}
\AddToHook{cmd/section/before}{%
\ifthenelse{\lengthtest{\pagetotal<4\baselineskip}}{%
% Section header near top of page
\renewcommand{\whichpage}{top\thepage}%
}
{\renewcommand{\whichpage}{\thepage}}%
}
\renewcommand{\sectionmark}[1]{%
\InsertMark{section}{\thesection\ #1}%
\InsertMark{which}{0}%
\InsertMark{whichpage}{\whichpage}%
}
\end{verbatim}
The code that compares the page numbers can be seen in action in section~\ref{sec:push} on page~\pageref{sec:push}.
\end{boxedminipage}
\newpage
\pagestyle{fancy}
\section{Section One}
\subsection{Subsection One}
\textbf{NOTE: Sections 1--3 use the simple version of the headers, which gives an `ugly inheritance' on page~\pageref{ugly_page}.}
\smallskip
\lipsum[1-2]
\section{Section Two}
\lipsum[3]
\subsection{Subsection Two}
\label{sec:inherited}
\lipsum[4-7]
\section{Third section}
\label{sec:ugly_inheritance}
\textbf{This section ``inherits'' the subsection title from the
previous section (\ref{sec:inherited}), which is good on this page. But it should not be used on the next page.
It could even be a subsection title from several sections ago. We will repair this in the next section.}
\afterpage{\MakeLinkTarget{}\label{ugly_page}}
\medskip
\lipsum
\fancypagestyle*{fancy2}{%
\fancyhead[L]{\FirstMark{section}}
\fancyhead[R]{%
\IfMarksEqualTF{subsection}{top}{first}
{% no subsection mark on this page
\DEBUG{Page \thepage, no subsection mark on this page^^J}%
\ifthenelse{%
% previous page ended in a subsection
\TopMark{which}=1\and
% Is there a section on the page?
\equal{\IfMarksEqualTF{section}{top}{first}
{\thepage}{\FirstMark{whichpage}}}{\thepage}}%
{
\DEBUG{Page \thepage, previous page ended in a subsection^^J
and whichpage = \FirstMark{whichpage}}%
% use previous subsection unless suppressed by section on top
\TopMark{subsection}% = \FirstMark{subsection}%
}
% otherwise use empty right header
{\DEBUG{Page \thepage, empty right header^^J}}%
}
{% if there is a subsectionmark on the page, use it
\DEBUG{Page \thepage, there is a subsection mark on this page^^J}%
\FirstMark{subsection}%
}%
}
}
\pagestyle{fancy2}
% The counter 'thispage' contains the page number where the \section
% command was processed, whereas 'page' is the page where the section
% command actually appears.
\newcounter{thispage}% Only for the documentation
\AddToHook{cmd/section/before}{%
\DEBUG{Section \the\numexpr\value{section}+1\relax\space is processed on page \thepage,^^J
position \the\pagetotal.^^J}%
\ifthenelse{\lengthtest{\pagetotal<4\baselineskip}}{%
\DEBUG{Section header near top of page^^J}%
\renewcommand{\whichpage}{top\thepage}%
}%
{\renewcommand{\whichpage}{\thepage}}%
\setcounter{thispage}{\thepage}% Only for the documentation
}
\renewcommand{\sectionmark}[1]{%
\InsertMark{section}{\thesection. #1}%
\InsertMark{which}{0}%
\InsertMark{whichpage}{\whichpage}%
}
\renewcommand{\subsectionmark}[1]{%
\InsertMark{subsection}{\thesubsection. #1}%
\InsertMark{which}{1}%
}
\section{Fourth section}
\label{sec:missing}
From this page on we suppress the ``inheritance'' of a subsection title if
\begin{itemize}
\item the previous page ended with a section, not a subsection,
\item or the current page has a section title at or near the top.
\end{itemize}
On the current page there is no subsection, and the previous page ended on a section.
Therefore this page will have an empty subsection title in the right header.
\bigskip
Besides the code on page~\pageref{good_code} we have the following definition of the headers (NOTE: the actual code also contains debugging commands). For explanation, see Example 35 in the \textsf{fancyhdr} documentation, in the section ``I want the first section and the first subsection in my headers''.
\begin{verbatim}
\fancypagestyle*{fancy2}{%
\fancyhead[L]{\FirstMark{section}}
\fancyhead[R]{%
\IfMarksEqualTF{subsection}{top}{first}
{% no subsection mark on this page
\ifthenelse{%
% previous page ended in a subsection
\TopMark{which}=1\and
% Is there a section on the page?
\equal{\IfMarksEqualTF{section}{top}{first}
{\thepage}{\FirstMark{whichpage}}}{\thepage}}%
{
% use previous subsection unless suppressed by section on top
\TopMark{subsection}%
}
% otherwise use empty right header
{}%
}
{% if there is a subsectionmark on the page, use it
\FirstMark{subsection}%
}%
}
}
\pagestyle{fancy2}
\end{verbatim}
The suppression of the inherited right header can be seen on the pages \pageref{sec:push} and \pageref{sec:newpage}.
\medskip
\lipsum[1-3]
\subsection{A Fourth Subsection}
\lipsum[8-11]
\section{Fifth section}
\lipsum[1-7]
\subsection{Fifth subsection}
\lipsum[8-17]
\section{Sixth section}
\label{sec:push}
{\bfseries This section is processed at the bottom of page~\thethispage. But as you can see it has been pushed to page~\pageref{sec:push}.}\\
Therefore the code that checks if \verb|\FirstMark{whichpage}| is equal to \verb|\thepage| suppresses the right header
(\verb|\FirstMark{whichpage}| = \thethispage{} and \verb|\thepage| = \pageref{sec:push}).
\medskip
\lipsum[1-6]
\subsection{Sixth subsection}
\lipsum[7]
\newpage
\section{Seventh section after a page break}
\label{sec:newpage}
Because this section starts at the top of the page, and there is no subsection on this page, the right header is also empty.
\medskip
\lipsum[8-10]
\end{document}
https://tex.stackexchange.com/q/586066/113546
Answer:
https://tex.stackexchange.com/a/587094/113546