I've heard that LaTeX (and for that matter TeX) are Turing-complete, so it should be possible to produce an infinite loop. And indeed, a quick search reveals several questions in which users have found infinite loops--but these were always for subtle reasons and rather indecipherable to someone just wanting to see a basic loop construction. (I suppose that "transparent" infinite loops are usually solved by the programmer without having to ask here.)

Hence, my question: How do you produce an infinite loop in a way that is enlightening to someone to whom the Turing-completeness of LaTeX feels like a myth?

Or, just to spice it up a bit: How do you tell LaTeX to print all infinitely many verses of "The Song That Never Ends"?

[Note: I conceived of a version of this question some time ago--long before I knew the answer--but never got around to posting it. I am interested in seeing approaches besides the one I eventually came up with myself.]

@cnieder: That sort of solution is precisely the reason I did not post the question back when I thought of it. I want an infinite loop that actually looks at least a little like a loop, and/or does something mildly interesting.
–
Charles StaatsJan 29 '13 at 1:24

"I've heard that LaTeX (and for that matter TeX) are Turing-complete". Had to point out that since LaTeX runs on top of TeX, TeX has to be Turing-complete if LaTeX is. I find it more surprising that LaTeX is Turing-complete. Is it even true, if we stick to the LaTeX commands and avoid dropping down to TeX, as we're supposed to?
–
alexisMar 23 '14 at 13:57

3 Answers
3

The following code will do what is asked, using the plain TeX construction \loop ... \if ... \repeat:

\documentclass{article}
\title{The Song That Never Ends}
\author{by Norman Martin}
\begin{document}
\maketitle
%
\noindent This is the song that never ends. \newline
\loop \iftrue
Yes, it goes on and on my friends. \newline
Some people started singing it not knowing what it was, \newline
and they'll continue singing it forever just because
\noindent this is the song that never ends. \newline
\repeat
%
\end{document}

I ran this code once, and aborted the process after it reported producing something like 47,000 pages (in a very short amount of time). Unfortunately, my computer complained that the resulting pdf file was "damaged" and could not be opened. A better (perhaps impossible?) answer would produce a file such that the output could actually be viewed once the process was killed (or died). The closest I've come is
the following, which prints only the first five hundred verses:

\documentclass{article}
\title{The Song That Never Ends}
\author{by Norman Martin}
\begin{document}
\maketitle
%
\noindent This is the song that never ends. \newline
\newcounter{repetitions}
\setcounter{repetitions}{0}
\loop \ifnum \value{repetitions}<500
Yes, it goes on and on my friends. \newline
Some people started singing it not knowing what it was, \newline
and they will go on singing it forever just because
\noindent this is the song that never ends. \newline
\addtocounter{repetitions}{1}
\repeat
%
\end{document}

Here is a variation of your first solution (this document can't be terminated!):

\documentclass{article}
\title{The Song That Never Ends}
\author{by Norman Martin}
\def\songthatneverend{
\noindent This is the song that never ends. \newline
Yes, it goes on and on my friends. \newline
Some people started singing it not knowing what it was, \newline
and they'll continue singing it forever just because
\expandafter\songthatneverend}
\begin{document}
\maketitle
\songthatneverend
\end{document}

And a version with a limited number of pages (25000 pages => 11MiB PDF):

\documentclass{article}
\usepackage{etoolbox}
\title{The Song That Never Ends}
\author{by Norman Martin}
\def\songthatneverend#1{
\noindent This is the song that never ends. \newline
Yes, it goes on and on my friends. \newline
Some people started singing it not knowing what it was, \newline
and they'll continue singing it forever just because
\ifnumless{\value{page}}{#1}{\def\cont{\expandafter\songthatneverend{#1}}}{\def\cont{}}\cont
}
\begin{document}
\maketitle
\songthatneverend{25000}
\end{document}

Here is a never ending counting of the primes... (except that it will end-up in some arithmetic overflow error after a while).

For less overhead, I use plain PDFTeX. Launching the run in a Terminal, the page numbers correspond to how many primes have been computed so far.

update: the algorithm was a bit faulty, although it computed correctly the primes. The tested integer n had to receive from time
to time a +2 kick that was missing, so that the amount of divisions
done slowly drifted above the necessary. Furthermore my imprudent use
of \1,\2, .. gave unwanted space tokens in the output (this was
very silly on my part, but I have learned my lesson).

Perhaps there is a more intelligent manner using TeX arithmetic to test if some number is a multiple of another one: as dividing and the multiplying seems quite waste, the first division could well have produced internally the remainder.
–
jfbuJan 30 '13 at 10:43