[Tex/LaTex] Two sections on one page make fancyhdr takes the second one as page header

fancyhdrheader-footer

I have two sections on one page. Unfortunately fancyhdr takes the second one as the page header, which will result in something like seen in the image below.

I want to have the first section as the header title (in this case: Introduction).

enter image description here

Here is the code for the above image:

\documentclass[12pt, oneside]{article}
\usepackage{fancyhdr}
\pagestyle{fancy}
\lhead{\fancyplain{}{\leftmark }}
\rhead{}
\usepackage{lipsum} %only for dummy text

\begin{document}
    \section{Introduction}
        \lipsum[2]
    \section{Overview}
        \lipsum[2]
\end{document}

Best Answer

titleps - as an alternative to fancyhdr - provides \toptitlemarks and \bottitlemarks to identify the top/bottom titles on a page, respectively. It should not be difficult to switch to titleps, in my opinion. Here's a short example that illustrates this (click the image to enlarge):

enter image description here

\documentclass[oneside]{article}
\usepackage{titleps,lipsum}% http://ctan.org/pkg/{titleps,lipsum}
\newpagestyle{main}{%
  \sethead
    {\toptitlemarks \thesection \quad \sectiontitle}% left
    {\thepage}% centre
    {\bottitlemarks \thesection \quad \sectiontitle}% right
  \setheadrule{.4pt}% Regular header rule
}
\pagestyle{main}
\begin{document}
\section{Introduction}
\lipsum[1-3]
\section{Overview}
\lipsum[4-6]
\section{Conclusion}
\lipsum[5-9]
\end{document}

The above MWE produces 3 pages with headers:

1 Introduction       1           2 Overview
...
2 Overview           2         3 Conclusion
...
3 Conclusion         3         3 Conclusion
...

Obviously page 3 presents a problem. However, you might not be interested in a dictionary-style header (showing both the first and last section on the page).

See the titleps documentation (section 4. Marks, p 4) for more information.