我想创建 n 个斐波纳契数的列表。这是我目前的代码:
\newcount\n
\n=5
\newcount\a
\newcount\b
\newcount\i
\a = 1
\b = 1
\i = 0
$a = \the\a$ \par
$b = \the\b$ \par
$i = \the\i$ \par
\vskip 15mm
\hrule
\loop
$\the\b$
\advance\b by \a
\multiply\a by -1
\advance\a by \b
\advance\i by 1
\ifnum\i<\n
\repeat
\bye
我想将所有数字放在单独的行上,但 TeX 不允许\par
这样\loop ... \repeat
做。您知道如何实现吗?
谢谢您的任何指导。
答案1
\endgraf
已重命名\par
用于以下用途:
\newcount\n
\n=5
\newcount\a
\newcount\b
\newcount\i
\a = 1
\b = 1
\i = 0
$a = \the\a$ \par
$b = \the\b$ \par
$i = \the\i$ \par
\vskip 15mm
\hrule
\loop
$\the\b$
\endgraf
\advance\b by \a
\multiply\a by -1
\advance\a by \b
\advance\i by 1
\ifnum\i<\n
\repeat
\bye
答案2
在您的例子中,您还可以将循环放在带有 obedlines 的组中。此命令将输入行转换为排版为一行的行。它应该在组内使用以限制其范围。
\newcount\n
\n=5
\newcount\a
\newcount\b
\newcount\i
\a = 1
\b = 1
\i = 0
$a = \the\a$ \par
$b = \the\b$ \par
$i = \the\i$ \par
\vskip 15mm
\hrule
{\obeylines
\loop
$\the\b$
\advance\b by \a
\multiply\a by -1
\advance\a by \b
\advance\i by 1
\ifnum\i<\n
\repeat
\par}
\bye
如果您需要 \n 的值高于 5,则该组允许以更有效的方式实现循环。(\n 的值在组末尾恢复。)
{\obeylines
\i=\n
\loop
\n=\b
\advance\b by \a
\hbox to40pt{\hss\the\b}
\a=\n
\advance\i by -1
\ifnum\i>0
\repeat
\par}