%  Define \thepage to output chapter-page (such as 1-3, 5-2, etc.)
%  and have each chapter's page number begin on 1.
%
% Largely reimplemented for use as a LaTeX2e package, taking account
% of \mainmatter, etc., and providing various extra capabilities,
% Robin Fairbairns <rf@cl.cam.ac.uk>, from an original by
% Max Hailperin <max@nic.gac.edu>, who asserted that the original
% source was TeXMaG Vol. 5, No. 3 (part 1)
%
% See documentation after \endinput
%
% This program may be distributed and/or modified under the
% conditions of the LaTeX Project Public License, either version 1.3
% of this license or (at your option) any later version.
% The latest version of this license is in
%   http://www.latex-project.org/lppl.txt
% and version 1.1 or later is part of all distributions of LaTeX 
% version 1999/06/01 or later.
%
% This program consists of the file chappg.sty

\NeedsTeXFormat{LaTeX2e}% Seems to work with any version
\ProvidesPackage{chappg}[2004/07/25 v2.1 page numbering by chapter]

\newif\if@chappg@auto      \@chappg@autofalse
\newif\if@chappg@resetting \@chappg@resettingfalse

\DeclareOption{auto}{\@chappg@autotrue}
\DeclareOption{noauto}{\@chappg@autofalse}
\ExecuteOptions{auto}% compatibility with previous version
\ProcessOptions

