How to customize page numbering as “Chapter number + page number” in book class?

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 }


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.

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?

-
\renewcommand\thepage{\arabic{chapter}-\arabic{page}} –  Ulrike Fischer Feb 4 '13 at 9:00
the chappg package does this for you (and deals with a number of consequent edge effects). –  wasteofspace Feb 4 '13 at 9:03
@wasteofspace Make that an answer, please. –  lockstep Feb 4 '13 at 9:13
@UlrikeFischer, I tried but it does not work. –  KOF Feb 4 '13 at 9:20
@KOF You don't really want that, do you? –  egreg Feb 4 '13 at 10:26

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}

% "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}

% "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}

-
Thanks, it worked. By the way, can you please add the numbering Reference and Appendix for backmatter. For Reference no chapter name, and for Appendix A/B it's like A-1, A-2.... B-1, B-2... –  KOF Feb 4 '13 at 9:44
@KOF Does the "Reference" chapter appear before the appendix chapters? In that case, one should use a numbering scheme like "Ref-1" for the sake of consistency. –  lockstep Feb 4 '13 at 10:08
Reference is before backmatter, while Appendix is after backmatter. There is also a minor issue, the heading of Appendix is like: Chapter Appendix A, where Appendix A is the Appendix name, How to get ride of the Chapter ? –  KOF Feb 4 '13 at 10:13
@KOF It's unclear why you use \backmattter at all. I will update my answer to include "Reference" plus appendices. Maybe my solution will also be sufficient for the "Chapter" prefix problem -- note that this is a different question (with existing answers at tex.sx). –  lockstep Feb 4 '13 at 10:18