Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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

enter image description here

% 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

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

share|improve this question
    
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. –  I am who I say I am Jul 7 '12 at 3:56
add comment

4 Answers

up vote 5 down vote accepted

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

\bye
share|improve this answer
    
The most compact solution! –  I am who I say I am Jul 7 '12 at 3:53
add comment

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

\bye

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

share|improve this answer
    
\pages=1 does not work. :-) –  I am who I say I am 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? –  I am who I say I am Jul 6 '12 at 19:01
1  
@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. –  I am who I say I am Jul 7 '12 at 9:39
add comment

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
share|improve this answer
    
Who can compile it? I cannot. –  I am who I say I am Jul 6 '12 at 18:37
1  
@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
add comment

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
%\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
share|improve this answer
    
No performance drawback? –  I am who I say I am Jul 6 '12 at 18:14
    
@HiggsBoson: I don't know. That would have to be measured. Just like any other software I would assume that if you are reducing your memory requirements, you are probably increasing the compute time. –  Peter Grill Jul 6 '12 at 18:48
    
Judging performance with these things can be complex: assignments have a time requirement but so do expressions and expansion, so you have to test the real use cases. Lots of assignments will normally be slower than a small number of expressions, but one assignment versus one expression is more difficult! –  Joseph Wright Jul 6 '12 at 18:50
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.