多年来,我一直只是使用普通的 TeX(最近使用 LaTeX),最近我开始阅读一些内容。我编写了一个小程序(非常简单),旨在说明如何在 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
\count0
有没有一种简单的方法可以恢复页面上任意位置的寄存器内容?
答案1
为了获得\count0
,请使用。这将在使用它的任何地方\the\count0
排版 的值。\count0
\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
答案2
完成沃纳的回答:
您也可以使用\number
。如果\count213=1789
则\the\count213
和\number\count213
是等价的,但如果您可以写\number2012
就不可能写\the2012
或\the{2012}
。
如果你想了解一些关于 整数的知识TeX
,你可以查看 的源代码TeXBook
,例如 Knuth 编写了以下代码:
\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}
在此代码中,Knuth 使用\number\p
。