2

I want to keep everything in one master file and require to start new page numbers with lastpage also after each \newpage command. MWE is below. Any help will be highly appreciated. Thanks

\documentclass[12pt,english]{article}
\usepackage[T1]{fontenc}
\usepackage[latin9]{inputenc}
\usepackage[a4paper]{geometry}
\geometry{verbose,tmargin=1cm,bmargin=1.5cm,lmargin=1cm,rmargin=1cm}
\usepackage{fancyhdr}
\pagestyle{fancy}
\usepackage{array}

\makeatletter


\usepackage{lastpage} % number of last page 

\fancyhf{}
\fancyhead[C]{}
\renewcommand{\headrulewidth}{0pt}
\renewcommand{\footrulewidth}{0.5pt}
\fancyfoot[R]{Page \thepage\ of \pageref{LastPage}}
%\fancyfoot[L]{\today}


\usepackage{babel}
\usepackage{blindtext}

% Begin Document
\begin{document}


\section{Introduction}

\subsection{Intro1}

% Blind Text
\blindtext[1]

\section{Material \& Method}

% Blind Text
\blindmathpaper


\section{Introduction}

\subsection{Intro1}

% Blind Text
\blindtext[1]

\newpage

\section{Material \& Method}

% Blind Text
\blindmathpaper

\blindtext[1]

\blindmathpaper


% End Document
\end{document}
  • And what is the problem? Your MWE work fine, page numbering is as expected: from Page 1 of 5 to Page 5 of 5. – Zarko Nov 10 '15 at 10:30
  • I require 1 of 2 and 2 of 2 before \newpage command and then require 1 of 3, 2 of 3 and 3 of 3. – MYaseen208 Nov 10 '15 at 10:33
  • This is not clear from your question ... add your comment to it. \newpage even \clearpage or\doubleclearpage doesn't discontinues numbering. Fro this You need special macro. If you will not receive an answer before, I will write one afternoon (now I'm busy with other tasks). – Zarko Nov 10 '15 at 10:40
  • page is a very unreliable counter so this not that easy! – user31729 Nov 10 '15 at 10:46
  • @ChristianHupfer, I know, a long time ago I design such macro with new counter. Now is my problem to find it :-). – Zarko Nov 10 '15 at 10:53
3

To avoid confusion, I created macros \newgroup to reset the page counter and \thelastpage to print the last page of a "group". The tricky bit was setting the LastPage label before the end of the page but not changing the counters until after the end of the page (when the header and footer are created).

\documentclass[12pt,english]{article}
\usepackage[T1]{fontenc}
\usepackage[latin9]{inputenc}
\usepackage[a4paper]{geometry}
\geometry{verbose,tmargin=1cm,bmargin=1.5cm,lmargin=1cm,rmargin=1cm}
\usepackage{fancyhdr}
\pagestyle{fancy}
\usepackage{array}

\newcounter{group}

\makeatletter
\newcommand{\newgroup}{%
  \let\@currentlabel=\empty
  \label{LastPage\thegroup}% \pageref doesn't need a \refstepcounter
  \newpage
  \stepcounter{group}%
  \setcounter{page}{1}%
}
\newcommand{\thelastpage}{\@ifundefined{r@LastPage\thegroup}%
  {\pageref{LastPage}}{\pageref{LastPage\thegroup}}}
\makeatother

\usepackage{lastpage} % number of last page 

\fancyhf{}
\fancyhead[C]{}
\renewcommand{\headrulewidth}{0pt}
\renewcommand{\footrulewidth}{0.5pt}
\fancyfoot[R]{Page \thepage\ of \thelastpage}
%\fancyfoot[L]{\today}


\usepackage{babel}
\usepackage{blindtext}

% Begin Document
\begin{document}


\section{Introduction}

\subsection{Intro1}

% Blind Text
\blindtext[1]

\section{Material \& Method}

% Blind Text
\blindmathpaper


\section{Introduction}

\subsection{Intro1}

% Blind Text
\blindtext[1]

\newgroup

\section{Material \& Method}

% Blind Text
\blindmathpaper

\blindtext[1]

\blindmathpaper


% End Document
\end{document}

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.