2

I've read through Macro that knows it is at the end of a section? and I'm trying to get the detection of top-of-page sections that wasn't fleshed out there.

On the other hand I'm fine with using standard headers/marks without changing the page layout, so perhaps the solution is simpler.

MWE

\documentclass{article}
\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhead{}
\fancyhead[L]{\topmark}
\usepackage{lipsum}

\begin{document} \section{One} \lipsum[1-6] \section{Two} \lipsum[7-9] \section{Three} \lipsum[10] \end{document}

\topmark works fine, but the previous section reminder is redundant on page 3. Within \fancyhead or similar how do I detect that the page begins with a new section and change behaviour?

For example, let's say I want to blank out the header (or print something else) for page 3, since there's no actual content continued from Section 2.

Current direction is to test for \pagetotal at the start of a sectioning command, but

\usepackage{etoolbox}
\pretocmd{\section}{\message{\the\pagetotal}}{}

reveals that it hasn't shipped the previous page yet at this point, if it's actual content that causes a page break, not a hard \pagebreak.

obscurans
  • 121

1 Answers1

1

I am not quite shure if I understand what the desired outcome is. In case you want to maintain the previous issued caption on pages that do not start with a new section you could use

\documentclass{article}
\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhead{}
\fancyhead[L]{\topmark}
\usepackage{lipsum}
\usepackage{etoolbox}
\usepackage{everyshi}

\pretocmd{\section}{% \ifdim\pagetotal=0pt% \ifnum\value{section}>0% \let\akttopmark\topmark \AtNextShipout{\let\topmark\akttopmark}% \fi% \fi% }{}

\begin{document} \section{One} \lipsum[1-6] \section{Two} \clearpage \section{Three} \end{document}

But in case you want the header on page 3 to be empty (to avoid repeated information on top of this page) just use

\documentclass{article}
\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhead{}
\fancyhead[L]{\topmark}
\usepackage{lipsum}
\usepackage{etoolbox}

\fancypagestyle{plain}{% \fancyhf{}% clear all header and footer fields \renewcommand{\headrulewidth}{0.4pt}% } \pretocmd{\section}{% \ifdim\pagetotal=0pt% \ifnum\value{section}>0% \thispagestyle{plain} \fi% \fi% }{}

\begin{document} \section{One} \lipsum[1-6] \section{Two} \clearpage \section{Three} \end{document}

which will clean the header just for this page.

However - the basic test is the part starting with \pretocmd{\section}% which is the test whether the section command is called on top of the page or not.

Edit:

Obviously the op is right stating in his comment that this does not work for the article class (but basically it does for scrbook). So using tracingpages and tracingmacros here is an adapted solution that works for the standard article class at least for me:

\documentclass{article}
\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhead{}
\fancyhead[L]{\topmark}
\usepackage{lipsum}
\usepackage{afterpage}
\usepackage{xpatch}

\fancypagestyle{plain}{% \fancyhf{}% clear all header and footer fields \renewcommand{\headrulewidth}{0.4pt}% }

\newdimen\pagetopsectres

\makeatletter \AtBeginDocument{% \pagetopsectres=\textheight \advance\pagetopsectres by 1\baselineskip

\apptocmd{@xsect}{%\showthe\textheight\showthe\pagetotal\showthe\baselineskip% \ifdim\pagetotal>\textheight% \ifdim\pagetotal<\pagetopsectres% \typeout{manipulating page header of next page}% \afterpage{\thispagestyle{plain}}% \fi% \fi% }{}{}}% \makeatother

\begin{document} \section{One} \lipsum[1-6] \section{Two} \lipsum[7-10] \section{Three} \lipsum[11-15] \section{Four} \lipsum[16-17]

%\par\hbox{} \end{document}

However be aware that this solution is

  1. fragile, so you have to check your output since the content of your document might be tricky (main issue here is that latex internally works with penalties you can not access directly (afaik) and therefor output is kind of asychronous to input)
  2. the solution will fail if you do not have a paragraph break on your last page if there is a section on the very top (replace \lipsum[16-17] by \lipsum[16] to check what this means). To overcome this issue you can go the dirty way and add a \par\hbox just before \end{document}.
AndiW
  • 786
  • Thanks for the pointer! I haven't gotten this to work yet, see complication. – obscurans Feb 16 '22 at 02:46
  • What exactly do you want to be the page design on pages 1-3 in your MWE? – AndiW Feb 16 '22 at 03:16
  • Page 1: irrelevant (say leave empty). Page 2: has header for Section 1. Page 3: empty. – obscurans Feb 16 '22 at 04:07
  • since you should use \clearpage rather than \pagebreak the answer's code should work... – AndiW Feb 18 '22 at 14:01
  • I've updated the MWE - the intent is to detect natural pagebreaks due to content. If I'm manually page breaking, I already know the next page starts with a section on top, and I might as well patch \clearpage directly instead – obscurans Feb 21 '22 at 03:05
  • answer updated... – AndiW Feb 21 '22 at 06:20
  • note: whenever you force a pagebrake using \clearpage you should use \clearpage\thispagestyle{plain} since the method of the answer does not check the call of \section just after a \clearpage (which should not be an issue). – AndiW Feb 22 '22 at 21:25