I've heard that LaTeX is Turing complete. Are there any programs written in LaTeX?

LatexTuring Complete

Latex Problem Overview


It's possible to do interesting things with what would ordinarily be thought of as typesetting languages. For example, you can construct the Mandelbrot set using postscript.

It is suggested in this MathOverflow question that LaTeX may be Turing-complete. This implies the ability to write arbitrary programs (although it may not be easy!). Does anyone know of any concrete example of such a program in LaTeX, which does something highly unusual with the language?

Latex Solutions


Solution 1 - Latex

In issue 13 of The Monad Reader, Stephen Hicks writes about implementing the solution to an ICFP contest (involving Mars rover navigation) in TeX, with copious use of macros. Amusingly, the solution's output when typeset is a postscript map of the rover's path.

Solution 2 - Latex

Alternatively, Andrew Greene wrote a BASIC interpreter in TeX (more details). This may count as slightly perverse.

Solution 3 - Latex

\def\K#1#2{#2}

\def\S#1#2#3{#1#3{#2#3}}

Solution 4 - Latex

The pgfmath library still amazes me. But on a more Turing-related note: it is possible to write an actual Turing machine in TeX, as per http://en.literateprograms.org/Turing_machine_simulator_(LaTeX). It's just a nifty way of using expansions in TeX.

PostScript is Turing complete as well, if you'll read the manual you'll be amazed by the general programming capabilities of it (at least, I was).

Solution 5 - Latex

I'm not sure if this qualifies as programming per se, but I've recently starting doing something a bit like Object Oriented stuff in LaTeX. (You don't need to know any maths to follow the following.) In recent papers, I've been writing about categories, which have objects and morphisms. Since there've been quite a few of those, I wanted a consistent style so that, say, 𝒞 was a category with typical object C and typical morphism c. Then I'd also have 𝒟 with D and d. So I define a "class", say "category" (you need to be a mathematician to understand the joke there), and declare that C is an instance of this class, and then have access to \ccat, \cobj, \cmor and so forth. The reason for not doing \cat{c}, \obj{c}, and \mor{c}, and so forth, is that sometimes these categories have special names and so after declaring the instance, I can modify it's name very easily (simply redefine \ccat - well, actually \mathccat since \ccat is a wrapper which selects \mathccat in math mode and \textccat in text mode). (Of course, it's a little more complicated than the above suggests and the OO stuff really comes in useful when I want to define a new category as a variant of an old one (it can even deal with the case where the old one doesn't exist yet.).)

Although it may not qualify as actual programming, I am using it in papers and do find it useful - the other answers (so far) have more of the feel of showing off the capabilities of LaTeX than of a sensible solution to a practical problem.

Solution 6 - Latex

I know of someone who wrote the answer to an ACM contest problem in LaTeX.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionire_and_cursesView Question on Stackoverflow
Solution 1 - LatexDerrick TurkView Answer on Stackoverflow
Solution 2 - LatexNorman GrayView Answer on Stackoverflow
Solution 3 - LatexMateusz GrotekView Answer on Stackoverflow
Solution 4 - LatexPieterView Answer on Stackoverflow
Solution 5 - LatexAndrew StaceyView Answer on Stackoverflow
Solution 6 - LatexAmberView Answer on Stackoverflow