Hacker News new | ask | show | jobs
by svat 1149 days ago
Nice post. Blog posts on TeX's internals are rare; see also Graham Douglas's posts on his blog (http://www.readytext.co.uk/?cat=14) and on Overleaf: https://www.overleaf.com/learn/latex/Articles/A_New_Series_o...

Once I had the thought of writing a "TeX debugger", that would separately show the function of TeX's "eyes", "mouth", "stomach" (actual terms used by Knuth) etc. It would have a bunch of different pages, one for the characters of the input file(s) (the input stack), one for the token (stream) that will be seen next, one for the "commands", one for the lists (horizontal lists, vlists, mlists) being assembled currently, one for the paragraph-breaking, one for the page-breaking, etc.

So if you were curious what was going on or encountered an error, you would be able to peer into exactly what part your error is coming from. Then I realized that with a typical macro-heavy usage of TeX (like LaTeX or even plain), almost all the action of interest to typical users would be in the expansion section.

I had hacked up an initial prototype (posted here: https://tex.stackexchange.com/a/391131) that shows what goes on with the following TeX input (without spaces):

    \expandafter \expandafter \expandafter \meaning \expandafter \uppercase \expandafter{a}
    \end
It may be interesting to exhume it and see what it does with the `\newif` from this post. The way I was doing it at the time is pretty slow (running TeX under gdb and exporting prodigious amounts of output), and the output format is pretty unhelpful/confusing, but these days I think it may make sense to compile TeX to WASM and have everything running in the browser. I haven't touched it in 6 years, but if anyone is interested in working on such a TeX debugger I can share ideas. :-)

My motivations were twofold:

- Most TeX users don't really understand what's going on, and this mismatch in mental models leads to much confusion, overly complex macros, etc.

- There are a lot of great ideas in TeX, which newer typesetting systems may ignore and not use. One problem is that TeX is too monolithic and I'm hoping that if people could see its parts separately, some of these ideas may be easier to grasp.