10

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

0

4 Answers 4

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

That would be

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

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

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

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)

2
  • 1
    The question was about plain TeX, not LaTeX. Commented Jan 26, 2019 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
    Commented Jan 26, 2019 at 23:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .