1
  • Am I right that MikTeX sources in C++ contain the code that opens .tex file (when I run latex.exe myfile.tex), reads it, parses commands, performs them if they are correct and logs error messages otherwise?
  • And the TeX/LaTeX syntax is 'mounted' into these sources as well?
  • For example, a LaTeX document must start with \documentclass command. Is this syntax requirement maintained somewhere in the С++ sources directly?
5
  • 2
    no, I believe miktex uses a similar web2c translation as texlive so the executable sources are web (pascal) auto converted to C but that is just tex-the-program. LaTeX itself so commands like \documentclass, \section etc are written in tex. Oct 3 at 12:31
  • 1
    use texdoc source2e to see the documented sources of latex Oct 3 at 12:32
  • 1
    the pascal to C for the miktex sources is documented here docs.miktex.org/hacking/index.html Oct 3 at 12:35
  • @David Carlisle So commands processing is performed in the original TeX in WEB language (that is then translated to C)? This program supports TeX primitives the combinations of which LaTeX (as a macros package) consists of? And if to draw analogies, TeX is like a pure C with its compiler and LaTeX is like a big library in this pure C for more comfortable use?
    – Vladimir
    Oct 3 at 13:11
  • more or less yes, although comparisons with other languages can be misleading Oct 3 at 13:19
4

MikTeX, like texlive includes an implementation tex-the-program largely automatically converted from the original WEB (Pascal) source to C, suplemented by additional C as needed. You can browse the sources at

https://miktex.org/sources

LaTeX is a program written in TeX and then processed with a suitable TeX executable so pdflatex, latex, lualatex, xelatex, etc are essentially the same latex source code processed with pdftex, etex, luahbtex, xetex respectively.

You can see the documented source of latex with texdoc source2e and the extracted run time tex file that is used to make the latex formats will be installed in your tex tree as <texmf-root>/tex/latex/base/latex.ltx

So for example the command you mention, \documentclass is defined in that file by

\def\documentclass{%
  \let\documentclass\@twoclasseserror
  \if@compatibility\else\let\usepackage\RequirePackage\fi
  \@fileswithoptions\@clsextension}

and the constraint that there must be exactly one is also implemented in TeX macros. As shown above, after the first use, \documentclass redefines itself to be \@twoclasseserror which will generate an error if used. Conversely, elsewhere in the file you will find

\everypar{\@nodocument}

Which will generate an error if a paragraph of text is started before the \begin{document} and \begin{document} checks that at least \normalfont is defined as a check that a document class has been specified.

4
  • Am I right that when I process my .tex file with latex.exe or pdflatex.exe then finally it is the very original TeX that executes the commands I used (LaTeX higher-level commands reduced to low-level TeX primitives)? And in terms of linear space, TeX is like a basis and LaTeX is like a space over this basis (LaTeX commands are like linear combinations of TeX primitives (and other LaTeX commands in turn))?
    – Vladimir
    Oct 5 at 16:21
  • @Vladimir yes latex is written in tex and tex is an interpreted macro expansion language so ultimately any latex defined command has a meaning given by a tex primitive, or is a character, but this expansion has to be interleaved with execution, you can not expand out a latex document to just a sequence of tex primitives. Oct 5 at 16:25
  • Could you explain what means to interleave the expansion with execution? I thought if original TeX was 'taught' all the LaTeX macros (e.g. by \def and \let commands), it could compile a modern LaTeX document. But now it seems it is not enough to just introduce all the LaTeX commands to TeX. Maybe to compile a modern LaTeX document one needs a corresponding compiler program (not for TeX but for LaTeX itself)...
    – Vladimir
    Oct 9 at 8:47
  • @Vladimir there is no separate latex binary, the latex program is the tex program, but what you can not do is expand out a latex document to remove all the latex commands and just have primitives. Oct 9 at 19:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.