Allow documents to replace the title block

This commit is contained in:
gloamxun committed 2026-10-19 14:32:35 +08:00
1 parent 23cf1f13de
commit 5e9552d785
8 files changed
+88 -17

No files matched your search

+7 -5
View File
@@ -315,10 +315,12 @@ a 0.4pt frame, and painted in both the PDF and the SVG preview.
Base font-size class options `10pt`, `11pt`, and `12pt` now set the body Base font-size class options `10pt`, `11pt`, and `12pt` now set the body
size and propagate through headings, title material, lists, tables, block size and propagate through headings, title material, lists, tables, block
boxes, footnotes, and the default paragraph indent. boxes, footnotes, and the default paragraph indent.
Known gaps for full document classes: custom `\maketitle` implementations A document-level `\renewcommand{\maketitle}` replaces the engine title block;
are ignored (the engine title layout is used), `\parbox`/`\minipage` are class and package files that replace `\maketitle` are ignored so template
block-level rather than inline, and `\vbox` and `\vtop` map to a full-width title code cannot break the native layout. Known gaps for full document
block box rather than true nested VBox material. classes: `\parbox`/`\minipage` are block-level rather than inline, and
`\vbox` and `\vtop` map to a full-width block box rather than true nested
VBox material.
`\footnote{...}` captures its content, emits a superscript marker, reserves `\footnote{...}` captures its content, emits a superscript marker, reserves
space on the page where the paragraph ends, and paints the notes with a space on the page where the paragraph ends, and paints the notes with a
@@ -490,7 +492,7 @@ advisory breaks, starred `\vspace`/`\hspace`, `center`, `\hrule`, `\fbox`,
| Notes/floats | footnotes with page-bottom insertion area, title/author `\thanks`, figure `[htbp]` placement (deferred top floats, bottom/page/here), table page breaks | table float queue, `\textfraction` | | Notes/floats | footnotes with page-bottom insertion area, title/author `\thanks`, figure `[htbp]` placement (deferred top floats, bottom/page/here), table page breaks | table float queue, `\textfraction` |
| Contents | `\tableofcontents`, `\listoffigures`, `\listoftables` generated from numbered headings and captions, dotted leaders, page numbers resolved after pagination | per-chapter book/report contents | | Contents | `\tableofcontents`, `\listoffigures`, `\listoftables` generated from numbered headings and captions, dotted leaders, page numbers resolved after pagination | per-chapter book/report contents |
| Pages | A4/Letter/... page sizes, margins/geometry, automatic pagination, `\newpage`, `\pagebreak`, plain/fancy page styles, `\thispagestyle`, fancyhdr L/C/R headers and footers | — | | Pages | A4/Letter/... page sizes, margins/geometry, automatic pagination, `\newpage`, `\pagebreak`, plain/fancy page styles, `\thispagestyle`, fancyhdr L/C/R headers and footers | — |
| Classes/packages | built-in + local classes, option protocol, local package options, base font sizes 10/11/12pt, chapter headings and numbering, chapter page breaks | class-specific formatting beyond chapter breaks | | Classes/packages | built-in + local classes, option protocol, local package options, base font sizes 10/11/12pt, chapter headings and numbering, chapter page breaks, document-level custom `\maketitle` | class-specific formatting beyond chapter breaks |
| Live preview | incremental paragraph cache, page-hash diffing, SVG refresh | — | | Live preview | incremental paragraph cache, page-hash diffing, SVG refresh | — |
The compatibility layer also covers spacing macros, text symbols/letters, style The compatibility layer also covers spacing macros, text symbols/letters, style
@@ -910,6 +910,14 @@ impl Parser {
self.push_block(Block::FramedBox { runs, frame, width }); self.push_block(Block::FramedBox { runs, frame, width });
} }
} }
expanded::FrontendEventKind::MetadataValue(field) => {
let value = match field {
expanded::MetadataField::Title => self.document.title.clone(),
expanded::MetadataField::Author => self.document.author.clone(),
expanded::MetadataField::Date => self.document.date.clone(),
};
self.append_text(&value);
}
expanded::FrontendEventKind::MetadataStart(field) => { expanded::FrontendEventKind::MetadataStart(field) => {
self.metadata_capture = Some((*field, Vec::new())); self.metadata_capture = Some((*field, Vec::new()));
} }
+1
View File
@@ -108,6 +108,7 @@ pub enum FrontendEventKind {
FootnoteStart, FootnoteStart,
FootnoteEnd, FootnoteEnd,
MetadataStart(MetadataField), MetadataStart(MetadataField),
MetadataValue(MetadataField),
MetadataEnd(MetadataField), MetadataEnd(MetadataField),
DisplayMathStart, DisplayMathStart,
DisplayMathEnd(MathDelimiterSettings), DisplayMathEnd(MathDelimiterSettings),
+1 -6
View File
@@ -12,7 +12,7 @@ geometry-aware `\textwidth`/`\textheight` named length registers; table of
contents and figure/table lists with dotted leaders; native PDF rectangles; unified pagination; contents and figure/table lists with dotted leaders; native PDF rectangles; unified pagination;
`\centering`/`\raggedright`/`\raggedleft` declarations and environments; `\centering`/`\raggedright`/`\raggedleft` declarations and environments;
chapter headings with chapter-prefixed section numbering and page breaks; chapter headings with chapter-prefixed section numbering and page breaks;
deferred top-float queue for figures deferred top-float queue for figures; document-level custom `\maketitle`
(`build_positioned_pages` is the only page builder and no separate break (`build_positioned_pages` is the only page builder and no separate break
planner remains). planner remains).
@@ -28,8 +28,3 @@ planner remains).
- `\vbox`/`\vtop` currently map to a full-width block `\parbox`. A real VBox - `\vbox`/`\vtop` currently map to a full-width block `\parbox`. A real VBox
needs a vertical-material capture with its own natural width, baseline, and needs a vertical-material capture with its own natural width, baseline, and
nested line/box children in the positioned renderer. nested line/box children in the positioned renderer.
## 3. Custom title blocks (remaining)
- Let a document-level `\renewcommand{\maketitle}` contribute a real title
block once the engine's title model can run box material.
+5 -3
View File
@@ -46,6 +46,10 @@ the source changes; the SVG preview is refreshed per page.
- Class sizes: 10pt/11pt/12pt set the body size and propagate through - Class sizes: 10pt/11pt/12pt set the body size and propagate through
headings, title material, lists, tables, block boxes, footnotes, and the headings, title material, lists, tables, block boxes, footnotes, and the
default paragraph indent (10/12pt use 15pt; 11pt uses 17pt). default paragraph indent (10/12pt use 15pt; 11pt uses 17pt).
- Title blocks: a document-level `\renewcommand{\maketitle}` replaces the
engine title layout and can use `\@title`/`\@author`/`\@date`; class and
package replacements are ignored so template title code keeps using the
native layout.
- Classes/packages: built-in classes, local `.cls`/`.sty`, paper options, - Classes/packages: built-in classes, local `.cls`/`.sty`, paper options,
`\DeclareOption`/`\ProcessOptions`/`\ExecuteOptions`/option passing. `\DeclareOption`/`\ProcessOptions`/`\ExecuteOptions`/option passing.
- Compatibility: spacing macros (quad/qquad/thin/en/med/thick, `\vspace*`, - Compatibility: spacing macros (quad/qquad/thin/en/med/thick, `\vspace*`,
@@ -70,14 +74,12 @@ See `docs/ROADMAP.md` for concrete implementation plans.
ends rather than the exact reference line. ends rather than the exact reference line.
- Contents: chapter headings are listed with chapter-prefixed section - Contents: chapter headings are listed with chapter-prefixed section
numbers, but contents are not split per chapter. numbers, but contents are not split per chapter.
- Miscellaneous: a document-level `\renewcommand{\maketitle}` is ignored in
favor of the engine title block.
## Verification ## Verification
- `examples/hello.tex`, `examples/boxes.tex`, `examples/compat.tex` compile to - `examples/hello.tex`, `examples/boxes.tex`, `examples/compat.tex` compile to
one A4 page each; the arXiv template project compiles to 3 Letter pages. one A4 page each; the arXiv template project compiles to 3 Letter pages.
- `cargo fmt --check` passes; `cargo test --offline` (37 tests: 33 compiler - `cargo fmt --check` passes; `cargo test --offline` (38 tests: 34 compiler
integration tests + 4 server tests) covers the three examples, the arXiv integration tests + 4 server tests) covers the three examples, the arXiv
fixture, floats/newpage, page styles and `\thispagestyle`, SVG/PDF output, fixture, floats/newpage, page styles and `\thispagestyle`, SVG/PDF output,
project API, static frontend assets, path traversal, the legacy project API, static frontend assets, path traversal, the legacy
+3 -3
View File
@@ -22,6 +22,9 @@ const PRELUDE: &str = r#"
\def\title#1{\texrrametadatastart{title}#1\texrrametadataend{title}} \def\title#1{\texrrametadatastart{title}#1\texrrametadataend{title}}
\def\author#1{\texrrametadatastart{author}#1\texrrametadataend{author}} \def\author#1{\texrrametadatastart{author}#1\texrrametadataend{author}}
\def\date#1{\texrrametadatastart{date}#1\texrrametadataend{date}} \def\date#1{\texrrametadatastart{date}#1\texrrametadataend{date}}
\def\@title{\texrrametadatatitle}
\def\@author{\texrrametadataauthor}
\def\@date{\texrrametadatadate}
\def\today{__TEXRRA_TODAY__} \def\today{__TEXRRA_TODAY__}
\def\maketitle{\texrramaketitle} \def\maketitle{\texrramaketitle}
\def\noindent{\texrranoindent} \def\noindent{\texrranoindent}
@@ -361,9 +364,6 @@ const PRELUDE: &str = r#"
\def\thefootnote{*} \def\thefootnote{*}
\def\footnoterule{} \def\footnoterule{}
\def\thanks#1{\footnote{#1}} \def\thanks#1{\footnote{#1}}
\def\@title{}
\def\@author{}
\def\@date{}
\def\@thanks{} \def\@thanks{}
\def\@makefntext#1{} \def\@makefntext#1{}
\def\@makefnmark{} \def\@makefnmark{}
+41
View File
@@ -2121,6 +2121,15 @@ fn metadata_field_argument(
} }
} }
fn metadata_value_primitive(
field: MetadataField,
token: token::Token,
input: &mut vm::ExecutionInput<CompilerState>,
) -> texlang::prelude::Result<()> {
write_event(input, token, FrontendEventKind::MetadataValue(field));
Ok(())
}
fn metadata_start_primitive( fn metadata_start_primitive(
token: token::Token, token: token::Token,
input: &mut vm::ExecutionInput<CompilerState>, input: &mut vm::ExecutionInput<CompilerState>,
@@ -4751,8 +4760,22 @@ fn define_latex_command(
// parser. Style files routinely replace them with box-level LaTeX // parser. Style files routinely replace them with box-level LaTeX
// implementations the engine cannot execute, so keep the native // implementations the engine cannot execute, so keep the native
// meaning instead of failing or replacing it with an unusable macro. // meaning instead of failing or replacing it with an unusable macro.
// A document may still replace its own title block in the main source.
let from_style_file = match input.vm().trace(token).origin {
trace::Origin::File(path) => matches!(
path.extension().and_then(|extension| extension.to_str()),
Some("sty" | "cls")
),
trace::Origin::Terminal => false,
};
let document_title_block = matches!(
&macro_name,
EngineCommandName::ControlSequence(name) if name == "maketitle"
);
if !document_title_block || from_style_file {
return Ok(()); return Ok(());
} }
}
let already_defined = input.state().engine.macros.get(&macro_name).is_some() let already_defined = input.state().engine.macros.get(&macro_name).is_some()
|| input.vm().commands_map.get_command(&name_ref).is_some(); || input.vm().commands_map.get_command(&name_ref).is_some();
let define = match definition_kind { let define = match definition_kind {
@@ -6904,6 +6927,24 @@ fn built_in_commands() -> HashMap<&'static str, texlang::command::BuiltIn<Compil
"texrramaketitle", "texrramaketitle",
texlang::command::BuiltIn::new_execution(make_title_primitive), texlang::command::BuiltIn::new_execution(make_title_primitive),
); );
built_ins.insert(
"texrrametadatatitle",
texlang::command::BuiltIn::new_execution(|token, input| {
metadata_value_primitive(MetadataField::Title, token, input)
}),
);
built_ins.insert(
"texrrametadataauthor",
texlang::command::BuiltIn::new_execution(|token, input| {
metadata_value_primitive(MetadataField::Author, token, input)
}),
);
built_ins.insert(
"texrrametadatadate",
texlang::command::BuiltIn::new_execution(|token, input| {
metadata_value_primitive(MetadataField::Date, token, input)
}),
);
built_ins.insert( built_ins.insert(
"texrrametadatastart", "texrrametadatastart",
texlang::command::BuiltIn::new_execution(metadata_start_primitive), texlang::command::BuiltIn::new_execution(metadata_start_primitive),
+22
View File
@@ -1000,3 +1000,25 @@ fn chapter_headings_number_sections_and_enter_contents() {
"section was numbered under chapter zero: {svg}" "section was numbered under chapter zero: {svg}"
); );
} }
#[test]
fn custom_maketitle_replaces_the_engine_title_block() {
let source = r#"\documentclass{article}
\title{Custom title}
\author{Engine author}
\renewcommand{\maketitle}{\begin{center}\@title\end{center}}
\begin{document}
\maketitle
Body text.
\end{document}
"#;
let value = fresh_preview(source, "custom-title.tex");
assert_eq!(value["status"], "ok", "{}", value["message"]);
let svg = page_svg(&value, 0);
assert!(svg.contains("Custom"), "custom title missing: {svg}");
assert!(
!svg.contains("Engine title"),
"engine title block was not replaced: {svg}"
);
assert!(svg.contains("Body"), "body text missing: {svg}");
}