Span multicolumn cells in LaTeX tables
This commit is contained in:
1 parent
04446e673f
commit
78ffd054d4
5 files changed
+124
-19
No files matched your search
@@ -70,6 +70,43 @@ pub struct HalignRule {
|
||||
pub trim_right: i64,
|
||||
}
|
||||
|
||||
fn split_halign_cells(row: &str) -> Vec<&str> {
|
||||
let mut cells = Vec::new();
|
||||
let mut depth = 0usize;
|
||||
let mut start = 0usize;
|
||||
for (index, character) in row.char_indices() {
|
||||
match character {
|
||||
'{' => depth += 1,
|
||||
'}' => depth = depth.saturating_sub(1),
|
||||
'&' if depth == 0 => {
|
||||
cells.push(&row[start..index]);
|
||||
start = index + 1;
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
cells.push(&row[start..]);
|
||||
cells
|
||||
}
|
||||
|
||||
fn take_halign_group(text: &str) -> Option<(String, &str)> {
|
||||
let rest = text.strip_prefix('{')?;
|
||||
let mut depth = 1usize;
|
||||
for (index, character) in rest.char_indices() {
|
||||
match character {
|
||||
'{' => depth += 1,
|
||||
'}' => {
|
||||
depth = depth.saturating_sub(1);
|
||||
if depth == 0 {
|
||||
return Some((rest[..index].to_string(), &rest[index + 1..]));
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
/// A laid-out `\halign` table: natural column widths plus row rules.
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
|
||||
pub struct Halign {
|
||||
@@ -1076,32 +1113,54 @@ impl Unit {
|
||||
continue;
|
||||
}
|
||||
let mut cells = Vec::new();
|
||||
for raw_cell in raw_row.split('&') {
|
||||
for raw_cell in split_halign_cells(raw_row) {
|
||||
let mut text = raw_cell.trim().to_owned();
|
||||
let mut span = 1;
|
||||
if let Some(rest) = text.strip_prefix(r"\span") {
|
||||
span = 2;
|
||||
text = rest.trim().to_owned();
|
||||
}
|
||||
if let Some(rest) = text.strip_prefix(r"\multispan") {
|
||||
let mut alignment = None;
|
||||
if let Some(rest) = text.strip_prefix(r"\multicolumn") {
|
||||
let rest = rest.trim_start();
|
||||
if let Some(body) = rest.strip_prefix('{') {
|
||||
if let Some((count, tail)) = body.split_once('}') {
|
||||
span = count.trim().parse().unwrap_or(1).max(1);
|
||||
text = tail.trim().to_owned();
|
||||
if let Some((count, rest)) = take_halign_group(rest) {
|
||||
let rest = rest.trim_start();
|
||||
if let Some((spec, rest)) = take_halign_group(rest) {
|
||||
let rest = rest.trim_start();
|
||||
if let Some((body, rest)) = take_halign_group(rest) {
|
||||
span = count.trim().parse().unwrap_or(1).max(1);
|
||||
alignment = Some(if spec.contains('r') {
|
||||
CellAlignment::Right
|
||||
} else if spec.contains('c') {
|
||||
CellAlignment::Center
|
||||
} else {
|
||||
CellAlignment::Left
|
||||
});
|
||||
text = format!("{}{}", body.trim(), rest);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if let Some(rest) = text.strip_prefix(r"\omit") {
|
||||
text = rest.trim().to_owned();
|
||||
if alignment.is_none() {
|
||||
if let Some(rest) = text.strip_prefix(r"\span") {
|
||||
span = 2;
|
||||
text = rest.trim().to_owned();
|
||||
}
|
||||
if let Some(rest) = text.strip_prefix(r"\multispan") {
|
||||
let rest = rest.trim_start();
|
||||
if let Some(body) = rest.strip_prefix('{') {
|
||||
if let Some((count, tail)) = body.split_once('}') {
|
||||
span = count.trim().parse().unwrap_or(1).max(1);
|
||||
text = tail.trim().to_owned();
|
||||
}
|
||||
}
|
||||
}
|
||||
if let Some(rest) = text.strip_prefix(r"\omit") {
|
||||
text = rest.trim().to_owned();
|
||||
}
|
||||
}
|
||||
let column: usize = cells.iter().map(|cell: &HalignCell| cell.span.max(1)).sum();
|
||||
cells.push(HalignCell {
|
||||
text,
|
||||
span,
|
||||
alignment: alignments
|
||||
.get(column)
|
||||
.copied()
|
||||
alignment: alignment
|
||||
.or_else(|| alignments.get(column).copied())
|
||||
.unwrap_or(CellAlignment::Left),
|
||||
});
|
||||
}
|
||||
@@ -7365,6 +7424,30 @@ mod tests {
|
||||
assert_eq!(halign.rows[1].cells[0].span, 1);
|
||||
assert_eq!(halign.rows[1].cells[1].span, 2);
|
||||
}
|
||||
#[test]
|
||||
fn halign_parses_multicolumn_cells() {
|
||||
let unit =
|
||||
Unit::halign_from_source(1, r"\multicolumn{2}{c}{Merged} & Tail \\ A & B & C", 3, 10);
|
||||
let VerticalItem::Halign(halign) = &unit.items[0] else {
|
||||
panic!("expected halign");
|
||||
};
|
||||
assert_eq!(halign.rows[0].cells[0].text, "Merged");
|
||||
assert_eq!(halign.rows[0].cells[0].span, 2);
|
||||
assert_eq!(halign.rows[0].cells[0].alignment, CellAlignment::Center);
|
||||
assert_eq!(halign.rows[0].cells[1].text, "Tail");
|
||||
assert_eq!(halign.rows[1].cells.len(), 3);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn halign_keeps_ampersands_inside_groups() {
|
||||
let unit = Unit::halign_from_source(1, r"{A & B} & C", 2, 10);
|
||||
let VerticalItem::Halign(halign) = &unit.items[0] else {
|
||||
panic!("expected halign");
|
||||
};
|
||||
assert_eq!(halign.rows[0].cells[0].text, "{A & B}");
|
||||
assert_eq!(halign.rows[0].cells[1].text, "C");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn halign_computes_natural_column_widths() {
|
||||
let rows = vec![
|
||||
|
||||
Reference in new issue
Block a user