Offset section and float counters with setcounter
This commit is contained in:
1 parent
ea0468783e
commit
2640884721
4 files changed
+92
-21
No files matched your search
+88
-17
@@ -157,6 +157,8 @@ struct PageGeometry {
|
||||
list_figure_name: Option<String>,
|
||||
/// `\\listtablename` heading for the list of tables.
|
||||
list_table_name: Option<String>,
|
||||
/// `\\setcounter` assignments keyed by counter name.
|
||||
counters: Vec<(String, u32)>,
|
||||
left_skip: Option<i64>,
|
||||
right_skip: Option<i64>,
|
||||
tolerance: Option<i64>,
|
||||
@@ -950,6 +952,28 @@ fn counter_assignment(line: &str, name: &str) -> Option<u32> {
|
||||
rest[..close].trim().parse().ok()
|
||||
}
|
||||
|
||||
fn any_counter_assignment(line: &str) -> Option<(String, u32)> {
|
||||
let rest = line.strip_prefix("\\setcounter")?.trim_start();
|
||||
let rest = rest.strip_prefix('{')?;
|
||||
let close = rest.find('}')?;
|
||||
let name = rest[..close].trim().to_string();
|
||||
let rest = rest[close + 1..].trim_start();
|
||||
let rest = rest.strip_prefix('{')?;
|
||||
let close = rest.find('}')?;
|
||||
let value = rest[..close].trim().parse().ok()?;
|
||||
Some((name, value))
|
||||
}
|
||||
|
||||
fn geometry_counter(geometry: &PageGeometry, name: &str) -> u32 {
|
||||
geometry
|
||||
.counters
|
||||
.iter()
|
||||
.rev()
|
||||
.find(|(counter, _)| counter == name)
|
||||
.map(|(_, value)| *value)
|
||||
.unwrap_or(0)
|
||||
}
|
||||
|
||||
fn strip_page_geometry(source: &str) -> (String, PageGeometry) {
|
||||
let mut geometry = PageGeometry::default();
|
||||
let mut body = String::new();
|
||||
@@ -1000,12 +1024,12 @@ 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);
|
||||
if let Some((name, value)) = any_counter_assignment(trimmed) {
|
||||
match name.as_str() {
|
||||
"secnumdepth" => geometry.secnumdepth = Some(value),
|
||||
"tocdepth" => geometry.tocdepth = Some(value),
|
||||
_ => geometry.counters.push((name, value)),
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if let Some(spread) = baselinestretch_assignment(trimmed) {
|
||||
@@ -4244,8 +4268,12 @@ 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>, secnumdepth: u32) -> Vec<(String, String)> {
|
||||
let mut counters = [0u32; 5];
|
||||
fn number_sections(
|
||||
blocks: &mut Vec<SourceParagraph>,
|
||||
secnumdepth: u32,
|
||||
section_start: u32,
|
||||
) -> Vec<(String, String)> {
|
||||
let mut counters = [section_start, 0u32, 0, 0, 0];
|
||||
let mut appendix = false;
|
||||
let mut labels = Vec::new();
|
||||
let mut last_number: Option<String> = None;
|
||||
@@ -4463,9 +4491,11 @@ fn number_floats(
|
||||
paragraphs: &mut [SourceParagraph],
|
||||
figure_name: &str,
|
||||
table_name: &str,
|
||||
figure_start: u32,
|
||||
table_start: u32,
|
||||
) -> Vec<(String, String)> {
|
||||
let mut figures = 0u32;
|
||||
let mut tables = 0u32;
|
||||
let mut figures = figure_start;
|
||||
let mut tables = table_start;
|
||||
let mut labels = Vec::new();
|
||||
for paragraph in paragraphs {
|
||||
let Some(spec) = paragraph.float.as_mut() else {
|
||||
@@ -4705,7 +4735,11 @@ fn build_document(paragraphs: &[SourceParagraph], geometry: &PageGeometry) -> Do
|
||||
if let Some(name) = geometry.abstract_name.as_deref() {
|
||||
rename_abstract_heading(&mut paragraphs, name);
|
||||
}
|
||||
let section_labels = number_sections(&mut paragraphs, geometry.secnumdepth.unwrap_or(3));
|
||||
let section_labels = number_sections(
|
||||
&mut paragraphs,
|
||||
geometry.secnumdepth.unwrap_or(3),
|
||||
geometry_counter(geometry, "section"),
|
||||
);
|
||||
expand_toc(
|
||||
&mut paragraphs,
|
||||
geometry.tocdepth.unwrap_or(3),
|
||||
@@ -4720,6 +4754,8 @@ fn build_document(paragraphs: &[SourceParagraph], geometry: &PageGeometry) -> Do
|
||||
&mut paragraphs,
|
||||
geometry.figure_name.as_deref().unwrap_or("Figure"),
|
||||
geometry.table_name.as_deref().unwrap_or("Table"),
|
||||
geometry_counter(geometry, "figure"),
|
||||
geometry_counter(geometry, "table"),
|
||||
));
|
||||
expand_float_lists(
|
||||
&mut paragraphs,
|
||||
@@ -5196,6 +5232,8 @@ mod tests {
|
||||
&mut blocks,
|
||||
geometry.figure_name.as_deref().unwrap_or("Figure"),
|
||||
geometry.table_name.as_deref().unwrap_or("Table"),
|
||||
0,
|
||||
0,
|
||||
);
|
||||
let caption = blocks[0]
|
||||
.float
|
||||
@@ -5213,6 +5251,8 @@ mod tests {
|
||||
&mut blocks,
|
||||
geometry.figure_name.as_deref().unwrap_or("Figure"),
|
||||
geometry.table_name.as_deref().unwrap_or("Table"),
|
||||
0,
|
||||
0,
|
||||
);
|
||||
let caption = blocks[0]
|
||||
.float
|
||||
@@ -5663,7 +5703,7 @@ mod source_tests {
|
||||
let (body, _) = geometry_and_body(source);
|
||||
let mut blocks = paragraphs(&body).expect("blocks");
|
||||
assert_eq!(blocks.len(), 2);
|
||||
number_floats(&mut blocks, "Figure", "Table");
|
||||
number_floats(&mut blocks, "Figure", "Table", 0, 0);
|
||||
let first = blocks[0].float.as_ref().expect("first float");
|
||||
assert_eq!(first.caption.as_deref(), Some("Note"));
|
||||
let second = blocks[1].float.as_ref().expect("second float");
|
||||
@@ -6504,6 +6544,37 @@ A definition.
|
||||
assert_eq!(document.units[3].source, "Table 1: Numbers");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn setcounter_offsets_figures_and_tables() {
|
||||
let source = "\\setcounter{figure}{5}\n\n\\setcounter{table}{2}\n\n\\begin{figure}\n\\caption{A}\n\\end{figure}\n\n\\begin{table}\n\\caption{B}\n\\end{table}";
|
||||
let (body, geometry) = geometry_and_body(source);
|
||||
let mut blocks =
|
||||
paragraphs_with_definitions(&body, &geometry.theorem_definitions).expect("blocks");
|
||||
number_floats(
|
||||
&mut blocks,
|
||||
"Figure",
|
||||
"Table",
|
||||
geometry_counter(&geometry, "figure"),
|
||||
geometry_counter(&geometry, "table"),
|
||||
);
|
||||
let captions: Vec<&str> = blocks
|
||||
.iter()
|
||||
.filter_map(|block| block.float.as_ref()?.caption.as_deref())
|
||||
.collect();
|
||||
assert!(captions.contains(&"Figure 6: A"), "{captions:?}");
|
||||
assert!(captions.contains(&"Table 3: B"), "{captions:?}");
|
||||
}
|
||||
#[test]
|
||||
fn setcounter_offsets_section_numbers() {
|
||||
let source = "\\setcounter{section}{4}\n\n\\section{Intro}\n\nText.";
|
||||
let (body, geometry) = geometry_and_body(source);
|
||||
assert_eq!(geometry_counter(&geometry, "section"), 4);
|
||||
let paragraphs =
|
||||
paragraphs_with_definitions(&body, &geometry.theorem_definitions).expect("blocks");
|
||||
let document = build_document(¶graphs, &geometry);
|
||||
assert_eq!(document.units[0].source, "5 Intro");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn float_list_names_follow_renewcommand() {
|
||||
let source = "\\renewcommand{\\listfigurename}{Abbildungen}\n\n\\renewcommand{\\listtablename}{Tabellen}\n\n\\listoffigures\n\n\\begin{figure}\n\\caption{Plot}\n\\end{figure}\n\n\\listoftables\n\n\\begin{table}\n\\begin{tabular}{l}\nA\n\\end{tabular}\n\\caption{Numbers}\n\\end{table}";
|
||||
@@ -6529,7 +6600,7 @@ A definition.
|
||||
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);
|
||||
number_sections(&mut blocks, 1, 0);
|
||||
assert_eq!(blocks[0].text, "1 A");
|
||||
assert_eq!(blocks[0].heading_number.as_deref(), Some("1"));
|
||||
assert_eq!(blocks[1].text, "B");
|
||||
@@ -6543,7 +6614,7 @@ A definition.
|
||||
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);
|
||||
number_sections(&mut blocks, 3, 0);
|
||||
expand_toc(&mut blocks, 1, "Contents");
|
||||
let section = blocks.iter().filter(|block| block.text == "1 A").count();
|
||||
let subsection = blocks.iter().filter(|block| block.text == "1.1 B").count();
|
||||
@@ -6557,7 +6628,7 @@ A definition.
|
||||
let mut blocks = paragraphs(source).expect("paragraphs");
|
||||
// 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);
|
||||
number_sections(&mut blocks, 5, 0);
|
||||
let numbers: Vec<&str> = blocks
|
||||
.iter()
|
||||
.filter(|block| block.heading_level.is_some())
|
||||
@@ -6573,7 +6644,7 @@ 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, 3);
|
||||
number_sections(&mut blocks, 3, 0);
|
||||
expand_toc(&mut blocks, 3, "Contents");
|
||||
let paragraph_entries = blocks
|
||||
.iter()
|
||||
@@ -6921,7 +6992,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, 3);
|
||||
number_sections(&mut blocks, 3, 0);
|
||||
assert_eq!(blocks[0].text, "1 One");
|
||||
assert_eq!(blocks[1].text, "1.1 Two");
|
||||
assert_eq!(blocks[2].text, "Star");
|
||||
|
||||
Reference in new issue
Block a user