[Tex/LaTex] How to customize page numbering as “Chapter number + page number” in book class

front-matterpage-numberingsectioning

I am writing my final thesis, and I want to change the default page numbering like (also in TOC):

1. Introduction             1-1
   1.1 Background ......... 1-1
   1.1 Object ......... ... 1-2
   1.1 Outline ............ 1-4

2. State of the art         2-1
   2.1 Introduction ....... 2-1
   2.1 Blabla ......... ... 2-4
   2.1 Blabla ............. 2-18

  ...

Reference                   R-1
Appendix A                  A-1
Appendix B                  A-3

where R and A stand for Reference and Appendix respectively.

Using fancyhdr:

\fancyhead[LE]{ \thechapter - \thepage }
\fancyhead[RO]{ \thechapter - \thepage }

Where number 5 is the first page of chapter 2 and it should be 2-1, the second page number should be like 2-2 instead of 2-6. Besides, the Roman page number in frontmatter becomes like 0-XV etc.

enter image description here

I have been looking through some relevant posts, but all of them can not solve this issue.

What I want is to make the whole document page numbering as Chapter No. + page No. staring with chapter No. + 1, and use the default Roman number for frontmatter pages without chapter No.

So how to figure this out?

Best Answer

Redefine \thepage at the start of \mainmatter, and change the definition of the internal \@chapter macro so that it will also reset the page counter to 1 if the mainmatter switch is true. (Note that \clearpage/\cleardoublepage is called before \@chapter.)

\documentclass{book}

\usepackage{etoolbox}

\makeatletter
\appto{\mainmatter}{\renewcommand*{\thepage}{\thechapter-\arabic{page}}}
\patchcmd{\@chapter}{\if@mainmatter}{\if@mainmatter\setcounter{page}{1}}{}{}
% If \backmatter is used
\appto{\backmatter}{%
  \renewcommand*{\thepage}{\arabic{page}}%
  \setcounter{page}{1}%
}
\makeatother

\usepackage{lipsum}

\begin{document}

\frontmatter

\tableofcontents

\chapter{Preface}

\lipsum[1-3]

\mainmatter

\chapter{foo}

\section{foobar}

\lipsum[1-12]

\chapter{gnu}

\section{gnugnat}

\lipsum[1-12]

\cleardoublepage
\renewcommand*{\thepage}{R-\arabic{page}}
\setcounter{page}{1}

\addcontentsline{toc}{chapter}{\bibname}% optional

% "Fake" Bibliography chapter
\begin{thebibliography}{9}
\bibitem{A01} A bibitem.
\end{thebibliography}

\cleardoublepage
\renewcommand*{\thepage}{\thechapter-\arabic{page}}
\appendix

\chapter{appfoo}

\lipsum[1-12]

\end{document}​

EDIT: Here's an alternative if you insist on manually "numbering" your appendix chapters. Also note that your updated page numbering scheme is inconsistent (some, but not all chapters start with page [Prefix-]1).

\documentclass{book}

\usepackage{etoolbox}

\makeatletter
\appto{\mainmatter}{\renewcommand*{\thepage}{\thechapter-\arabic{page}}}
\patchcmd{\@chapter}{\if@mainmatter}{\if@mainmatter\setcounter{page}{1}}{}{}
\makeatother

\usepackage{lipsum}

\begin{document}

\frontmatter

\tableofcontents

\chapter{Preface}

\lipsum[1-3]

\mainmatter

\chapter{foo}

\section{foobar}

\lipsum[1-12]

\chapter{gnu}

\section{gnugnat}

\lipsum[1-12]

\cleardoublepage
\renewcommand*{\thepage}{R-\arabic{page}}
\setcounter{page}{1}

\addcontentsline{toc}{chapter}{\bibname}% optional

% "Fake" Bibliography chapter
\begin{thebibliography}{9}
\bibitem{A01} A bibitem.
\end{thebibliography}

\cleardoublepage
\renewcommand*{\thepage}{A-\arabic{page}}
\setcounter{page}{1}
\backmatter

\chapter{Appendix~A}

\lipsum[1-12]

\chapter{Appendix~B}

\lipsum[1-12]

\end{document}