Slave duplicate counter

If both should have always the same value, why not simply \let the slave to the master? This copies the internal reference to the \count register.

\newcommand*{\dupcntr}[2]{%
    \expandafter\let\csname c@#1\expandafter\endcsname\csname c@#2\endcsname
}

This is for LaTeX counters. For TeX count's simply remove the c@ (if given by cs name) or use \let#1#2 if given by control sequence.


Check out my "amsthm tweaks": posted here. I did just this, because the amsthm package is lazy about making subordinate theorem numbers, and this breaks the fncylab package's way of customizing \ref's output. The code is thus:

\makeatletter
\newcommand{\clonecounter}[2]{
  \@xp\xdef\csname c@#1\endcsname{\@xp\@nx\csname c@#2\endcsname}
  \@xp\xdef\csname the#1\endcsname{\@xp\@nx\csname the#2\endcsname}
  % This is for hyperref compatibility
  \@xp\xdef\csname theH#1\endcsname{\@xp\@nx\csname theH#2\endcsname}
  % Don't copy p@#2 or cl@#2!
  \global\@xp\let\csname p@#1\endcsname = \@empty
  \global\@xp\let\csname cl@#1\endcsname = \@empty
  % This is wrong, for some reason.  So it's commented out.
  %\@addtoreset{#1}{@ckpt}
 }
\makeatother

This makes a new counter #1 which, at its birth, is a complete slave to #2: same count register, same manner of printing. It sets up all the internal quantities that LaTeX considers to constitute a "counter", so you can \addtocounter and \refstepcounter and \@addtoreset correctly.


Heiko Oberdiek's aliascnt package provides this functionality. It provides \newaliascnt{<slave>}{<master>} and can be used in instances where theorems share base counters:

enter image description here

\documentclass{article}
\usepackage{aliascnt}% http://ctan.org/pkg/aliascnt
\newtheorem{foo}{Foo}% counter "foo"
\newaliascnt{bar}{foo}% alias counter "bar"
\setlength{\parindent}{0pt}% Just for this example
\begin{document}
\setcounter{foo}{10}%
\begin{foo}Something\end{foo}
\verb|foo:|\ \thefoo \par
\verb|bar:|\ \thebar
\end{document}

As expected, it integrates well with hyperref.

Tags:

Counters