\thispagestyle
is executed on the correct pageThe LaTeX statement \thispagestyle has the problem that it changes the page style of the page on which the statement is executed instead of the page that is active at the time the corresponding position is output.
As a result, in the following example:
\documentclass{scrreprt}
\RedeclareSectionCommand[%
pagestyle=headings
]{chapter}
\pagestyle{headings}
\usepackage{blindtext}
\begin{document}
\chapter{Test Chapter}
\Blindtext[5]\thispagestyle{plain}
\vspace*{5\baselineskip}
\Blindtext[3]
\section{Test Section}\thispagestyle{empty}
\Blindtext
\blinddocument
\end{document}
the second page does not show the plain
page style of the first \thispagestyle
statement, but the empty page style of the second \thispagestyle
statement, although the ”Test Section“ section is output on the third page. The reason for this is that \thispagestyle
is based on a global switch and a global macro that is evaluated in the output routine. The \thispagestyle{empty}
defines both before it is known that the \section
heading causes a page break. Therefore, it affects the page before the heading, although the intention would clearly be the change on the page with the heading.
The trick now is to delay \thispagestyle
until the page is output. A marker is set for this:
\NewMarkClass{specialpage}
\newcommand*{\savethispagestyle}[1]{%
\InsertMark{specialpage}{#1}%
}
and immediately before the output of a page via hook build/page/reset
, if this marker is not empty, the last of these markers of the current page is used for \thispagestyle
:
\AddToHook{build/page/reset}{%
\Ifstr{\LastMark{specialpage}}{}{}{%
\thispagestyle{\LastMark{specialpage}}% last specialpage marker is not empty
\InsertMark{specialpage}{}%
}%
}
Now replace at least the \thispagestyle{empty}
statement after \section{Test Section}
with \savethispagestyle{empty}
in the above example:
\documentclass{scrreprt}
\NewMarkClass{specialpage}
\newcommand*{\savethispagestyle}[1]{%
\InsertMark{specialpage}{#1}%
}
\AddToHook{build/page/reset}{%
\Ifstr{\LastMark{specialpage}}{}{}{%
\thispagestyle{\LastMark{specialpage}}%
\InsertMark{specialpage}{}%
}%
}
\RedeclareSectionCommand[%
pagestyle=headings
]{chapter}
\pagestyle{headings}
\usepackage{blindtext}
\begin{document}
\chapter{Test Chapter}
\Blindtext[5]\savethispagestyle{plain}
\vspace*{5\baselineskip}
\Blindtext[3]
\section{Test Section}\savethispagestyle{empty}
\Blindtext
\blinddocument
\end{document}
As expected, page 2 shows the page style plain
and page 3 with \section
heading the page style empty
.
If you want to use this solution with the standard classes, you need the scrbase
package for the definition of \Ifstr
. It should also be noted that \IfArgIsEmpty
does not work at this point, as the argument must first be fully expanded before it is tested to see if it is empty.
Incidentally, the question also answers how to link \thispagestyle{...}
to \section
:
\documentclass{scrreprt}
\NewMarkClass{specialpage}
\newcommand*{\savethispagestyle}[1]{%
\InsertMark{specialpage}{#1}%
}
\AddToHook{build/page/reset}{%
\Ifstr{\LastMark{specialpage}}{}{}{%
\thispagestyle{\LastMark{specialpage}}%
\InsertMark{specialpage}{}%
}%
}
% Pages with chapter headings in `empty` page style:
\RedeclareSectionCommand[pagestyle=empty]{chapter}
% Pages with section headings in `empty` page style:
\AddtoDoHook{heading/endgroup/section}{\savethispagestyle{empty}\csname @gobble\endcsname}
\pagestyle{headings}
\usepackage{blindtext}
\begin{document}
\chapter{Test Chapter}
\Blindtext[5]
\vspace*{5\baselineskip}
\Blindtext[3]
\section{Test Section}
\Blindtext
\blinddocument
\end{document}
Via the do-hook heading/endgroup/section
, \savethispagestyle{empty}
is automatically executed immediately after the processing of the \section
heading. The \csname @gobble\endcsname
at the end of the do-hook definition ensures that the argument passed to the do-hook is ”eaten“. This solution only works with a KOMA-Script class, as only these have do-hooks. With the standard classes, you would have to find another way to execute \savethispagestyle{empty}
at the end of \section{...}
instead.