Scale tabular rows with arraystretch

\renewcommand{\arraystretch}{factor} (or \def\arraystretch) multiplies
the tabular line height and recomputes the table height from the new row
heights, so classes that loosen table leading render like LaTeX.
This commit is contained in:
gloamxun committed 2026-10-25 00:17:44 +08:00
1 parent a923ec0970
commit fd6217b3c7
4 files changed
+53 -8

No files matched your search

+49 -4
View File
@@ -141,6 +141,8 @@ struct PageGeometry {
linespread: Option<i64>,
/// `\tabcolsep` on each side of a tabular column.
tab_col_sep: Option<i64>,
/// `\arraystretch` factor in thousandths.
array_stretch: Option<i64>,
left_skip: Option<i64>,
right_skip: Option<i64>,
tolerance: Option<i64>,
@@ -689,6 +691,15 @@ fn linespread_assignment(line: &str) -> Option<i64> {
spread_percent(argument)
}
/// `\renewcommand{\arraystretch}{factor}` in thousandths.
fn arraystretch_assignment(line: &str) -> Option<i64> {
let rest = line
.strip_prefix("\\renewcommand{\\arraystretch}")
.or_else(|| line.strip_prefix("\\def\\arraystretch"))?;
let (argument, _) = braced_group(rest.trim_start())?;
spread_percent(argument)
}
/// `\renewcommand{\baselinestretch}{factor}` in thousandths.
fn baselinestretch_assignment(line: &str) -> Option<i64> {
let rest = line
@@ -976,6 +987,10 @@ fn strip_page_geometry(source: &str) -> (String, PageGeometry) {
geometry.linespread = Some(spread);
continue;
}
if let Some(spread) = arraystretch_assignment(trimmed) {
geometry.array_stretch = Some(spread);
continue;
}
if trimmed.starts_with("\\newcommand")
|| trimmed.starts_with("\\renewcommand")
|| trimmed.starts_with("\\providecommand")
@@ -1200,11 +1215,20 @@ fn tabular_unit(geometry: &PageGeometry, id: u64, template: &str, source: &str)
Vec::new(),
);
let separator = geometry.tab_col_sep.unwrap_or(6 * SCALE);
if separator != 6 * SCALE {
let stretch = geometry.array_stretch.unwrap_or(1000);
if separator != 6 * SCALE || stretch != 1000 {
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;
if separator != 6 * SCALE {
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;
}
if stretch != 1000 {
halign.line_height = halign.line_height.saturating_mul(stretch) / 1000;
halign.height = (0..halign.rows.len())
.map(|index| halign.row_height(index) + halign.row_gap(index))
.sum();
}
}
}
unit
@@ -4939,6 +4963,27 @@ mod tests {
assert_eq!(unit.source, "Huge text");
}
#[test]
fn arraystretch_scales_tabular_rows() {
let source = "\\renewcommand{\\arraystretch}{2}\n\n\\begin{tabular}{ll}\nA & B \\\\\nC & D\n\\end{tabular}";
let (body, geometry) = geometry_and_body(source);
assert_eq!(geometry.array_stretch, Some(2000));
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.line_height, 24 * SCALE);
assert_eq!(halign.height, 48 * SCALE);
}
#[test]
fn tabcolsep_sets_tabular_column_glue() {
let source = "\\tabcolsep=20pt\n\n\\begin{tabular}{ll}\nA & B \\\\\nC & D\n\\end{tabular}";