我想避免使用\tmp
。是否可以在保持相同输出的情况下将其删除?
% Declare counters.
\newcount\pages% \pages is given so you cannot remove it!
\newcount\x
\newcount\tmp
% Initializing.
\pages=9% Try it for an odd or even integer.
\tmp=\pages
\advance\tmp by 1
\x=1
\loop
\ifnum\x<\tmp
\noindent\hfill\the\x
\advance\x by 1
\ifnum\x>\pages
\hfill\null\endgraf
\else
\hfill\the\x\hfill\null\endgraf
\fi
\advance\x by 1
\repeat
\bye
注意:请不要使用e-TeX
扩展,因为我现在正在学习 Knuth 的原始 TeX。
答案1
从 开始\x=0
,尽快执行并反转内部条件:
\newcount\pages % \pages is given so you cannot remove it!
\newcount\x
% Initializing.
\pages=9 % Try it for an odd or even integer.
\x=0
\loop
\ifnum\x<\pages
\advance\x by 1
\noindent\hfill\the\x
\ifnum\x<\pages
\advance\x by 1
\hfill\the\x
\fi
\hfill\null\endgraf
\repeat
\bye
答案2
在循环结束时简单地测试相等性有什么问题:
% Declare counters.
\newcount\pages% \pages is given so you cannot remove it!
\newcount\x
% Initializing.
\pages=9 % Try it for an odd or even integer.
\x=1 %
\loop
\ifnum\x=\pages
\noindent
\hfill
\number\x
\hfill
\null
\endgraf
\fi
\ifnum\x<\pages
\noindent
\hfill
\number\x
\advance\x by 1 %
\hfill
\number\x
\hfill
\null
\endgraf
\advance\x by 1 %
\repeat
\bye
(我添加了一些空格以正确终止数字。)
答案3
\tmp
您在回答中已经将其删除,这\x
也会将其删除。
% Declare counters.
\newcount\pages% \pages is given so you cannot remove it!
% Initializing.
\pages=9% Try it for an odd or even integer.
\def\z#1{%
\ifnum#1<\numexpr\pages+1\relax
\noindent\hfill#1%
\ifnum#1=\pages
\hfill\null\endgraf
\else
\hfill\number\numexpr#1+1\hfill\null\endgraf
\fi
\expandafter\z\expandafter{\number\numexpr#1+2\expandafter\relax\expandafter}%
\fi}
\z1
\bye
答案4
使用\numexpr\pages+1\relax
似乎有效:
代码:
% Declare counters.
\newcount\pages% \pages is given so you cannot remove it!
\newcount\x
%\newcount\tmp
% Initializing.
\pages=9% Try it for an odd or even integer.
%\tmp=\pages
%\advance\tmp by 1
\x=1
\loop
\ifnum\x<\numexpr\pages+1\relax
\noindent\hfill\the\x
\advance\x by 1
\ifnum\x>\pages
\hfill\null\endgraf
\else
\hfill\the\x\hfill\null\endgraf
\fi
\advance\x by 1
\repeat
\bye