2

I wanted to create list of n Fibonaci numbers. This is my code so far:

\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

I'd like to have all numbers on a separate lines but TeX doesn't allow \par in \loop ... \repeat construction. Do you have any idea how to achieve this?

Thanks for any directions.

  • 1
    Just use \endgraf: $\the\b$\endgraf. \par wouldn't work because \loop is not a \long macro, but \endgraf does the same, with other name. – Phelype Oleinik Oct 10 '18 at 14:19
2

\endgraf is a renamed \par for such use:

\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
  • or you could copy the definition of \loop from latex, which would allow a blank line or \par – David Carlisle Oct 10 '18 at 14:22
0

In your case you could also put the loop in a group with obeylines. This command turns lines of input into lines that are typeset as a line. It should be used inside a group to limit its scope.

\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

The group allows to implement the loop in a more efficient way if you need higher values for \n than 5. (The value of \n is restored at the end of the group.)

{\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}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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