Add chapter headings and chapter-prefixed numbering

This commit is contained in:
gloamxun committed 2026-10-19 14:23:53 +08:00
1 parent 6d4aa37189
commit a798221817
7 files changed
+68 -11

No files matched your search

+1 -1
View File
@@ -490,7 +490,7 @@ advisory breaks, starred `\vspace`/`\hspace`, `center`, `\hrule`, `\fbox`,
| Notes/floats | footnotes with page-bottom insertion area, title/author `\thanks`, figure/table `[htbp]` placement (top/bottom/page/here approximations) | deferred float queue, `\textfraction` | | Notes/floats | footnotes with page-bottom insertion area, title/author `\thanks`, figure/table `[htbp]` placement (top/bottom/page/here approximations) | deferred 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 | class-specific section formatting | | Classes/packages | built-in + local classes, option protocol, local package options, base font sizes 10/11/12pt, chapter headings and numbering | class-specific formatting (automatic chapter page 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
+19 -5
View File
@@ -460,7 +460,8 @@ struct Parser {
text_style_stack: Vec<(expanded::TextStyle, bool, bool, bool, bool, bool)>, text_style_stack: Vec<(expanded::TextStyle, bool, bool, bool, bool, bool)>,
style_declarations: Vec<(usize, expanded::TextStyle)>, style_declarations: Vec<(usize, expanded::TextStyle)>,
group_depth: usize, group_depth: usize,
section_numbers: [u32; 3], section_numbers: [u32; 4],
has_chapters: bool,
equation_number: u32, equation_number: u32,
figure_number: u32, figure_number: u32,
table_number: u32, table_number: u32,
@@ -518,7 +519,8 @@ impl Parser {
text_style_stack: Vec::new(), text_style_stack: Vec::new(),
style_declarations: Vec::new(), style_declarations: Vec::new(),
group_depth: 0, group_depth: 0,
section_numbers: [0; 3], section_numbers: [0; 4],
has_chapters: false,
equation_number: 0, equation_number: 0,
figure_number: 0, figure_number: 0,
table_number: 0, table_number: 0,
@@ -1257,7 +1259,10 @@ impl Parser {
fn start_heading(&mut self, level: u8, numbered: bool) { fn start_heading(&mut self, level: u8, numbered: bool) {
self.flush_paragraph(); self.flush_paragraph();
self.finish_heading(); self.finish_heading();
let level = level.clamp(1, 3); let level = level.min(3);
if level == 0 {
self.has_chapters = true;
}
let runs = if numbered { let runs = if numbered {
let number = self.next_section_number(level); let number = self.next_section_number(level);
self.current_reference = number.clone(); self.current_reference = number.clone();
@@ -1941,10 +1946,19 @@ impl Parser {
} }
fn next_section_number(&mut self, level: u8) -> String { fn next_section_number(&mut self, level: u8) -> String {
let index = usize::from(level.saturating_sub(1).min(2)); let index = usize::from(level.min(3));
self.section_numbers[index] += 1; self.section_numbers[index] += 1;
self.section_numbers[index + 1..].fill(0); self.section_numbers[index + 1..].fill(0);
self.section_numbers[..=index] if level == 0 {
self.has_chapters = true;
return self.section_numbers[0].to_string();
}
let start = if self.has_chapters && self.section_numbers[0] > 0 {
0
} else {
1
};
self.section_numbers[start..=index]
.iter() .iter()
.map(u32::to_string) .map(u32::to_string)
.collect::<Vec<_>>() .collect::<Vec<_>>()
+2 -1
View File
@@ -10,7 +10,8 @@ footnotes/`\thanks`; figure/table `[htbp]` placement approximations; page
styles (plain/fancy, `\thispagestyle`, fancyhdr headers/footers, `\thepage`); styles (plain/fancy, `\thispagestyle`, fancyhdr headers/footers, `\thepage`);
geometry-aware `\textwidth`/`\textheight` named length registers; table of 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
(`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).
+7 -4
View File
@@ -34,9 +34,11 @@ the source changes; the SVG preview is refreshed per page.
- Notes: `\footnote{...}` page-bottom insertion area with separator rule, - Notes: `\footnote{...}` page-bottom insertion area with separator rule,
superscript markers, and title/author `\thanks`. superscript markers, and title/author `\thanks`.
- Contents: `\tableofcontents`, `\listoffigures`, and `\listoftables` - Contents: `\tableofcontents`, `\listoffigures`, and `\listoftables`
generate headings plus entries from numbered sections and captioned generate headings plus entries from numbered chapters/sections and captioned
figures/tables, with dotted leaders and page numbers resolved after figures/tables, with dotted leaders and page numbers resolved after
pagination. pagination.
- Chapters: `\chapter` is a level-0 heading; chapter numbers prefix section
numbers (`1.1`, `1.1.1`) and reset lower counters.
- 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).
@@ -63,8 +65,9 @@ See `docs/ROADMAP.md` for concrete implementation plans.
- Notes: footnotes inside captions, floats, or arbitrary boxes are not - Notes: footnotes inside captions, floats, or arbitrary boxes are not
collected; the insertion area currently follows the page where the paragraph collected; the insertion area currently follows the page where the paragraph
ends rather than the exact reference line. ends rather than the exact reference line.
- Contents: only numbered headings plus captions are listed; per-chapter - Contents: chapter headings are listed with chapter-prefixed section
contents in book/report are not distinguished. numbers, but contents are not split per chapter and `\chapter` does not
force a page break.
- Miscellaneous: a document-level `\renewcommand{\maketitle}` is ignored in - Miscellaneous: a document-level `\renewcommand{\maketitle}` is ignored in
favor of the engine title block. favor of the engine title block.
@@ -72,7 +75,7 @@ See `docs/ROADMAP.md` for concrete implementation plans.
- `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` (36 tests: 32 compiler - `cargo fmt --check` passes; `cargo test --offline` (37 tests: 33 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
+2
View File
@@ -37,6 +37,8 @@ use self::layout::{
// from the article class's 14.4pt setting to 14pt. // from the article class's 14.4pt setting to 14pt.
fn article_heading_font_size(level: u8, base: u8) -> u8 { fn article_heading_font_size(level: u8, base: u8) -> u8 {
match (base, level) { match (base, level) {
(12, 0) => 24,
(_, 0) => 20,
(12, 1) => 17, (12, 1) => 17,
(_, 1) => 14, (_, 1) => 14,
(12, 2) => 14, (12, 2) => 14,
+11
View File
@@ -5832,6 +5832,13 @@ fn heading_start_primitive(
Ok(()) Ok(())
} }
fn chapter_primitive(
token: token::Token,
input: &mut vm::ExecutionInput<CompilerState>,
) -> texlang::prelude::Result<()> {
heading_primitive(token, input, 0)
}
fn section_primitive( fn section_primitive(
token: token::Token, token: token::Token,
input: &mut vm::ExecutionInput<CompilerState>, input: &mut vm::ExecutionInput<CompilerState>,
@@ -7203,6 +7210,10 @@ fn built_in_commands() -> HashMap<&'static str, texlang::command::BuiltIn<Compil
"texrraend", "texrraend",
texlang::command::BuiltIn::new_execution(end_environment_primitive), texlang::command::BuiltIn::new_execution(end_environment_primitive),
); );
built_ins.insert(
"chapter",
texlang::command::BuiltIn::new_execution(chapter_primitive),
);
built_ins.insert( built_ins.insert(
"section", "section",
texlang::command::BuiltIn::new_execution(section_primitive), texlang::command::BuiltIn::new_execution(section_primitive),
+26
View File
@@ -951,3 +951,29 @@ fn centering_declaration_centers_a_paragraph() {
"centering leaked into the following paragraph: centered={plain_alpha}, left={left_plain}" "centering leaked into the following paragraph: centered={plain_alpha}, left={left_plain}"
); );
} }
#[test]
fn chapter_headings_number_sections_and_enter_contents() {
let source = r#"\documentclass{book}
\begin{document}
\tableofcontents
\chapter{First Chapter}
\section{Alpha}
\subsection{Beta}
\chapter{Second Chapter}
\section{Gamma}
\end{document}
"#;
let value = fresh_preview(source, "chapters.tex");
assert_eq!(value["status"], "ok", "{}", value["message"]);
let svg = page_svg(&value, 0);
for needle in [
"Contents", "First", "Chapter", "Alpha", "Beta", "Gamma", "1.1", "1.1.1", "2.1",
] {
assert!(svg.contains(needle), "chapter numbering is missing : {svg}");
}
assert!(
!svg.contains("0.1"),
"section was numbered under chapter zero: {svg}"
);
}