Lay out arXiv authors in two columns

Encode \And author groups into a marker paragraph and build a centred two-column
halign instead of ten sequential lines. Footnote groups are extracted before
the table and reattached to the paragraph; literal ~ is normalized because the
CMR table font cannot cover it. The arXiv title page now puts the abstract at
y=279pt (TeX: 281pt) instead of ~431pt.

The generic \and join path is unchanged; its thanks test now asserts the
extracted footnote instead of a literal \footnote string.
This commit is contained in:
gloamxun committed 2026-10-27 22:16:58 +08:00
1 parent 6b89ab1dd7
commit ca806341a8
3 files changed
+142 -14

No files matched your search

+2 -2
View File
@@ -41,8 +41,8 @@ execution is not a goal.
- Still visibly different from TeX:
- body face is CMR/fallback, not Times from `\rmdefault{ptm}`;
- title uses small caps; the arXiv class's top/bottom rules and
"A Preprint" undertitle are drawn, and author metadata is split onto
class-like lines. Two-column affiliations and ORCID icons are still
"A Preprint" undertitle are drawn, and two author groups are laid out
side by side as a centred two-column halign. ORCID icons are still
missing;
- reference style is numeric, not natbib author-year;
- display math is approximate;
+2 -2
View File
@@ -116,8 +116,8 @@ problems in the generated PDF, all fixed in the follow-up increment:
Remaining visible gaps in this template:
- the display equation is still a rough approximation of the printed formula;
- the title rules and A Preprint undertitle are now drawn, but the author
block is sequential rather than the class's two columns;
- the title rules, A Preprint undertitle and two-column author groups are now
drawn; ORCID icons are still missing;
- the `\fbox{\rule...}` figure body is not drawn (only its caption survives);
- `orcid.pdf` is a vector image and stays unsupported;
- the author block is concatenated and does not reproduce the class layout.
+138 -10
View File
@@ -12512,6 +12512,105 @@ fn author_lines(author: &str) -> Vec<(String, bool)> {
lines
}
const AUTHOR_BLOCK_MARKER: char = '\u{E050}';
const AUTHOR_CELL_MARKER: char = '\u{E051}';
const AUTHOR_ROW_MARKER: char = '\u{E052}';
/// Take `\footnote{...}` groups out of author metadata and leave numeric
/// markers in the text. A table cell cannot parse footnotes itself.
fn take_author_footnotes(text: &str) -> (String, Vec<(u32, String)>) {
let mut out = String::new();
let mut footnotes = Vec::new();
let mut rest = text;
while let Some(index) = rest.find("\\footnote") {
out.push_str(&rest[..index]);
let after = rest[index + "\\footnote".len()..].trim_start();
let Some(body) = after.strip_prefix('{') else {
out.push_str(&rest[index..]);
break;
};
let mut depth = 1usize;
let mut end = None;
for (offset, character) in body.char_indices() {
match character {
'{' => depth += 1,
'}' => {
depth -= 1;
if depth == 0 {
end = Some(offset);
break;
}
}
_ => {}
}
}
let Some(end) = end else {
out.push_str(&rest[index..]);
break;
};
let number = footnotes.len() as u32 + 1;
footnotes.push((number, body[..end].trim().to_string()));
out.push_str(&format!(" ({number})"));
rest = &body[end + 1..];
}
out.push_str(rest);
(out, footnotes)
}
fn encode_author_block(groups: &[Vec<String>]) -> String {
let rows = groups.iter().map(Vec::len).max().unwrap_or(0);
let mut text = String::from(AUTHOR_BLOCK_MARKER);
for row in 0..rows {
if row > 0 {
text.push(AUTHOR_ROW_MARKER);
}
for column in 0..groups.len() {
if column > 0 {
text.push(AUTHOR_CELL_MARKER);
}
if let Some(line) = groups[column].get(row) {
text.push_str(line);
}
}
}
text
}
fn decode_author_block(text: &str) -> Option<Vec<Vec<String>>> {
let rest = text.strip_prefix(AUTHOR_BLOCK_MARKER)?;
Some(
rest.split(AUTHOR_ROW_MARKER)
.map(|row| {
row.split(AUTHOR_CELL_MARKER)
.map(|cell| cell.trim().to_string())
.collect()
})
.collect(),
)
}
/// Lay out two author groups side by side as a centred two-column halign.
fn author_table_unit(index: usize, rows: &[Vec<String>], line_width: i64) -> Unit {
let mut source = String::new();
for row in rows {
for (column, cell) in row.iter().enumerate() {
if column > 0 {
source.push_str(" & ");
}
// A literal `~` is not covered by the CMR table font; use the
// same nonbreaking-space normalization as paragraph text.
source.push_str(&cell.replace('~', " "));
}
source.push_str(" \\\\ ");
}
let mut unit =
Unit::halign_from_source_with_template(index as u64, "c@{\\hspace{2em}}c", &source, 12);
if let Some(VerticalItem::Halign(halign)) = unit.items.first_mut() {
halign.margin = ((line_width - halign.width) / 2).max(0);
}
unit
}
fn expand_maketitle(blocks: &[SourceParagraph], geometry: &PageGeometry) -> Vec<SourceParagraph> {
if !blocks
.iter()
@@ -12540,7 +12639,7 @@ fn expand_maketitle(blocks: &[SourceParagraph], geometry: &PageGeometry) -> Vec<
let author_space = block.space_before
+ 12 * SCALE
+ if has_title_bars(geometry) {
18 * SCALE
43 * SCALE
} else {
0
};
@@ -12550,16 +12649,37 @@ fn expand_maketitle(blocks: &[SourceParagraph], geometry: &PageGeometry) -> Vec<
.filter(|author| !author.is_empty())
{
let author = expand_today(&thanks_to_footnotes(author), &today);
for (index, (line, starts_group)) in author_lines(&author).into_iter().enumerate() {
let mut paragraph = title_block(
line,
title_large_size(base),
if index == 0 { author_space } else { 0 },
);
if starts_group {
paragraph.space_before += 8 * SCALE;
let (author, footnotes) = take_author_footnotes(&author);
let lines = author_lines(&author);
let mut groups: Vec<Vec<String>> = Vec::new();
for (line, starts_group) in lines {
if starts_group || groups.is_empty() {
groups.push(Vec::new());
}
if let Some(group) = groups.last_mut() {
group.push(line);
}
}
if has_title_bars(geometry) && groups.len() > 1 {
let mut paragraph = title_block(encode_author_block(&groups), 10.0, author_space);
paragraph.line_height = 11;
paragraph.footnotes = footnotes;
result.push(paragraph);
} else {
for (index, (line, starts_group)) in author_lines(&author).into_iter().enumerate() {
let mut paragraph = title_block(
line,
title_large_size(base),
if index == 0 { author_space } else { 0 },
);
if starts_group {
paragraph.space_before += 8 * SCALE;
}
if index == 0 {
paragraph.footnotes = footnotes.clone();
}
result.push(paragraph);
}
}
}
let date = match geometry.date.as_deref() {
@@ -13500,6 +13620,13 @@ fn build_document_with_labels(
.iter()
.enumerate()
.map(|(index, paragraph)| {
if let Some(rows) = decode_author_block(&paragraph.text) {
let mut unit = author_table_unit(index, &rows, line_width);
unit.space_before = paragraph.space_before;
unit.space_stretch = paragraph.space_stretch;
unit.space_shrink = paragraph.space_shrink;
return unit;
}
if let Some(spec) = &paragraph.float {
let width = line_width;
let mut content = Vec::new();
@@ -21022,7 +21149,8 @@ left & right \\
let (body, geometry) = geometry_and_body(source);
let blocks = paragraphs(&body).expect("blocks");
let expanded = expand_maketitle(&blocks, &geometry);
assert_eq!(expanded[1].text, "A, B \\footnote{Support}");
assert_eq!(expanded[1].text, "A, B (1)");
assert_eq!(expanded[1].footnotes, vec![(1, "Support".to_string())]);
assert!(!expanded[1].text.contains("thanks"));
}