Expand counter formatting commands

\arabic, \roman, \Roman, \alph, \Alph and \value with a counter name were
unsupported and leaked into the text. The body pass now reads the braced counter
name and replaces the command with its stored value in the requested form,
using the same counter table that \setcounter/\stepcounter fill.

A preview test sets section to 4 and checks "4 iv D 4." with no warnings.
This commit is contained in:
gloamxun committed 2026-10-27 03:24:00 +08:00
1 parent 1d1009395b
commit 8a5411af1d
4 files changed
+70 -6

No files matched your search

+64
View File
@@ -6466,6 +6466,7 @@ fn geometry_and_body_with_images(
body = expand_today(&body, &today);
body = expand_lipsum(&body);
body = expand_par_breaks(&body);
body = expand_counter_formats(&body, &geometry);
// Class \maketitle definitions (arXiv, sn-jnl) issue \thispagestyle{empty}.
if geometry.fancy_pagestyle && geometry.title.is_some() {
geometry.suppress_first_page_header = true;
@@ -6536,6 +6537,48 @@ fn unwrap_scaled_tables(source: &str) -> String {
output
}
/// Expand `\arabic`/`\roman`/`\Roman`/`\alph`/`\Alph`/`\value` with a counter
/// name into its current formatted value.
fn expand_counter_formats(body: &str, geometry: &PageGeometry) -> String {
let mut output = String::new();
let mut rest = body;
loop {
let mut next: Option<(usize, &str)> = None;
for command in [
"\\arabic", "\\roman", "\\Roman", "\\alph", "\\Alph", "\\value",
] {
if let Some(index) = rest.find(command) {
let better = next.is_none_or(|(known, _)| index < known);
if better {
next = Some((index, command));
}
}
}
let Some((index, command)) = next else {
output.push_str(rest);
break;
};
output.push_str(&rest[..index]);
let after = rest[index + command.len()..].trim_start();
let Some((name, used)) = braced_group(after) else {
output.push_str(command);
rest = &rest[index + command.len()..];
continue;
};
let value = geometry_counter(geometry, name.trim()) as i64;
let text = match command {
"\\roman" => roman_numeral(value),
"\\Roman" => roman_numeral(value).to_uppercase(),
"\\alph" => alphabetic_page_number(value.max(0) as usize, false),
"\\Alph" => alphabetic_page_number(value.max(0) as usize, true),
_ => value.to_string(),
};
output.push_str(&text);
rest = &after[used..];
}
output
}
/// Adjust a document counter from `\stepcounter`/`\addtocounter`.
fn bump_geometry_counter(geometry: &mut PageGeometry, name: &str, delta: i64) {
if let Some(entry) = geometry
@@ -17149,6 +17192,27 @@ A definition.
assert!(text.contains("D"), "{text}");
}
#[test]
fn counter_forms_expand_in_text() {
let source = "\\documentclass{article}\n\\begin{document}\n\n\\setcounter{section}{4}\n\n\\arabic{section} \\roman{section} \\Alph{section} \\value{section}.\n\n\\end{document}\n";
let json = preview_json(source).expect("preview");
let value: serde_json::Value = serde_json::from_str(&json).expect("JSON");
assert_eq!(
value["warnings"].as_array().map(Vec::len),
Some(0),
"{}",
value["warnings"]
);
let svg = value["pages"][0]["svg"].as_str().expect("svg");
let text: String = svg
.split('>')
.filter_map(|piece| piece.split('<').next())
.collect::<String>()
.split_whitespace()
.collect();
assert!(text.contains("4ivD4."), "{text}");
}
#[test]
fn stepcounter_offsets_the_next_heading() {
let source = "\\stepcounter{section}\n\n\\section{Introduction}";