Expand newcolumntype letters in table specs

This commit is contained in:
gloamxun committed 2026-10-26 20:33:14 +08:00
1 parent 97e883f51d
commit 9d57ca604e
3 files changed
+76 -3

No files matched your search

+74 -1
View File
@@ -159,6 +159,8 @@ struct PageGeometry {
linespread: Option<i64>,
/// `\tabcolsep` on each side of a tabular column.
tab_col_sep: Option<i64>,
/// Custom column letters declared with `\newcolumntype`.
column_types: Vec<(char, String)>,
/// `\itemsep` between list items, as width/stretch/shrink.
item_sep: Option<(i64, i64, i64)>,
/// `\topsep` above a list, as width/stretch/shrink.
@@ -2094,6 +2096,35 @@ fn strip_page_geometry(source: &str) -> (String, PageGeometry) {
}
continue;
}
if let Some(rest) = trimmed.strip_prefix("\\newcolumntype") {
let rest = rest.trim_start();
let rest = if rest.starts_with('[') {
rest.find(']')
.map(|close| rest[close + 1..].trim_start())
.unwrap_or(rest)
} else {
rest
};
if let Some((name, used)) = braced_group(rest) {
let after = rest[used..].trim_start();
let after = if after.starts_with('[') {
after
.find(']')
.map(|close| after[close + 1..].trim_start())
.unwrap_or(after)
} else {
after
};
if let Some((spec, _)) = braced_group(after) {
if let Some(letter) = name.trim().chars().next() {
geometry
.column_types
.push((letter, spec.trim().to_string()));
}
}
}
continue;
}
if trimmed.starts_with("\\newsavebox")
|| trimmed.starts_with("\\sbox")
|| trimmed.starts_with("\\savebox")
@@ -2759,6 +2790,36 @@ fn expand_tabular_template(template: &str, source: &str, target: Option<i64>) ->
out
}
/// Substitute `\newcolumntype` letters at the top level of a column spec.
fn expand_column_types(template: &str, types: &[(char, String)]) -> String {
if types.is_empty() {
return template.to_string();
}
let mut out = String::new();
let mut depth = 0_usize;
for character in template.chars() {
match character {
'{' => {
depth += 1;
out.push(character);
}
'}' => {
depth = depth.saturating_sub(1);
out.push(character);
}
_ if depth == 0 => {
if let Some((_, spec)) = types.iter().find(|(letter, _)| *letter == character) {
out.push_str(spec);
} else {
out.push(character);
}
}
_ => out.push(character),
}
}
out
}
fn tabular_unit(
geometry: &PageGeometry,
id: u64,
@@ -2767,8 +2828,9 @@ fn tabular_unit(
target: Option<i64>,
) -> Unit {
let separator = geometry.tab_col_sep.unwrap_or(6 * SCALE);
let template = expand_column_types(template, &geometry.column_types);
let inner_target = target.map(|target| (target - 2 * 6 * SCALE).max(10 * 65_536));
let template = expand_tabular_template(template, source, inner_target);
let template = expand_tabular_template(&template, source, inner_target);
let mut unit = Unit::halign_from_source_with_template_and_tabskips(
id,
&engine_template(&template),
@@ -12090,6 +12152,17 @@ See \ref{fig:a} and \ref{tab:b}.";
assert!(json.contains(">S</text>"));
}
#[test]
fn newcolumntype_expands_in_templates() {
let source = "\\newcolumntype{L}{>{\\centering}p{20mm}}\n\n\\begin{tabular}{Ll}\nalpha beta & right \\\\\n\\end{tabular}";
let (body, geometry) = geometry_and_body(source);
assert_eq!(geometry.column_types.len(), 1);
assert!(!body.contains("newcolumntype"));
let json = preview_json(source).expect("preview");
assert!(json.contains("\"warnings\":[]"), "warnings: {json}");
assert!(!json.contains("centering"));
}
#[test]
fn array_column_modifiers_stay_out_of_the_text() {
let source =