# How to set a counter to double the value of another counter?

The title says it. Lets have to counters:

\newcounter{myCount}
\newcounter{anotherCount}


I set myCount to some value for example 10

\setcounter{myCount}{10}


How do I set counter anotherCount to double the value of myCount? I have tried things like

\setcounter{anotherCount}{2\value{myCount}}


and

\setcounter{anotherCount}{2\themyCount}


but non of this works.

-
\multiply\myCount by 2 — this is not a length! –  Eddy_Em Jan 15 at 8:09
\setcounter{myCount}{10} \setcounter{anotherCount}{\value{myCount}} \addtocounter{anotherCount}{\value{myCount}} \arabic{anotherCount} works, but there must be better (I never use LaTeX counters and just read the first five lines of the documentation). –  jfbu Jan 15 at 8:16
I have read five more lines and \theanotherCount can be used rather than \arabic{anotherCount} to display the result. –  jfbu Jan 15 at 8:20
add comment

## 3 Answers

The 'official' LaTeX way (using only things documented in LaTeX: A Document Preparation System) is to use \addtocounter, as mentioned in a comment

\documentclass{article}
\newcounter{A}
\newcounter{B}
\begin{document}
\setcounter{A}{3}
\setcounter{B}{\value{A}}
\addtocounter{B}{\value{A}}

\arabic{A} and \arabic{B}
\end{document}


Loading the calc package (part of the core LaTeX2e system) allows

\documentclass{article}
\usepackage{calc}

\newcounter{A}
\newcounter{B}
\begin{document}
\setcounter{A}{3}
\setcounter{B}{2*\value{A}}

\arabic{A} and \arabic{B}
\end{document}


Of course, you can also use lower-level TeX or e-TeX constructs, as covered in the other answers.

-
the "official LaTeX" way was defined before eTeX was developed with its \numexpr and \dimexpr –  Herbert Jan 15 at 8:38
@Herbert Of course. The \numexpr primitive does not have LaTeX syntax, so really doesn't fit 'properly'. One could of course redefine \setcounter to use e-TeX, in a similar way to the way that calc alters the definitions to allow expressions in a very different way. (Or you could use Heiko's intcalc package for the same thing without requiring e-TeX.) –  Joseph Wright Jan 15 at 8:41
For me calc syntax is simplest and looks consistent with LaTeX counters. –  drasto Jan 15 at 10:04
add comment
\documentclass{minimal}
\begin{document}
\newcounter{A}    \newcounter{B}
\setcounter{A}{3} \setcounter{B}{\numexpr\theA*2\relax}

\theA\ and \theB

\end{document}

-
add comment

Don't forget that counter isn't same as length. Use commands \advance, \divide and \multiply to work with counters.

For example, this code will compute value of typographics sheets (run it in the end of document):

\newcount{\podp}
\newcount{\podpfr}
\podp=\c@page
\podpfr=\c@page
\divide\podp by 16
\multiply\podp by 100
\multiply\podpfr by 100
\divide\podpfr by 16
\advance\podpfr by -\podp
\divide\podp by 100
…
\hbox to 0pt{Формат $60\!\times\!84\;1/16$}\hfil Усл.печ.л.~%
{\the\podp.\ifnum\podpfr<10 0\fi\the\podpfr}\hfil

-
The OP asked about LaTeX counters and you use TeX things. Chances are if the OP knew about TeX \newcount the question would not have been asked in the first place. –  jfbu Jan 15 at 8:25
LaTeX is just a set of macros on TeX, so I see nothing awful to use tex in latex. –  Eddy_Em Jan 15 at 8:44
sure, I agree. It is is just in the context of this question. But after all, if your answer is an incitation to people who read it to not be kept prisoniers of the LaTeX confines, the better. However LaTeX is not a superset of Plain (some things in Plain are not taken over in LaTeX). And LaTeX counters are more than a TeX count register. So theoretically one would have also to redefine the LaTeX macros \c@..., \the... after the computation, to stay in the context of the question. –  jfbu Jan 15 at 8:57
In simple things like this there's better to use tex commands whether to use one more latex package that do such simple work. If such thins meets frequently in document, there would be better to choose latex-way. –  Eddy_Em Jan 15 at 9:00
add comment