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
+82 -28
+82 -28
View File
@@ -689,10 +689,44 @@ pub(crate) fn text_width(text: &str) -> i64 {
text.chars()
.map(|character| {
if character.is_whitespace() {
metrics.space()
} else {
metrics.width(character)
return metrics.space();
}
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()
}
@@ -4588,25 +4622,25 @@ fn table_cell_plain_text(text: &str) -> String {
}
}
match name.as_str() {
"mu" => out.push_str("u"),
"sim" => out.push_str("approx. "),
"times" => out.push('x'),
"pm" => out.push_str("+/-"),
"le" => out.push_str("<="),
"ge" => out.push_str(">="),
"approx" => out.push('~'),
"cdot" => out.push('*'),
"ldots" | "dots" => out.push_str("..."),
"alpha" => out.push_str("alpha"),
"beta" => out.push_str("beta"),
"gamma" => out.push_str("gamma"),
"delta" => out.push_str("delta"),
"theta" => out.push_str("theta"),
"lambda" => out.push_str("lambda"),
"pi" => out.push_str("pi"),
"sigma" => out.push_str("sigma"),
"phi" => out.push_str("phi"),
"omega" => out.push_str("omega"),
"mu" => out.push('µ'),
"sim" => out.push('~'),
"times" => out.push('×'),
"pm" => out.push('±'),
"le" => out.push('≤'),
"ge" => out.push('≥'),
"approx" => out.push('≈'),
"cdot" => out.push('·'),
"ldots" | "dots" => out.push('…'),
"alpha" => out.push('α'),
"beta" => out.push('β'),
"gamma" => out.push('γ'),
"delta" => out.push('δ'),
"theta" => out.push('θ'),
"lambda" => out.push('λ'),
"pi" => out.push('π'),
"sigma" => out.push('σ'),
"phi" => out.push('φ'),
"omega" => out.push('ω'),
_ => {}
}
continue;
@@ -4624,8 +4658,28 @@ fn table_cell_plain_text(text: &str) -> String {
} else if let Some(next) = chars.next() {
exponent.push(next);
}
out.push_str("**");
out.push_str(&exponent);
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);
}
continue;
}
if character == '{' || character == '}' {
@@ -8079,10 +8133,10 @@ mod tests {
#[test]
fn table_cells_keep_readable_math_fallbacks() {
assert_eq!(table_cell_plain_text("Size ($\\mu$m)"), "Size (um)");
assert_eq!(table_cell_plain_text("$\\sim$100"), "approx. 100");
assert_eq!(table_cell_plain_text("up to $10^6$"), "up to 10**6");
assert_eq!(table_cell_plain_text("$\\alpha + \\beta$"), "alpha + beta");
assert_eq!(table_cell_plain_text("Size ($\\mu$m)"), "Size (µm)");
assert_eq!(table_cell_plain_text("$\\sim$100"), "~100");
assert_eq!(table_cell_plain_text("up to $10^6$"), "up to 10⁶");
assert_eq!(table_cell_plain_text("$\\alpha + \\beta$"), "α + β");
}
#[test]