ChatGPT解决这个技术问题 Extra ChatGPT

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

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?

You know, I WAS going to have a weekend playing outside. You know, one of those away from a computer weekends? Then you go and post this question. This is why I can't have nice things.
/d{def}def/u{dup}d[0 -185 u 0 300 u]concat/q 5e-3 d/m{mul}d/z{A u m B u m}d/r{rlineto}d/X -2 q 1{d/Y -2 q 2{d/A 0 d/B 0 d 64 -1 1{/f exch d/B A/A z sub X add d B 2 m m Y add d z add 4 gt{exit}if/f 64 d}for f 64 div setgray X Y moveto 0 q neg u 0 0 q u 0 r r r r fill/Y}for/X}for showpage

M
Matt Ball

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.


N
Nulano

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


Interpreter link is dead
Use Archive.org to see interpreter: web.archive.org/web/20120707000649/http://…
M
Mateusz Grotek

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

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


finally somebody that makes sense
What is this supposed to do?
@URL It shows that the Turing complete S and K combinators can be expressed in LaTeX.
N
Nulano

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).


People have written LISP in PostScript. (Read the previous sentence until you have fully grasped the strange beauty of such a construction).
The PLRM manual mentioned is now a dead link, try this instead adobe.com/products/postscript/pdfs/PLRM.pdf
A
Andrew Stacey

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.


Would love to see how you're doing this!
A
Amber

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


Hm... unfortunately, I don't have the code lying around, and I've fallen out of touch with the person who wrote it. :/