2

I construct an article with three kinds of pagenumber style.

- fist part: \pagenumbering{Roman}

- Sencond part: \pagenumbering{arabic}

- third part: \pagenumbering{Alpha}

Q1:

How can I get the sum of pages of each part? For example, there are 3 pages in the first part with pagenumber style Roman, and 5 pages in the second part with pagenumber style arabic. The answer to this question will be used in the Q2.

Q2:

In the foot of every page in each part, I'd like typeset the page number like this: Page\ \thepage\ of\ \totalpages{Roman}(or arabic, Alpha for related part). I know @abspage@last, but it just give a total page number of an whole article. So is it possible to make a macro like \totalpages{Roman/arabic/Alpha...} to achieve this? No other packages preferred.

Example:

\documentclass{article}
\usepackage{geometry,fancyhdr}
\pagestyle{fancy}\fancyhf{}

\newcommand\totalpages[1]{...}

\begin{document}
part 1:
\pagenumbering{Roman}
\cfoot{Page\ \thepage\ of\ \totalpages{Roman}}
...
part 2:
\pagenumbering{arabic}
\cfoot{Page\ \thepage\ of\ \totalpages{arabic}}
...
part 3: \pagenumbering{Alpha}
\cfoot{Page\ \thepage\ of\ \totalpages{Alpha}}
...
\end{document}
2
  • Would that be "Page I of X" or "Page I of 10"? Sep 18, 2022 at 13:14
  • Both are all right and preferred.
    – lyl
    Sep 18, 2022 at 13:48

1 Answer 1

1

EDIT: I have added an alternative solution (currently commented out) that gives Page A of 4 instead of Page A of D. It needs some hackery, using LaTeX internals, however.

Here is a solution with an extra macro \changepagenumbering. This remembers the last page number of the previous chunk and puts it in a label. It also does a\newpage, because changing page numbers mid-page doesn't make sense. Maybe it also works without this \newpage. I then use \pageref to pick up this number in \totalpages. You will need at least one extra LaTeX run to get the numbers correct.

\documentclass{article}
\usepackage{geometry,fancyhdr}
\pagestyle{fancy}\fancyhf{}
\usepackage{lipsum}

% Start a new page numbering scheme. Remember the number of pages of the
% previous scheme and start a new page (changing page numbering mid-page
% doesn't make sense)
%
\newcommand\previousnumbering{}
\newcommand\changepagenumbering[1]{%
  \label{last-pagenumber-of-\previousnumbering}%
  \newpage
  \renewcommand\previousnumbering{#1}%
  \pagenumbering{#1}%
}
\newcommand\totalpages[1]{\pageref{last-pagenumber-of-#1}}

% If you want the number of pages in decimal use this instead of the above:

% \newcommand\mypagecount{\arabic{page}}
% \makeatletter
% \newcommand\mylabel[1]{\@bsphack
%   \protected@write\@auxout{\let\mypagecount\relax}%
%          {\string\newlabel{#1}{{\@currentlabel}{\mypagecount}}}%
%   \@esphack}
% \makeatother
% \newcommand\previousnumbering{}
% \newcommand\changepagenumbering[1]{%
%   \mylabel{last-pagenumber-of-\previousnumbering}%
%   \newpage
%   \renewcommand\previousnumbering{#1}%
%   \pagenumbering{#1}%
% }
% \newcommand\totalpages[1]{\pageref{last-pagenumber-of-#1}}

\begin{document}
\changepagenumbering{Roman}
\cfoot{Page\ \thepage\ of\ \totalpages{Roman}}
\section{part 1}

\lipsum

\changepagenumbering{arabic}
\cfoot{Page\ \thepage\ of\ \totalpages{arabic}}
\section{part 2}

\lipsum[1-40]

\changepagenumbering{Alph}
\cfoot{Page\ \thepage\ of\ \totalpages{Alph}}
\section{part 3}

\lipsum[10-30]

% end with a dummy one
\changepagenumbering{arabic}
\end{document}

enter image description here

13
  • Would you kindly provide also a solution for ` Page I of 10`?
    – lyl
    Sep 20, 2022 at 2:20
  • That probably would require another package. Is that a problem? Sep 20, 2022 at 7:52
  • I prefer a solution non other packages. Or is it possible to get the absolute number of a middle page in article when there are two or more page number styles in it?
    – lyl
    Sep 20, 2022 at 8:02
  • 1
    Modern LaTeX has a counter totalpages, which is the total number of pages that has yet been output. It can be printed with \thetotalpages. However, when you are constructing the footer the current page has not yet been output, so it hasn't been counted yet. So on page 4, the counter is 3. You could use this to do the calculation, totalpages - the value at the end of the previous section. But notice: the calculated number must be put is a \label-like structure, written to the aux-file because it must be used before it has been calculated. Sep 20, 2022 at 8:35
  • I have added an alternative solution that gives decimal numbers. Sep 20, 2022 at 8:46

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .