7

I'm sure this is very easy, but searching the internet I came up with nothing, and reading the TeXbook, I learned so much other stuff that I forgot what I was originally looking for ;-)

So, I just want to set the value of a counter to the difference of the values of two other counters in plain TeX, like this:

\newcount\countOne%
\newcount\countTwo%
\newcount\diff%
\diff=\countOne-\countTwo\relax%

Only, this doesn't work, ending up with "missing number, treated as zero"...

9
\diff=\countOne
\advance\diff-\countTwo
  • That was extremely fast!! I knew it was easy... Thanks! Sorry, can't upvote due to low reputation. – Duke Dec 19 '16 at 11:59
6

That would be

\diff=\countOne
\advance\diff-\countTwo
  • I'll accept David Carlisle's answer as it was a little faster, but thanks anyway! – Duke Dec 19 '16 at 12:00
4

For the case that e-TeX is available (\numexpr):

\newcount\countOne
\newcount\countTwo
\newcount\diff
\diff=\numexpr\countOne-\countTwo\relax
0

Since the examples given here are no MWE, I wasn't really able to test them. So, here is another code (but a MWE) that works as well:

\documentclass{article}

\begin{document}

\newcounter{FirstCounter}
\setcounter{FirstCounter}{42}
\newcounter{SecondCounter}
\setcounter{SecondCounter}{23}

\newcounter{FirstMinusSecondCounter}
\setcounter{FirstMinusSecondCounter}{%
  \numexpr\value{FirstCounter}-\value{SecondCounter}%
}

\noindent %just for a nicer output...
1st Counter: \theFirstCounter\\
2nd Counter: \theSecondCounter\\
1st minus 2nd: \theFirstMinusSecondCounter

\end{document}

(based upon an answer to this related question about adding numbers)

  • 1
    The question was about plain TeX, not LaTeX. – ShreevatsaR Jan 26 '19 at 23:05
  • O, yeah, now that you mention it... I guess I didn't realize this since I was focusing too much on solving my problem (with LaTeX)... – Prof.Chaos Jan 26 '19 at 23:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.