views:

492

answers:

7

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?

+5  A: 

http://en.literateprograms.org/Turing_machine_simulator_(LaTeX) may be of interest. :)

jer
+3  A: 

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_%28LaTeX%29. 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).

Pieter
+3  A: 

I guess that what most answers your question is a Turing machine implemented in LaTeX. Or you can do your Mandelbrot in LaTeX.

EDIT: this is a much nicer and more graphically pleasing Mandelbrot with LaTeX.

You say "arbitrary programs", but I'm sure you know that Turing Complete does not necessarily mean that you can build cars with it. I.e., LaTeX is very instruction-rich. Compare it with this One Instruction Set Computer who also happens to be Turing Complete, but don't try to create a face-recognition program with it.

Abel
Arbitrary programs means that any program can be written. It may not be practical, and of course it doesn't mean arbitrary I/O, but a Turing-complete language can calculate anything.
David Thornley
Indeed, exactly! ;)
Abel
Thanks - the Mandelbrot is interesting.
ire_and_curses
@David: Calculate anything that is *computable*, of course :).
KennyTM
A: 

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

Amber
Sounds good - is the implementation described anywhere?
ire_and_curses
Hm... unfortunately, I don't have the code lying around, and I've fallen out of touch with the person who wrote it. :/
Amber
+12  A: 

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.

Derrick Turk
+1 - That is fantastic.
ire_and_curses
Yes. Amazing...
Geoff
+1  A: 

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.

Andrew Stacey
+2  A: 

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

Norman Gray