Honour secnumdepth and tocdepth counters

This commit is contained in:
gloamxun committed 2026-10-24 23:38:08 +08:00
1 parent 9aacb91184
commit 32ca78bff5
4 files changed
+79 -14

No files matched your search

+75 -10
View File
@@ -187,6 +187,10 @@ struct PageGeometry {
images: HashMap<String, RasterImage>,
/// Directories searched for `\includegraphics` paths.
graphics_paths: Vec<String>,
/// `\setcounter{secnumdepth}`; article defaults to 3.
secnumdepth: Option<u32>,
/// `\setcounter{tocdepth}`; article defaults to 3.
tocdepth: Option<u32>,
}
/// A `\newtheorem` declaration: display name, shared counter and section scope.
@@ -868,6 +872,19 @@ fn expand_conditionals_at(
output
}
fn counter_assignment(line: &str, name: &str) -> Option<u32> {
let rest = line.strip_prefix("\\setcounter")?.trim_start();
let rest = rest.strip_prefix('{')?;
let close = rest.find('}')?;
if rest[..close].trim() != name {
return None;
}
let rest = rest[close + 1..].trim_start();
let rest = rest.strip_prefix('{')?;
let close = rest.find('}')?;
rest[..close].trim().parse().ok()
}
fn strip_page_geometry(source: &str) -> (String, PageGeometry) {
let mut geometry = PageGeometry::default();
let mut body = String::new();
@@ -918,6 +935,14 @@ fn strip_page_geometry(source: &str) -> (String, PageGeometry) {
}
continue;
}
if let Some(depth) = counter_assignment(trimmed, "secnumdepth") {
geometry.secnumdepth = Some(depth);
continue;
}
if let Some(depth) = counter_assignment(trimmed, "tocdepth") {
geometry.tocdepth = Some(depth);
continue;
}
if trimmed.starts_with("\\newcommand")
|| trimmed.starts_with("\\renewcommand")
|| trimmed.starts_with("\\providecommand")
@@ -3972,7 +3997,7 @@ fn expand_maketitle(blocks: &[SourceParagraph], geometry: &PageGeometry) -> Vec<
/// Advance the section counters and prefix each numbered heading with its
/// number. Labels inside a heading (or on their own line after it) resolve to
/// that number, and label-only blocks are removed.
fn number_sections(blocks: &mut Vec<SourceParagraph>) -> Vec<(String, String)> {
fn number_sections(blocks: &mut Vec<SourceParagraph>, secnumdepth: u32) -> Vec<(String, String)> {
let mut counters = [0u32; 5];
let mut appendix = false;
let mut labels = Vec::new();
@@ -4031,6 +4056,14 @@ fn number_sections(blocks: &mut Vec<SourceParagraph>) -> Vec<(String, String)> {
}
parts.join(".")
};
if level as u32 > secnumdepth {
let value = last_number.clone().unwrap_or_else(|| "??".to_string());
for label in block_labels {
labels.push((label, value.clone()));
}
index += 1;
continue;
}
blocks[index].heading_number = Some(number.clone());
last_number = Some(number.clone());
for label in block_labels {
@@ -4247,11 +4280,13 @@ fn append_bibliography(paragraphs: &mut Vec<SourceParagraph>, bibliography: &[(S
}
}
fn expand_toc(paragraphs: &mut Vec<SourceParagraph>) {
fn expand_toc(paragraphs: &mut Vec<SourceParagraph>, tocdepth: u32) {
let entries: Vec<(u8, String)> = paragraphs
.iter()
.filter(|paragraph| {
paragraph.heading_level.is_some_and(|level| level <= 3)
paragraph
.heading_level
.is_some_and(|level| level as u32 <= tocdepth)
&& !paragraph.heading_starred
&& paragraph.heading_number.is_some()
})
@@ -4350,8 +4385,8 @@ fn expand_float_lists(paragraphs: &mut Vec<SourceParagraph>) {
fn build_document(paragraphs: &[SourceParagraph], geometry: &PageGeometry) -> Document {
let mut paragraphs = expand_maketitle(paragraphs, geometry);
let section_labels = number_sections(&mut paragraphs);
expand_toc(&mut paragraphs);
let section_labels = number_sections(&mut paragraphs, geometry.secnumdepth.unwrap_or(3));
expand_toc(&mut paragraphs, geometry.tocdepth.unwrap_or(3));
let mut references = section_labels;
references.extend(number_theorems(
&mut paragraphs,
@@ -5815,11 +5850,41 @@ A definition.
assert_eq!(document.units[1].source, "Table 1: Numbers");
}
#[test]
fn secnumdepth_stops_numbering_deep_headings() {
let (body, geometry) =
geometry_and_body("\\setcounter{secnumdepth}{1}\n\n\\section{A}\n\n\\subsection{B}");
assert_eq!(geometry.secnumdepth, Some(1));
let mut blocks = paragraphs_with_definitions(&body, &[]).expect("paragraphs");
number_sections(&mut blocks, 1);
assert_eq!(blocks[0].text, "1 A");
assert_eq!(blocks[0].heading_number.as_deref(), Some("1"));
assert_eq!(blocks[1].text, "B");
assert_eq!(blocks[1].heading_number, None);
}
#[test]
fn tocdepth_limits_the_table_of_contents() {
let source =
"\\tableofcontents\n\n\\setcounter{tocdepth}{1}\n\n\\section{A}\n\n\\subsection{B}";
let (body, geometry) = geometry_and_body(source);
assert_eq!(geometry.tocdepth, Some(1));
let mut blocks = paragraphs_with_definitions(&body, &[]).expect("paragraphs");
number_sections(&mut blocks, 3);
expand_toc(&mut blocks, 1);
let section = blocks.iter().filter(|block| block.text == "1 A").count();
let subsection = blocks.iter().filter(|block| block.text == "1.1 B").count();
assert_eq!(section, 2, "heading plus one TOC line");
assert_eq!(subsection, 1, "heading only; no TOC line");
}
#[test]
fn paragraph_headings_number_to_five_levels() {
let source = "\\section{A}\n\n\\paragraph{P}\n\n\\subparagraph{S}\n\n\\subsection{B}\n\n\\paragraph{Q}";
let mut blocks = paragraphs(source).expect("paragraphs");
number_sections(&mut blocks);
// Article's default secnumdepth is 3; the five-level counters are
// reachable by raising it, which LaTeX classes do for reports.
number_sections(&mut blocks, 5);
let numbers: Vec<&str> = blocks
.iter()
.filter(|block| block.heading_level.is_some())
@@ -5835,11 +5900,11 @@ A definition.
fn table_of_contents_stops_at_subsubsections() {
let source = "\\tableofcontents\n\n\\section{A}\n\n\\paragraph{P}";
let mut blocks = paragraphs(source).expect("paragraphs");
number_sections(&mut blocks);
expand_toc(&mut blocks);
number_sections(&mut blocks, 3);
expand_toc(&mut blocks, 3);
let paragraph_entries = blocks
.iter()
.filter(|block| block.text.trim().ends_with(" P"))
.filter(|block| block.text.trim() == "P")
.count();
assert_eq!(paragraph_entries, 1);
assert!(
@@ -6130,7 +6195,7 @@ A definition.
fn headings_are_numbered_and_starred_headings_are_not() {
let source = "\\section{One}\n\n\\subsection{Two}\n\n\\section*{Star}\n\n\\section{Three}";
let mut blocks = paragraphs(source).expect("blocks");
number_sections(&mut blocks);
number_sections(&mut blocks, 3);
assert_eq!(blocks[0].text, "1 One");
assert_eq!(blocks[1].text, "1.1 Two");
assert_eq!(blocks[2].text, "Star");