Use Unicode math glyphs in table cells

Table conversion previously fell back to Size (um), approx. 100 and 10**6.
Emit µ, ~, ×, ±, Greek letters and Unicode superscripts instead, with ASCII
equivalents for width measurement when the CMR table lacks the glyph.

The arXiv table now reads Size (µm), ~100 and 10⁶ in both PDF and SVG; test
count stays 1076.
This commit is contained in:
gloamxun committed 2026-10-28 00:14:55 +08:00
1 parent fd9ee0740b
commit 37b956c0a6
1 file changed
+81 -27
+81 -27
View File
@@ -689,10 +689,44 @@ pub(crate) fn text_width(text: &str) -> i64 {
text.chars() text.chars()
.map(|character| { .map(|character| {
if character.is_whitespace() { if character.is_whitespace() {
metrics.space() return metrics.space();
} else {
metrics.width(character)
} }
let width = metrics.width(character);
if width > 0 {
return width;
}
let fallback = match character {
'µ' => 'u',
'×' => 'x',
'±' => '+',
'≤' => '<',
'≥' => '>',
'≈' => '~',
'·' => '.',
'…' => '.',
'⁰' => '0',
'¹' => '1',
'²' => '2',
'³' => '3',
'⁴' => '4',
'⁵' => '5',
'⁶' => '6',
'⁷' => '7',
'⁸' => '8',
'⁹' => '9',
'α' => 'a',
'β' => 'b',
'γ' => 'g',
'δ' => 'd',
'θ' => 't',
'λ' => 'l',
'π' => 'p',
'σ' => 's',
'φ' => 'f',
'ω' => 'w',
other => other,
};
metrics.width(fallback).max(metrics.width('x'))
}) })
.sum() .sum()
} }
@@ -4588,25 +4622,25 @@ fn table_cell_plain_text(text: &str) -> String {
} }
} }
match name.as_str() { match name.as_str() {
"mu" => out.push_str("u"), "mu" => out.push('µ'),
"sim" => out.push_str("approx. "), "sim" => out.push('~'),
"times" => out.push('x'), "times" => out.push('×'),
"pm" => out.push_str("+/-"), "pm" => out.push('±'),
"le" => out.push_str("<="), "le" => out.push('≤'),
"ge" => out.push_str(">="), "ge" => out.push('≥'),
"approx" => out.push('~'), "approx" => out.push('≈'),
"cdot" => out.push('*'), "cdot" => out.push('·'),
"ldots" | "dots" => out.push_str("..."), "ldots" | "dots" => out.push('…'),
"alpha" => out.push_str("alpha"), "alpha" => out.push('α'),
"beta" => out.push_str("beta"), "beta" => out.push('β'),
"gamma" => out.push_str("gamma"), "gamma" => out.push('γ'),
"delta" => out.push_str("delta"), "delta" => out.push('δ'),
"theta" => out.push_str("theta"), "theta" => out.push('θ'),
"lambda" => out.push_str("lambda"), "lambda" => out.push('λ'),
"pi" => out.push_str("pi"), "pi" => out.push('π'),
"sigma" => out.push_str("sigma"), "sigma" => out.push('σ'),
"phi" => out.push_str("phi"), "phi" => out.push('φ'),
"omega" => out.push_str("omega"), "omega" => out.push('ω'),
_ => {} _ => {}
} }
continue; continue;
@@ -4624,8 +4658,28 @@ fn table_cell_plain_text(text: &str) -> String {
} else if let Some(next) = chars.next() { } else if let Some(next) = chars.next() {
exponent.push(next); exponent.push(next);
} }
out.push_str("**"); let digits_only =
!exponent.is_empty() && exponent.chars().all(|digit| digit.is_ascii_digit());
if digits_only {
for digit in exponent.chars() {
out.push(match digit {
'0' => '⁰',
'1' => '¹',
'2' => '²',
'3' => '³',
'4' => '⁴',
'5' => '⁵',
'6' => '⁶',
'7' => '⁷',
'8' => '⁸',
'9' => '⁹',
_ => digit,
});
}
} else {
out.push('^');
out.push_str(&exponent); out.push_str(&exponent);
}
continue; continue;
} }
if character == '{' || character == '}' { if character == '{' || character == '}' {
@@ -8079,10 +8133,10 @@ mod tests {
#[test] #[test]
fn table_cells_keep_readable_math_fallbacks() { fn table_cells_keep_readable_math_fallbacks() {
assert_eq!(table_cell_plain_text("Size ($\\mu$m)"), "Size (um)"); assert_eq!(table_cell_plain_text("Size ($\\mu$m)"), "Size (µm)");
assert_eq!(table_cell_plain_text("$\\sim$100"), "approx. 100"); assert_eq!(table_cell_plain_text("$\\sim$100"), "~100");
assert_eq!(table_cell_plain_text("up to $10^6$"), "up to 10**6"); assert_eq!(table_cell_plain_text("up to $10^6$"), "up to 10⁶");
assert_eq!(table_cell_plain_text("$\\alpha + \\beta$"), "alpha + beta"); assert_eq!(table_cell_plain_text("$\\alpha + \\beta$"), "α + β");
} }
#[test] #[test]