5

I want to get two different page numbers on the same page in LaTeX. For example, I want to have overall page numbers in the footer and within chapter page numbers in the header. I wonder how to get this one. Any help will be highly appreciated. Thanks.

% Document Class
\documentclass[a4paper,12pt]{book}
\usepackage[english]{babel}
\usepackage{blindtext}

\usepackage{fancyhdr}
\usepackage{lastpage}

\fancypagestyle{Special}{%
\fancyhf{}% Clear fancy header/footer
\renewcommand{\headrulewidth}{0.7pt}
\renewcommand{\footrulewidth}{0.7pt}
\fancyhead[r]{Page \thepage\ of \pageref{LastPage}}
\fancyfoot[r]{Page \thepage\ of \pageref{LastPage}}
}


% Begin Document
\begin{document}

\tableofcontents

\mainmatter
\pagestyle{Special}

\chapter{First Chapter}
\section{Introduction}

\subsection{Intro1}
\subsection{Intro2}

% Blind Text
\blindtext[1]

\section{Material \& Method}

% Blind Text
\blindmathpaper

\chapter{Second Chapter}
\section{Introduction}

\subsection{Intro1}
\subsection{Intro2}

% Blind Text
\blindtext[1]

\section{Material \& Method}

% Blind Text
\blindmathpaper

% End Document
\end{document}
8

I introduce a \newcounter{pagecntr}[chapter] which will be set to zero for every new chapter. I did not manage to set it directly to 1. That's why I needed to do the ugly \setcounter{temp}{\thepagecntr}\stepcounter{temp}\thetemp every time I want to print it. I guess, there are better approaches, but well... doesn't matter, it works.

The final trick is to use the package everyshi which increments my new counter by one for every page.

 % arara: pdflatex

\documentclass[a4paper,12pt]{book}
\usepackage[english]{babel}
\usepackage{blindtext}

\usepackage{fancyhdr}
\usepackage{lastpage}
\usepackage{everyshi}
\newcounter{pagecntr}[chapter]
\EveryShipout{\stepcounter{pagecntr}}
\newcounter{temp}

\fancypagestyle{Special}{%
    \fancyhf{}% Clear fancy header/footer
    \renewcommand{\headrulewidth}{0.7pt}
    \renewcommand{\footrulewidth}{0.7pt}
    \fancyhead[r]{Page \setcounter{temp}{\thepagecntr}\stepcounter{temp}\thetemp}
    \fancyfoot[r]{Page \thepage\ of \pageref{LastPage}}
}

\begin{document}    
    \tableofcontents    
    \mainmatter
    \pagestyle{Special}     
    \chapter{First Chapter}
    \section{Introduction}  
    \subsection{Intro1}
    \subsection{Intro2} 
    \blindtext[1]
    \section{Material \& Method}    
    \blindmathpaper 
    \chapter{Second Chapter}
    \section{Introduction}  
    \subsection{Intro1}
    \subsection{Intro2} 
    \blindtext[1]   
    \section{Material \& Method}    
    \blindmathpaper 
\end{document}

enter image description here

NB: I would recommend to write something like \thetemp\ of Ch.\ \thechapter as two numbers on one page will be confusing if not explained.

  • Thanks @LaRiFaRi for your nice answer. Is this possible to have page numbers in header for some chapters and for others no page numbers in header? – MYaseen208 Aug 6 '15 at 15:58
  • @MYaseen208 just define a \fancypagestyle{evenMoreSpecial}{... with empty header and switch to \pagestyle{evenMoreSpecial} where ever you need. If you want something automatic, you have to do a follow up question. Please always keep in mind that your readers will have to understand your numbering. Sounds like a bit too much what you are doing right now. But well, you will have your reasons. – LaRiFaRi Aug 6 '15 at 22:18
2

Here is an additional suggestion using scrlayer-scrpage instead fancyhdr. The pagestyle scrheadings is defined without the chapter pagenumber in the header and an alias Normal is set for this pagestyle. Then scrheadings is cloned to page style Specialadding the chapter pagenumber in the header. The onbackground hook for @everystyle@ is used to step the chapter pagenumber on every page.

\documentclass[a4paper,12pt]{book}
\usepackage[english]{babel}
\usepackage{blindtext}

\usepackage{lastpage}
\newcounter{pagecntr}[chapter]

\setlength\headheight{14.5pt}
\usepackage[
  automark,
  markcase=upper,
  headsepline=.7pt,
  footsepline=.7pt
]{scrlayer-scrpage}
\AddToLayerPageStyleOptions{@everystyle@}{onbackground={\stepcounter{pagecntr}}}

\clearpairofpagestyles
\cfoot[\usekomafont{pagenumber}\thepage]{}
\refoot{\pagemark}
\rofoot{\pagemark}
\renewcommand*\pagemark{%
  \usekomafont{pagenumber}Page \thepage{} of \pageref{LastPage}%
}
\addtokomafont{pageheadfoot}{\upshape}

\DeclareNewPageStyleAlias{Normal}{scrheadings}

\newpairofpagestyles[scrheadings]{Special}{%
  \rohead{Page~\thepagecntr}%
  \rehead{Page~\thepagecntr}%
}

\begin{document}    
\tableofcontents    
\mainmatter

\chapter{First Chapter}
\section{Introduction}  
\subsection{Intro1}
\subsection{Intro2} 
\blindtext[1]
\section{Material \& Method}    
\blindmathpaper 

\chapter{Second Chapter}
\pagestyle{Special}
\section{Introduction}  
\subsection{Intro1}
\subsection{Intro2} 
\blindtext[1]   
\section{Material \& Method}    
\blindmathpaper 

\chapter{Second Chapter}
\pagestyle{Normal}
\section{Introduction}  
\subsection{Intro1}
\subsection{Intro2} 
\blindtext[1]   
\section{Material \& Method}    
\blindmathpaper 
\end{document}

enter image description here

  • Could you add the code which is needed for your output? – LaRiFaRi Aug 7 '15 at 7:20
  • @LaRiFaRi Oh sorry. Now the code is added. – esdd Aug 7 '15 at 8:44

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.