12

After many years of being merely a regular TeX (LaTeX lately) I started reading little bit more recently. I wrote a small program (very naive) which is suppose to illustrate doing integer arithmetic in TeX.

\message{Please enter the first number:}
\read-1 to \first

The first entered number is \first

\count0=\first

\message{Please enter the second number:}
\read-1 to \second

\count1=\second

The second entered number is \second

\advance\count0 by \count1


Could you please compare the sum of number you have entered and
the number of the page :)
\bye

Is there a simple way to recover the content of register \count0 on an arbitrary place on the page?

Werner
  • 603,163

2 Answers2

13

In order to obtain \count0, use \the\count0. This will typeset the value of \count0 wherever it is used.

enter image description here

\message{Please enter the first number:}
\read-1 to \first

The first entered number is \first

\count0=\first

\message{Please enter the second number:}
\read-1 to \second

\count1=\second

The second entered number is \second

\advance\count0 by \count1


Could you please compare the sum of number you have entered and
the number of the page :) \the\count0
\bye
Werner
  • 603,163
11

To complete the answer of Werner :

You can also use \number. If \count213=1789 then \the\count213and \number\count213 are equivalent, but if you can write \number2012 it's not possible to write \the2012 or \the{2012} .

If you want to learn something about integers with TeX, you can look at the sources of the TeXBook, for example Knuth writes this code :

\newif\ifprime \newif\ifunknown
\newcount\n \newcount\p \newcount\d \newcount\a
\def\primes#1{2,~3% assume that #1 is at least 3
  \n=#1 \advance\n by-2 % n more to go
  \p=5 % odd primes starting with p
  \loop\ifnum\n>0 \printifprime\advance\p by2 \repeat}
\def\printp{, % we will invoke \printp if p is prime
  \ifnum\n=1 and~\fi % this precedes the last value
  \number\p \advance\n by -1 }
\def\printifprime{\testprimality \ifprime\printp\fi}
\def\testprimality{{\d=3 \global\primetrue
  \loop\trialdivision \ifunknown\advance\d by2 \repeat}}
\def\trialdivision{\a=\p \divide\a by\d
  \ifnum\a>\d \unknowntrue\else\unknownfalse\fi
  \multiply\a by\d
  \ifnum\a=\p \global\primefalse\unknownfalse\fi}

In this code Knuth uses \number\p.

Alain Matthes
  • 95,075
  • I have read the book first time twenty years ago but this is the first time I am reading the book (I have it next to me) with the mind set that TeX is general programming language. It is overwhelming with the breath of information. He has whole chapter with many exercises on arithmetics. We do not use TeX as a general purpose programming language but I am trying to discover those dark corners of TeX which I never use as a working Mathematician. – Predrag Punosevac Dec 27 '11 at 13:49