Transpose valign into a table

\valign had no handler. Add a preprocessing pass that finds \valign{...},
splits the group at \cr, treats each remaining \cr-separated line as one column
(entries split on &), and emits an equivalent tabular with the matrix
transposed; the existing capture then lays it out. Column alignment defaults to
left because the \valign preamble describes vertical alignment here.

A test transposes \valign{\cr a & b & c \cr d & e & f \cr} into two columns and
three rows, checking (a,d) and (c,f).
This commit is contained in:
gloamxun committed 2026-10-27 01:23:20 +08:00
1 parent 05e1bd096b
commit 7a9e28d0f4
3 files changed
+90 -4

No files matched your search

+85
View File
@@ -5874,6 +5874,57 @@ fn is_internal_at_command(name: &str) -> bool {
.is_some_and(|rest| INTERNAL_AT_COMMANDS.contains(&rest))
}
/// Rewrite `\valign{...}` into a transposed `tabular`: each `\cr`-separated
/// input line is one column, and the output rows take one entry from each line.
fn expand_valign(source: &str) -> String {
let mut out = String::new();
let mut rest = source;
while let Some(position) = rest.find("\\valign") {
out.push_str(&rest[..position]);
let after = &rest[position + "\\valign".len()..];
let trimmed = after.trim_start();
let Some((content, used)) = braced_group(trimmed) else {
out.push_str(&rest[position..]);
return out;
};
out.push_str(&render_valign(content));
rest = &trimmed[used..];
}
out.push_str(rest);
out
}
fn render_valign(content: &str) -> String {
let body = match content.split_once("\\cr") {
Some((_preamble, body)) => body,
None => content,
};
let columns: Vec<Vec<String>> = body
.split("\\cr")
.map(|line| {
line.split('&')
.map(|cell| cell.trim().to_string())
.collect::<Vec<_>>()
})
.filter(|cells| cells.iter().any(|cell| !cell.is_empty()))
.collect();
if columns.is_empty() {
return String::new();
}
let height = columns.iter().map(Vec::len).max().unwrap_or(0);
let mut out = format!("\\begin{{tabular}}{{{}}}\n", "l".repeat(columns.len()));
for row in 0..height {
let cells: Vec<&str> = columns
.iter()
.map(|column| column.get(row).map(String::as_str).unwrap_or(""))
.collect();
out.push_str(&cells.join(" & "));
out.push_str(" \\\\\n");
}
out.push_str("\\end{tabular}");
out
}
/// Parse the `\catcode` target and value that follow the command, supporting
/// the backtick form (`\catcode`\@=11`), a bare character, or a code point.
fn catcode_assignment(characters: &[char], start: usize) -> Option<(char, i64, usize)> {
@@ -6158,6 +6209,7 @@ fn geometry_and_body_with_images(
// body before `\begin{document}` so its commands are consumed as setup
// instead of being typeset as text at the top of page one.
let source = lift_at_begin_document(source);
let source = expand_valign(&source);
let source = canonicalize_at_catcodes(&source);
let source = collapse_definition_bodies(&source);
let source = collapse_braced_command(&source, "\\newgeometry");
@@ -16081,6 +16133,39 @@ A definition.
assert!(!text.contains("makeatletter"), "{text}");
}
#[test]
fn valign_transposes_into_a_table() {
let source = "\\valign{\\cr a & b & c \\cr d & e & f \\cr}";
let (body, geometry) = geometry_and_body(source);
assert_eq!(geometry.tabular.len(), 1);
let blocks = paragraphs(&body).expect("paragraphs");
let document = build_document(&blocks, &geometry);
let halign = document
.units
.iter()
.find_map(|unit| {
unit.items.iter().find_map(|item| match item {
VerticalItem::Halign(halign) => Some(halign),
_ => None,
})
})
.expect("halign");
assert_eq!(halign.column_widths.len(), 2);
assert_eq!(halign.rows.len(), 3);
let row0: Vec<&str> = halign.rows[0]
.cells
.iter()
.map(|cell| cell.text.as_str())
.collect();
let row2: Vec<&str> = halign.rows[2]
.cells
.iter()
.map(|cell| cell.text.as_str())
.collect();
assert_eq!(row0, vec!["a", "d"]);
assert_eq!(row2, vec!["c", "f"]);
}
#[test]
fn percent_catcode_turns_comments_literal() {
let literal = "\\documentclass{article}\n\\catcode`\\%=12\n\\begin{document}\n\n100% sure\n\n\\end{document}\n";