%
% replace \cs{pagenumbering} so as to have the optional argument...
\renewcommand\pagenumbering[2][\@chappg@thechapter]{%
  \global\c@page\@ne
  \protected@xdef\thepage{%
    \expandafter\noexpand\csname @#2\endcsname
      \noexpand\c@page
  }%
  \gdef\@chappg@prefix{#1}%
%
% and now make sure that page numbering resetting is in its right
% state
  \def\@tempa{#2}%
  \def\@tempb{bychapter}%
  \ifx\@tempa\@tempb
    \if@chappg@resetting
    \else
      \@addtoreset{page}{chapter}%
      \@chappg@resettingtrue
    \fi
  \else
    \if@chappg@resetting
      \@removefromreset{page}{chapter}%
      \@chappg@resettingfalse
    \fi
  \fi
}

% This is the command used by \pagenumbering{bychapter} to actually
% create the page number
\newcommand\@bychapter[1]{%
  \@chappg@prefix\chappgsep\@arabic#1%
  \ifx\@chappg@prefix\@chappg@surprise
    \if@chappg@surprised\else
      \xdef\@chappg@surprise@line{\the\inputlineno}%
      \global\@chappg@surprisedtrue
    \fi
  \fi  
}

% output a chapter number if there have been any chapters; otherwise
% gobble the following thing, which will be \chappgsep
\def\@chappg@thechapter{%
  \ifnum\c@chapter=0
    \expandafter\@gobble
  \else
    \thechapter
  \fi
}

% separator in the above
\providecommand\chappgsep{-}

%
% select default: this will be hacked by \frontmatter if the author
% uses it; if noauto option was given, the user has opted to execute
% this command hirself, but otherwise we behave backwards-compatibly
\if@chappg@auto\pagenumbering{bychapter}\fi

%
% Hack at \mainmatter so that it restores bychapter numbering if
% there's been a \frontmatter command and it was there in the first place
\let\@@mainmatter\mainmatter
\def\mainmatter{\@@mainmatter
  \if@chappg@auto\pagenumbering{bychapter}\fi
}

%
% Hack at \backmatter so that it warns the user of possibly surprising
% results if \pagenumbering[whatever]{bychapter} hasn't been used
\let\@@backmatter\backmatter
\def\backmatter{\@@backmatter
  \let\@chappg@prefix\@chappg@surprise
}
\def\@chappg@surprise{\textbf{??}}
\newif\if@chappg@surprised  \@chappg@surprisedfalse

\AtEndDocument{\if@chappg@surprised
  \PackageWarningNoLine{chappg}{Possibly surprising page numbering in
    \string\backmatter
    \MessageBreak
    Use \string\pagenumbering[prefix]{bychapter}%
    \MessageBreak
    to select alternative before line \@chappg@surprise@line
  }
  \fi
}

%%
%% command to hack at an environment to cause it to behave prettily
%% #1->environment name, #2->prefix for page numbers
%\newcommand\bychapterenv[2]{%
%  \@ifundefined{@@#1}{%
%    \expandafter\let\csname @@#1\expandafter\endcsname
%      \csname#1\endcsname
%  }{}%
%  \expandafter\edef\csname#1\endcsname{%
%    \expandafter\noexpand\csname @@#1\endcsname
%    \global\let\noexpand\@@bychapter\noexpand\@bychapter
%    \noexpand\pagenumbering[\noexpand#2]{bychapter}%
%  }%
%  \@ifundefined{@@end#1}{%
%    \expandafter\let\csname @@end#1\expandafter\endcsname
%      \csname end#1\endcsname
%  }{}%
%  \expandafter\edef\csname end#1\endcsname{%
%    \expandafter\noexpand\csname @@end#1\endcsname
%    \global\let\noexpand\@bychapter\noexpand\@@bychapter
%  }%
%}
%
%%
%% Specific cases: set up for dealing with bibliography, index, and (if
%% it's defined) glossary
%\bychapterenv{thebibliography}\bibname
%\bychapterenv{theindex}\indexname
%\@ifundefined{theglossary}{}{%
%  \bychapterenv{theglossary}{Glossary}%
%}

%
% The next magic makes the page counter be reset to one rather than zero
\renewcommand\@stpelt[1]{%
  \global\csname c@#1\endcsname
    \expandafter\ifx \csname c@#1\endcsname \c@page
      \@ne
    \else
      \z@
    \fi
}

%% fragment included from
%% CTAN macros/latex/contrib/fragments/removefr.tex
%%
%Date: Sat, 30 Jul 1994 17:58:55 PST
%From: Donald Arseneau <asnd@erich.triumf.ca>
%
%  \@removefromreset{FOO}{BAR} : Removes counter FOO from the list of
%                       counters \cl@BAR to be reset when counter BAR
%                       is stepped.  The opposite of \@addtoreset.

\def\@removefromreset#1#2{\let\@tempb\@elt
   \expandafter\let\expandafter\@tempa\csname c@#1\endcsname
   \def\@elt##1{\expandafter\ifx\csname c@##1\endcsname\@tempa\else
         \noexpand\@elt{##1}\fi}%
   \expandafter\edef\csname cl@#2\endcsname{\csname cl@#2\endcsname}%
   \let\@elt\@tempb}

\endinput

chappg.sty: number pages by chapter

Basic operation of the package is to redefine \thepage to be
\thechapter-\arabic{page}, and to cause the page number to be reset
(to 1) at the start of each chapter.  So the pages of chapter 3 will
be numbered 3-1, 3-2, ..., and the pages of appendix B will be
numbered B-1, B-2, ...

The package assumes the environment of the standard report or book
classes (or classes derived from them by inclusion or copying).

The package's effect is achieved by defining a new \pagenumbering
style `bychapter', and using it.  The package changes \pagenumbering
to take an optional argument, which is the `prefix' to the page
number.  Ordinarily, this will be the chapter number, but a user may
care to say (for example):

  \chapter*{Preface}
  \pagenumbering[Preface]{bychapter}

in her document somewhere, and the pages of the preface will be
numbered Preface-1, Preface-2, ...

The separator between the chapter and page numbers (hyphen above, but
I can imagine people wanting it to be an en-dash) is \chappgsep, which
en-dash-lovers may redefine (after loading the package); for example:

  \renewcommand{\chappgsep}{--}%

Havoc is wreaked with this structure in the LaTeX \frontmatter and
\mainmatter commands (which both explicitly use \pagenumbering and
therefore destroy the changed definition of \thepage

My design decision was to assume that \frontmatter is indeed to be in
\pagenumbering{roman}, and to restore the chapter-numbering in
\mainmatter.  Chapters in the frontmatter can not be numbered `by'
their chapters, since the chapters don't actually have numbers.  If
the author wishes to have some special treatment of part of the
frontmatter, the analogue of the above is

  \chapter{Preface}
  \pagenumbering[Preface]{bychapter}

The conception breaks down again in \backmatter; again, the \chapter
commands don't number the chapters, so that the pages will all be
numbered as if they were `within' the last real chapter.  Therefore,
if by-chapter page numbering is still enabled while in the backmatter,
and the user hasn't issued a new \pagenumbering[prefix]{bychapter}
since the \backmatter command, the package rewrites the prefix as
{\bf??}, and produces a warning at the end of the document.

