# Slave duplicate counter

Is it possible to create a counter that is a "slave" to another (master) counter, but also an exact duplicate?

The \@addtoreset{<slave>}{<master>} resets <slave> whenever <master> is incremented (similar functionality is offered by chngcntr), but I don't know about anything incrementing one counter (<slave>) automatically whenever another counter (<master>) is incremented.

The idea is to have a macro (say) \dupcntr{<slave>}{<master>} as an analogue to \@addtoreset. The application for this might be where one is interested in renaming a counter used/provided by a package into something more meaningful for the user (say, <slave>), without having to reference <master>. Or, if the original counter contains @ that needs to be escaped every time it's used, it is sometimes more convenient to have a "more user friendly" counter.

I know one way around is to use a macro that returns the value of <master>, but is it possible to perform the same manipulation with a macro than one could with the counter? Manipulation here refers to the printing of the counter (via \the<slave>) and perhaps it's representation (like \arabic, \alph, ...).

-

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.

-
You have to copy also \themaster into \theslave; of course, redefining \themaster wouldn't redefine \theslave, nor conversely. –  egreg Nov 7 '11 at 0:23
@egreg: True. I would say \newcommand\theslave{\themaster} should do it. –  Martin Scharrer Nov 7 '11 at 8:04
Thanks Martin, this was a straight-forward solution. –  Werner Dec 13 '11 at 17:08

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.
}
\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:

\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.

-

ConTeXt presents a key-value interface to couple two counters. For example:

\definecounter
[first]
[numberconversion=Characters]

\definecounter
[second]
[number=first, numberconversion=numbers]

\starttext

\incrementcounter[first]
\incrementcounter[first]

\convertedcounter[first] % Prints "B"
\convertedcounter[second]% Prints "2"

\stoptext


If you want the second counter to inherit all the settings of the first counter, (conversion, color, style, etc), just use

\definecounter
[second]
[first]

-