[Tex/LaTex] How to make page numbering continuous after a second \mainmatter

front-matterpage-numbering

I am writing a document with multiple \mainmatters and \backmatters.
The structure is basically:

...
\mainmatter
\backmatter
...
\mainmatter
\backmatter
...

Is there a simple way of preventing LaTeX to reset the page counter?
Also \backmatter should not reset the counter.

Best Answer

This depends highly on the document class you're using. Taking the standard book as base, \fontmatter and \mainmatter both reset the page counter. \backmatter on the other hand doesn't:

\newcommand\frontmatter{%
    \cleardoublepage
  \@mainmatterfalse
  \pagenumbering{roman}}
\newcommand\mainmatter{%
    \cleardoublepage
  \@mainmattertrue
  \pagenumbering{arabic}}
\newcommand\backmatter{%
  \if@openright
    \cleardoublepage
  \else
    \clearpage
  \fi
  \@mainmatterfalse}

The important thing that causes the page numbering to be reset when issuing the above commands is \pagenumbering (taken from latex.ltx):

\def\pagenumbering#1{%
  \global\c@page \@ne \gdef\thepage{\csname @#1\endcsname
   \c@page}}

As mentioned, this sets the page counter to one (\global\c@page \@ne) and redefines the representation \thepage (\gdef\thepage{\csname @#1\endcsname\c@page}). So, with an issue of \pagenumbering{roman} (as is performed by \frontmatter), LaTeX issues something equivalent to

\setcounter{page}{1}
\renewcommand{\thepage}{\arabic{page}}

memoir does something similar. If your question is only "How to prevent LaTeX from resetting the page counter?", then you should not use \frontmatter and friends, or at least void what \pagenumbering is doing. You might just be better off redefining the page counter printing mechanism \thepage as is done above. Or, for convenience of use in order to stick to your current layout, perhaps the following suffices in your preamble (which removes the page counter resetting):

\makeatletter
\def\pagenumbering#1{%
  \gdef\thepage{\csname @#1\endcsname \c@page}}
\makeatother

Finally, neither book nor memoir resets the page counter for \backmatter.