Skip to main content

Module kernel_autoload

Module kernel_autoload 

Source
Expand description

Autoload the LaTeX format when an undefined control sequence turns out to be one the LaTeX kernel defines.

§Why this exists

In real LaTeX there is no such thing as “before the kernel”: latex.ltx is the format, so every kernel command is live from token one. LaTeXML (Perl and Rust alike) instead loads LaTeX.pool lazily, on first sight of a trigger control sequence — the list ported from Perl TeX.pool.ltxml L33-56 (\documentclass, \newcommand, \begin, …), installed in latexml_engine::tex.

A curated trigger list is incomplete by construction. Any kernel command that is not on it — and a document may legitimately use one before \documentclass — is simply undefined, gets an <ltx:ERROR/> stub, and the document derails. The canonical case is the “use this class if installed” idiom

\IfFileExists{proc-l.cls}{\documentclass{proc-l}}{\documentclass{amsproc}}

where the collapsed conditional means no class is ever selected and the run cascades into Fatal:TooManyErrors. Same-host Perl LaTeXML fails identically (see docs/parity/KNOWN_PERL_ERRORS.md — shared defect, upstream candidate), so this is a “at parity, still a bug” fix rather than a divergence repair.

§What this module is

The single funnel through which the undefined-CS paths ask “should the LaTeX kernel be loaded for this token instead of erroring?”. It holds no policy of its own: the answer comes from a hook the engine registers at TeX.pool load time (set_hook), because deciding it needs the kernel dump and the pool loader, neither of which latexml_core owns.

The eager Perl trigger list is kept — it fires on a legitimate use before any error is raised, which this hook cannot do. This is the safety net beneath it, not a replacement.

§Call sites: two, deliberately not three

read_x_token’s Outcome::Undefined arm and invoke_token_undefined are the paths a CS reaches when it is actually being used. read_balanced’s “cs SHOULD have defn by now; report early!” branch — the third generate_error_stub caller, inside token-list scanning — is left alone on purpose: it fires while collecting an \edef-style body rather than executing it, and loading a format mid-scan buys a rare case (\edef\x{\IfFileExists…} before \documentclass) at the price of running the whole pool from inside a partially-read token list. Wire it up only with a reproducer that needs it. A provider named for first use (provide_on_first_use) is asked there too: it defines a few commands, not a format, so the scan reads the name again.

Functions§

name_first_use_provider
Name provider, an expandable command that defines a family of commands where still undefined, as what an undefined token of that family runs on its first use ("{cs}:first@use@provider", global). Nothing is defined until then, so \ifdefined, \@ifundefined and a document’s own \newcommand see what TeX sees. Beyond Perl, which has no such seam: the ar5iv preload names hyperref’s and atbegshi’s user commands (OXIDIZED_DESIGN #588).
name_undefined_fallback
Name stand_in as what a use of the undefined token reads ("{cs}:undefined@fallback", global), for undefined_fallback.
provide_on_first_use
Run the provider named for the undefined token (name_first_use_provider), until it has provided once; true ⇒ token now has a meaning, retry it. Asked at every undefined-CS site, after try_autoload and try_refused_environment_definition.
reset_first_use_providers
Forget that a provider was named (reset_thread_engine).
reset_undefined_fallback
Forget that a stand-in was named (reset_thread_engine).
set_hook
Register the engine’s kernel-autoload decision procedure. Called from TeX.pool’s definition load, which precedes every conversion. Repeat calls are ignored (the first registration wins).
set_refused_environment_hook
Register the engine’s RefusedEnvironmentHook (first registration wins).
try_autoload
Ask the registered hook whether token should pull the LaTeX kernel in.
try_refused_environment_definition
Ask the registered RefusedEnvironmentHook about an undefined token, at the same two call sites as try_autoload (after it): true ⇒ token now has a meaning, retry it.
undefined_fallback
The stand-in LaTeXML reads for an undefined token, if one is named (name_undefined_fallback): the name stays undefined — \ifdefined, \@ifundefined and \meaning see what TeX sees — and a use of it reads the stand-in, which is responsible for saying that TeX errs there. The class-less \chapter is the one named (OXIDIZED_DESIGN #734, latexml_engine supply_chapter_fallback). Asked at the two undefined-CS sites, after try_autoload and try_refused_environment_definition: Some ⇒ read the stand-in in place of token.

Type Aliases§

KernelAutoloadHook
Engine-supplied decision procedure for “is token a LaTeX kernel control sequence, and if so load the kernel and report whether it is now defined”.
RefusedEnvironmentHook
Engine-supplied procedure for “does an open environment whose class definition the kernel’s lock refused define token for its body, and if so define it as the class would have and report it defined” — a class’s \figure that makes \csentence for its captions (bmcart.cls:716), its \abstract that makes \parttitle (:1508). The same contract as KernelAutoloadHook: true ⇒ retry token.