Apply tabcolsep to tabular columns

The captured tabular path passed \tabcolsep as scaled points to a
constructor that expects points, so the default gap was huge. Pass the
6pt default properly and let \tabcolsep (or \setlength{\tabcolsep})
override the per-boundary glue, keeping the table width in step.
This commit is contained in:
gloamxun committed 2026-10-25 00:16:09 +08:00
1 parent 1a74ece3ba
commit a923ec0970
4 files changed
+72 -23

No files matched your search

+68 -19
View File
@@ -139,6 +139,8 @@ struct PageGeometry {
baseline_skip: Option<i64>,
/// `\linespread` factor in thousandths.
linespread: Option<i64>,
/// `\tabcolsep` on each side of a tabular column.
tab_col_sep: Option<i64>,
left_skip: Option<i64>,
right_skip: Option<i64>,
tolerance: Option<i64>,
@@ -1089,6 +1091,7 @@ fn strip_page_geometry(source: &str) -> (String, PageGeometry) {
"maxdepth" => geometry.max_depth = Some(value),
"splittopskip" => geometry.split_top_skip = Some(value),
"splitmaxdepth" => geometry.split_max_depth = Some(value),
"tabcolsep" => geometry.tab_col_sep = Some(value),
_ => {}
}
} else if let Some(value) = dimension_assignment(trimmed, "parindent") {
@@ -1099,6 +1102,8 @@ fn strip_page_geometry(source: &str) -> (String, PageGeometry) {
geometry.par_skip_shrink = Some(shrink);
} else if let Some(value) = dimension_assignment(trimmed, "baselineskip") {
geometry.baseline_skip = Some(value);
} else if let Some(value) = dimension_assignment(trimmed, "tabcolsep") {
geometry.tab_col_sep = Some(value);
} else if let Some(value) = dimension_assignment(trimmed, "leftskip") {
geometry.left_skip = Some(value);
} else if let Some(value) = dimension_assignment(trimmed, "rightskip") {
@@ -1185,6 +1190,26 @@ fn braced(text: &str) -> Option<(&str, &str)> {
None
}
/// Build a captured `tabular` unit with the document's `\tabcolsep`.
fn tabular_unit(geometry: &PageGeometry, id: u64, template: &str, source: &str) -> Unit {
let mut unit = Unit::halign_from_source_with_template_and_tabskips(
id,
&engine_template(template),
source,
6,
Vec::new(),
);
let separator = geometry.tab_col_sep.unwrap_or(6 * SCALE);
if separator != 6 * SCALE {
if let Some(VerticalItem::Halign(halign)) = unit.items.first_mut() {
let gaps = halign.column_widths.len().saturating_sub(1);
halign.column_skips = vec![separator; gaps];
halign.width = halign.column_widths.iter().sum::<i64>() + separator * gaps as i64;
}
}
unit
}
fn engine_template(spec: &str) -> String {
let mut out = String::new();
let mut characters = spec.chars().peekable();
@@ -3042,11 +3067,6 @@ fn font_size_command(rest: &str) -> Option<(i64, &str)> {
Some((size, after))
}
#[cfg_attr(not(test), allow(dead_code))]
fn nfss_font(raw: &str, base: ParagraphFont) -> Option<(ParagraphFont, String)> {
nfss_font_with_size(raw, base).map(|(font, _, body)| (font, body))
}
fn nfss_font_with_size(
raw: &str,
base: ParagraphFont,
@@ -4594,13 +4614,7 @@ fn build_document(paragraphs: &[SourceParagraph], geometry: &PageGeometry) -> Do
}
if let Some(table) = spec.table {
if let Some((template, source)) = geometry.tabular.get(table) {
let table_unit = Unit::halign_from_source_with_template_and_tabskips(
index as u64,
&engine_template(template),
source,
6 * SCALE,
Vec::new(),
);
let table_unit = tabular_unit(&geometry, index as u64, template, source);
content.extend(table_unit.items);
}
}
@@ -4618,13 +4632,7 @@ fn build_document(paragraphs: &[SourceParagraph], geometry: &PageGeometry) -> Do
}
if let Some(table) = table_reference(&paragraph.text) {
if let Some((template, source)) = geometry.tabular.get(table) {
let mut unit = Unit::halign_from_source_with_template_and_tabskips(
index as u64,
&engine_template(template),
source,
6 * SCALE,
Vec::new(),
);
let mut unit = tabular_unit(&geometry, index as u64, template, source);
unit.space_before = paragraph.space_before;
unit.space_stretch = paragraph.space_stretch;
unit.space_shrink = paragraph.space_shrink;
@@ -4931,6 +4939,47 @@ mod tests {
assert_eq!(unit.source, "Huge text");
}
#[test]
fn tabcolsep_sets_tabular_column_glue() {
let source = "\\tabcolsep=20pt\n\n\\begin{tabular}{ll}\nA & B \\\\\nC & D\n\\end{tabular}";
let (body, geometry) = geometry_and_body(source);
assert_eq!(geometry.tab_col_sep, Some(20 * SCALE));
assert!(!body.contains("tabcolsep"));
let blocks =
paragraphs_with_definitions(&body, &geometry.theorem_definitions).expect("paragraphs");
let document = build_document(&blocks, &geometry);
let halign = document
.units
.iter()
.flat_map(|unit| &unit.items)
.find_map(|item| match item {
VerticalItem::Halign(halign) => Some(halign),
_ => None,
})
.expect("tabular halign");
assert_eq!(halign.column_skips, vec![20 * SCALE]);
let columns: i64 = halign.column_widths.iter().sum();
assert_eq!(halign.width, columns + 20 * SCALE);
let (_, default) = geometry_and_body("\\begin{tabular}{ll}\nA & B\n\\end{tabular}");
assert_eq!(default.tab_col_sep, None);
let blocks = paragraphs_with_definitions("\\texrratable{0}", &default.theorem_definitions)
.expect("paragraphs");
let document = build_document(&blocks, &default);
let halign = document
.units
.iter()
.flat_map(|unit| &unit.items)
.find_map(|item| match item {
VerticalItem::Halign(halign) => Some(halign),
_ => None,
})
.expect("tabular halign");
assert!(halign.column_skips.is_empty());
let columns: i64 = halign.column_widths.iter().sum();
assert_eq!(halign.width, columns + 6 * SCALE);
}
#[test]
fn linespread_scales_the_class_baseline() {
let source = "\\documentclass{article}\n\\linespread{1.5}\n\nBody text.";