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
- 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)
- 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}.
\clearpageyou should use\clearpage\thispagestyle{plain}since the method of the answer does not check the call of\sectionjust after a\clearpage(which should not be an issue). – AndiW Feb 22 '22 at 21:25