3

My need is quite simple. For example, I have a counter cnt1, now I want to create a new counter cnt2 which actually is the same one as cbt1 except for their names. That is cnt2 is a copy of cnt1 or just a rename of cnt1.

I have tried the following:

\newcounter{cnt2}
\setcounter{cnt2}{\value{cnt1}}

But it didn’t work as what I expect. I mean it is not the completely copy of cnt1. How can I make a copy of counter in LaTeX?

1 Answers1

7

There is a package for that, called aliascnt. See here for a very related discussion.

\documentclass{article}
\usepackage{aliascnt}
\newcounter{cnt1}
\begin{document}
\setcounter{cnt1}{7}%
\newaliascnt{cnt2}{cnt1}%
$\number\value{cnt1}=\number\value{cnt2}$

\addtocounter{cnt1}{5}%
$\number\value{cnt1}=\number\value{cnt2}$
\end{document}

enter image description here

  • Thanks! Nice work! – user450201 May 31 '20 at 03:14
  • 1
    +1. Just out of curiosity: Is there an advantage in writing \number\value{cnt1} instead of \arabic{cnt1}? – Mico May 31 '20 at 04:09
  • 2
    @Mico It is just a habit, I could have used \arabic instead, so there is no advantage in this code. Here one can find a discussion on this. –  May 31 '20 at 04:17
  • 1
    You can do it without the package, but it is ugly! \expandafter\expandafter\expandafter\let\expandafter\csname c@cnt2\expandafter\endcsname\csname c@cnt1\endcsname – John Kormylo May 31 '20 at 16:20