This is perhaps a soft question, and I realise it has a chance of being closed, but here we go...
I am currently getting to grips with the internals of Tex/Latex and have found the learning curve to be quite steep. I've been mulling over the mathematical nature of the macro system and wondering how it relates to the actual typesetting and layout of a document.
To premise my question, here's my brief analysis of why Tex/Latex is so... unforgiving. This is not intended to be a rant, please bear with me if it seems that way. I'll blockquote the bit which might seem ranty so you can skim it if this is old news to you.
Tex is what I would call an aggressively imperative language, but it is intended to be used in a mostly declarative fashion. You "markup" your document with sections, captions, etc, in a way which is superficially analogous to something like HTML. However, if you want to write your own macros to tweak anything, you have to deal with a lot of hairy implementation details.
The core logic for parsing tex seems to be "examine the next token, do the thing it says to do (which may arbitrarily change pretty much anything in the execution environment), then consume it and go onto the next token". This seems attractively simple, but the action of any token can have far-reaching effects which are not evident in the immediate scope in which it is used. This stands in stark contrast to the conventional wisdom for other imperative languages such as C++ where good design is things like "don't use global variables, variables should be
const
wherever possible, use RAII to establish invariants, and make functions idempotent where possible". Such practices make functions easier to understand by making them easily composable.Tex/Latex functions are not easily composable. Spooky-action-at-a-distance and unrestrained interference between macros seems to be idiomatic. It resembles a raw Turing machine more than almost every other language I have seen. It is certainly the most fragile language I have ever used.
It strikes me that this situation is heightened by the fact that, although it is normal to write and use macros that have highly non-local effects, paradoxically, the core language itself does not empower itself to easily inspect or control its environment beyond that which is immediately local. Hence we get tangled messes involving
\expandafter
, multiple ways of defining macros, nothing I would call a type system, and so on.
So that's what Tex looks like to me. So what -- shrug, right? It's just how the language is.
The thing is, nothing I've seen about the design of macro system so far seems to have anything to do with typesetting and layout.
I have no idea what the typesetting and layout actually is or how it works. Here is my question: does the typesetting and layout algorithm actually require the kind of code transformations afforded by the macro language, or could the same typesetting and layout process be easily implemented by something radically different?
I say "easily implemented" because, of course, Turing completeness means it is of course possible to get the same results in any other language. That's not the point. Some languages make it easy to work with matrices, for example, some languages make it easy to work with graphs, some with strings, some with predicates, etc etc. One way of looking at my question is: does the Tex macro system actually have an intimate connection with the mathematics and logic of typesetting and layout?