I heard that TeX is a Turing complete language. Does it mean that one can use TeX for example to write operating systems or games like Tetris or Hangman? I don't mean that one writes a source code of Tetris but I mean, can one play Tetris somehow by writing a document and compiling it?
-
1i don't see how one can play tetris without writing a program. From my understanding, latex is a set of macros and not a programming language per se. However, there are packages (pytex, pertex) that can call external programs written in e.g. python. Also, lualatex embeds lua code into a document. Thus, it might be possible by writting tetris in lua– YorgosCommented Jul 3, 2020 at 9:39
-
3Like reversi, for example?– Ian ThompsonCommented Jul 3, 2020 at 10:02
-
1See some of the answers here: What is the most bizarre thing you have seen done with TeX– Alan MunnCommented Jul 3, 2020 at 11:24
-
While theoretically possible, the programs would be incredibly slow, unless all they did was put boxes inside other boxes.– John KormyloCommented Jul 3, 2020 at 14:29
-
2Could you implement on a traditional 8-bit TeX-engine a unicode-TeX-engine like LuaTeX or XeTeX? ;-) - This might be an interesting way of updating things on old platforms. ;-)– Ulrich DiezCommented Jul 3, 2020 at 19:01
2 Answers
“Turing-complete” means that, in theory, a TeX engine with enough memory could run a simulation of any of those things. It wouldn’t be practical.
The game of Tetris would not actually be playable, because TeX is not designed to read a controller and update pictures in real time, but it might come closest. However, you might write code (let’s say, in TikZ) that produce an animation of a Tetris game based on a given input file. It’s also possible to imagine making a simulation of the memory of some 8-bit computer, like an Apple II, including the CPU and the memory, as a state machine. You could then run a simulated operating system in the document and draw what would appear on the screen when the simulated program finished.
LuaTeX provides hooks to insert arbitrary Lua modules, but that’s somewhat different from doing it in TeX.
-
2Another way to handle “input”/“output” is to use the error message/response loop that occurs during a TeX run. I.e. you run
pdflatex
on the input file from the command line;pdflatex
issues an “error message” describing the game state; your response describes the next move. Something like tic-tac-toe should be feasible this way in practice. Commented Jul 3, 2020 at 10:08 -
1@PeterLeFanuLumsdaine Some of the early proofs-of-concept of C++ template metaprogramming printed the results of the program in an error message!– DavislorCommented Jul 3, 2020 at 10:10
I used LaTeX to create and solve sudoku puzzles (> texdoc sudokubundle
) although I don't recommend using (La)TeX for this.