Hendrik, hello.
(a tangent...)
On 27 Aug 2019, at 14:16, Hendrik Boom wrote:
> That said, it doesn't even have a stable syntax. I tried to find a
> grammar for parsing LaTeX, and discovered there is none. It seems
> LaTeX's macros do the parsing, and they're a Turing-complete
> laanguage.
Oh yes. LaTeX is fun.
TeX has a _small_ set of very primitive commands -- loosely like
add-character-to-list or add-box-to-list-of-boxes, none of which a
normal user would type or be aware of -- and all of the rest is macro
expansion. LaTeX is just a package of macros, originally written by
Leslie Lamport, sitting on top of (the package of macros that is) plain
TeX.
The tokeniser turns "\emph {x$y$}" into
<command-sequence "emph">
at which point "emph" is looked up to see if it's a macro. "emph" is,
and requires an argument, so the tokeniser then gobbles
<begin-group> <alphabetic-letter x> <math-shift> <alphabetic-letter
y> <math-shift> <end-group>
this is replaced by the expansion of "emph"+arguments, which is then
expanded, and so on until something expands to one or more primitives.
Thus what syntax there is is entirely specified by the definitions of
the macros.
Even the tokeniser is reconfigurable on the fly, so that the following
is a valid LaTeX file:
\documentclass{article}
\catcode`@=0
@catcode`<=1
@catcode`>=2
@catcode`;=14
@catcode`\%=11 ; % is now just an ordinary alphabetic-letter
@begin<document>
Hello, this is a 100% valid @LaTeX@ file.
@end<document} ; note the unbalanced braces -- } is still an
end-group
There is a certain rather perverse pleasure to be had writing and
maintaining LaTeX packages. But giggles and LOLs it is not.
Earlier, you said:
> Although mathematical expressions
> (which Latex is all about) have tree structure (like MathML does, and
> is excessively wordy), for the most part latex represents mathematics
> as just a symbol cluster.
TeX doesn't represent any of the structure of the mathematics, because
it's focused purely on the typesetting of maths (and other text).
Semantics, as far as TeX is concerned, is for human readers. Thus in
$\sum_i x_i$, the fact that \sum is a summation is not important; what's
important is that it's in a category of objects for which certain
spacing and layout rules apply, and these are
managed/implemented/realised by the internals of TeX -- the primitives
-- not by any macros.
Best wishes,
Norman
--
Norman Gray :
https://nxg.me.uk
SUPA School of Physics and Astronomy, University of Glasgow, UK