% chngcntr.sty
%
% Provides commands to change the resetting of counters.
% Instructions for use are at the end of this file.
%
% Author: Peter Wilson, Herries Press
% Maintainer: Will Robertson (will dot robertson at latex-project dot org)
% Released under the Latex Project Public License
%
%
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{chngcntr}[2009/09/02 v1.0a change counter resetting]
%
% This package uses David Carlisle's \@removefromreset command as
% specified in the remreset package available from CTAN as
% macros/latex/contrib/supported/carlisle/remreset.sty
% It is \provided here as a convenience to the user, and with
% David Carlisle's permission.
% START OF DAVID CARLISLE'S CODE
\providecommand{\@removefromreset}[2]{{%
  \expandafter\let\csname c@#1\endcsname\@removefromreset
  \def\@elt##1{%
    \expandafter\ifx\csname c@##1\endcsname\@removefromreset
    \else
      \noexpand\@elt{##1}%
    \fi}%
  \expandafter\xdef\csname cl@#2\endcsname{%
    \csname cl@#2\endcsname}}}
% END OF DAVID CARLISLE'S CODE

%%\@ifbothc@t@s{<package>}{<counter>}{<within>}{<code when both are counters>}
\newcommand{\@ifbothc@t@s}[4]{%
  \@ifundefined{c@#2}{% counter undefined
    \PackageError{#1}{#2 is not a counter}{\@eha}}%
  {% else counter is defined
    \@ifundefined{c@#3}{% within undefined
      \PackageError{#1}{#3 is not a counter}{\@eha}}%
    {% else both counter and within  are defined
     #4}}}

%%\counterwithout*{<counter>}{<within>}
\newcommand{\counterwithout}{\@ifstar{\c@t@soutstar}{\c@t@sout}}
\newcommand{\c@t@soutstar}[2]{%
  \@ifbothc@t@s{chngcntr}{#1}{#2}{\@removefromreset{#1}{#2}}}
\newcommand{\c@t@sout}[2]{%
  \@ifbothc@t@s{chngcntr}{#1}{#2}{\@removefromreset{#1}{#2}%
                        \@namedef{the#1}{\arabic{#1}}}}


%%\counterwithin*{<counter>}{<within>}
\newcommand{\counterwithin}{\@ifstar{\c@t@sinstar}{\c@t@sin}}
\newcommand{\c@t@sinstar}[2]{%
  \@ifbothc@t@s{chngcntr}{#1}{#2}{\@addtoreset{#1}{#2}}}
\newcommand{\c@t@sin}[2]{%
  \@ifbothc@t@s{chngcntr}{#1}{#2}{\@addtoreset{#1}{#2}%
                        \@namedef{the#1}{\@nameuse{the#2}.\arabic{#1}}}}


\endinput

%%%%%%%%%%%%%%%%%%
% USAGE
%%%%%%%%%%%%%%%%%%
%
%     In LaTeX, a new counter called, say `ctr', is created by the command
% \newcounter{ctr}[within]. If the optional within argument is given
% the the counter `ctr' is reset to zero each time the counter `within'
% changes. The command \thectr typesets the value of the counter ctr.
% This is automatically defined by \newcounter and is initialised
% to typeset arabic numerals.	
%
%%%%%%%%%%%%%%%%%
% \counterwithin
% \counterwithin*
%%%%%%%%%%%%%%%%%
%    It is sometimes desireable to change a counter that has been defined
% by \newcounter{ctr} to act as though it had been defined as 
% \newcounter{ctr}[within]. The package provides the command
% \counterwithin{ctr}{within} that accomplishes this. By default,
% it also redefines the \thectr command so that it typesets values
% in the style \thewithin.\arabic{ctr}. The starred version of the
% command suppresses the redefinition of \thectr 
% (e.g., \counterwithin*{ctr}{within}).
%
%%%%%%%%%%%%%%%%%%
% \counterwithout
% \counterwithout*
%%%%%%%%%%%%%%%%%%
%    Likewise, the command \counterwithout{ctr}{within} changes a
% counter that has been created by \newcounter{ctr}[within] to act
% as though it had been created by \newcounter{ctr}. By default it
% also redefines the \thectr command so that it just typesets an arabic
% numeral. The starred version of the command suppresses the redefinition
% of \thectr.
%
%    Any number of \counterwithin{ctr}{...} and \counterwithout{ctr}{...}
% commands can be issued for a given counter, ctr, if you wish to toggle
% between the two styles. The current value of ctr is unaffected by
% \counterwithin and \counterwithout. If you want to change the value
% after one of these commands, use \setcounter{ctr}{...}, and to change
% the typeseting style use \renewcommand{\thectr}{...}.
%
%
% Peter W.
% 2001/03/30
%
% Change History:
% v1.0a - New maintainer (Will Robertson)
%