6

Let's say I have this simple loop:

\newcount\u\u1
\loop
    \the\u~
    \ifnum\u<2
        \advance\u1
\repeat

This prints the numbers as

1 2

What I want to achieve is

1
2

To do so I tried adding \\ anywhere in the loop, but that gives the error "! Forbidden control sequence found while scanning use of \\". Inserting \par gives "! Paragraph ended before \loop was complete".
So the question: How to insert linebreaks/linefeeds/enters/newlines in a loop in Plain Tex?

3
  • 1
    Use \endgraf instead of \par. Jun 15, 2018 at 12:13
  • Well that was... quick. Quicker than I expected. It worked, thank you! Jun 15, 2018 at 12:15
  • 1
    \endgraf is end paragarph rather than linebreak, linebreak is \break in plain tex (set on zero \parskip to see a difference) Jun 15, 2018 at 12:19

2 Answers 2

11

In plain.tex, \loop...\repeat is defined as:

\def\loop#1\repeat{\def\body{#1}\iterate}

so you cannot use \par inside it.

Also, in plain.tex, the \\ control sequence does not have the same "break line here" meaning as in LaTeX, so it doesn't work either.

You have two options:

First, redefine \loop...\repeat as a long macro:

\long\def\loop#1\repeat{\def\body{#1}\iterate}

so you can put \par in its argument.

Second, use \endgraf, which is exactly the same as TeX's primitive \par, except that when scanning the argument to a macro TeX looks for \par, but not \endgraf, so it can be used in these cases.


Have in mind that using \endgraf does a paragraph break, not a line break, as David said in the comments. Using \endgraf will insert a \parskip glue between each number. For an actual line break, see David's answer :)

1
  • 1
    For anyone wondering... the reason why \par isn't allowed in macro parameters is because it allows containing errors caused by a missing closing brace in a macro definition to the paragraph where they happen. See pg. 205 of the TexBook. Also Exercise 20.9 explores further how you can/can't "hide" a \par in a parameter anyway. Feb 2, 2021 at 0:10
4
\newcount\u\u1

\parindent0pt


\loop
    \the\u
    \ifnum\u<2
        \hfill\break
        \advance\u1
\repeat


\bye

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .