# Is there a trick to reduce the number of counters in the following case?

I want to avoid using \tmp. Is it possible to remove it while keeping the same output?

% 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
\x=1

\loop
\ifnum\x<\tmp
\noindent\hfill\the\x
\ifnum\x>\pages
\hfill\null\endgraf
\else
\hfill\the\x\hfill\null\endgraf
\fi
\repeat

\bye


Note: Please don't use e-TeX extension as I am learning Knuth's original TeX right now.

-
It might be handy to specify if a solution can use the e-TeX extensions, or has to stick to Knuth's TeX only. – Joseph Wright Jul 6 '12 at 18:32
@HiggsBoson: Not that it doesn't feel sort of like cheating, but still: tex.stackexchange.com/questions/60099/who-doesnt-use-e-tex – Ryan Reich Jul 6 '12 at 19:34
@RyanReich: OK. Thanks. I will learn the extension later after grasping Knuth's original TeX. – kiss my armpit Jul 7 '12 at 3:56

Start with \x=0, step it as soon as possible and invert the inner conditional:

\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
\noindent\hfill\the\x
\ifnum\x<\pages
\hfill\the\x
\fi
\hfill\null\endgraf
\repeat

\bye

-
The most compact solution! – kiss my armpit Jul 7 '12 at 3:53

What's wrong with simply testing for equality at the end of the loop:

% 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
\hfill
\number\x
\hfill
\null
\endgraf
\repeat

\bye


(I've added in a few spaces to correctly terminate numbers.)

-
\pages=1 does not work. :-) – kiss my armpit Jul 6 '12 at 18:24
@HiggsBoson I've updated the answer to cover the no-loop case. – Joseph Wright Jul 6 '12 at 18:30
Defining \printpage seems to be unnecessary. Why don't you put its body directly into the first \ifnum? – kiss my armpit Jul 6 '12 at 19:01
@HiggsBoson Oops: I was experimenting with a few approaches, and forgot that I could remove that auxiliary! – Joseph Wright Jul 6 '12 at 19:02
Sorry, I switched to egreg's solution because of the minimal number of used characters. – kiss my armpit Jul 7 '12 at 9:39

You got rid of \tmp in your answer, this removes \x as well.

% 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

-
Who can compile it? I cannot. – kiss my armpit Jul 6 '12 at 18:37
@HiggsBoson Works for me (needs the e-TeX extensions, of course) – Joseph Wright Jul 6 '12 at 18:48
@David: Please why did you use \repeat in place of \fi. If I insert \loop in your conditionals, I will end up with a mess. Also, braces aren't expandable. So you have more than necessary number of \expandafter. I withdrew from editing your answer, but my name is still hanging in there. – Ahmed Musa Jul 7 '12 at 1:16
\repeat is a mistake forgot to change it from the OP's version, of course it worked as it is defined to be \fi but would break a loop as you say. Will edit. The \expandafter is not expanding the brace but the token after it, it is needed to expand the \fi/\repeat token before iteration. – David Carlisle Jul 7 '12 at 10:24

Using \numexpr\pages+1\relax seems to work:

## Code:

% 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
\x=1

\loop
\ifnum\x<\numexpr\pages+1\relax
\noindent\hfill\the\x
\ifnum\x>\pages
\hfill\null\endgraf
\else
\hfill\the\x\hfill\null\endgraf
\fi