I want to \usepackage{fancyhdr} to list paragraph numbers in the header/footer of a document. Most of the time this works fine but when a new page starts with a paragraph, the number is also listed on the previous page. In the below screenshot this shows on page 8: paragraphs 12-16 are listed but only paragraphs 12-15 actually appear on the page. Any ideas on how to fix this problem?

enter image description here

This is the MWE:


% fancy headings as defined by the OP except we need to be more careful
% with plain headings
\fancypagestyle{plain}{% need to reset \firstpara on pages without headers
  \fancyhf{} % clear all header and footer fields
  \fancyfoot[C]{\bfseries \thepage}
   \def\firstpara{1}\def\lastpara{1}% reset counters each section


% now the macros for tracking the paragraph numbers
\def\firstpara{1}%    set to 1 by default
\def\lastpara{1}% set to 1 by default
   % if \firstpara=0 => new page => set \firstpara
   % if #2=0 => only #1 is meaningful to set \lastpara to this
   % otherwise use #2
  % if \firstpara=\lastpara print only \firstpara otherwise print a range
  % reset firstpara and lastpara

% back to the MWe





\chapter{A Chapter}

\section{First Section}







\section{Second Section}
















Just another paragraph withou number.







Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.