Place table captions above their tabular and footnote thanks notes
This commit is contained in:
1 parent
ac1bf660af
commit
aae046a2f0
4 files changed
+168
-31
No files matched your search
+164
-27
@@ -1042,6 +1042,7 @@ struct FloatSpec {
|
||||
width: Option<i64>,
|
||||
height: Option<i64>,
|
||||
caption: Option<String>,
|
||||
caption_starred: bool,
|
||||
/// Index into `PageGeometry::tabular` for a captured table body.
|
||||
table: Option<usize>,
|
||||
}
|
||||
@@ -1324,9 +1325,12 @@ fn float_block(raw: &str) -> Option<SourceParagraph> {
|
||||
}
|
||||
None => (FloatPlacement::Top, inner),
|
||||
};
|
||||
let mut caption = figure_caption(inner);
|
||||
let (mut caption, caption_starred) = match figure_caption(inner) {
|
||||
Some((text, starred)) => (Some(text), starred),
|
||||
None => (None, false),
|
||||
};
|
||||
let labels = figure_labels(inner);
|
||||
if !labels.is_empty() {
|
||||
if !labels.is_empty() && !caption_starred {
|
||||
let mut text = caption.unwrap_or_default();
|
||||
for label in labels {
|
||||
let _ = write!(text, "\\label{{{label}}}");
|
||||
@@ -1363,6 +1367,7 @@ fn float_block(raw: &str) -> Option<SourceParagraph> {
|
||||
width,
|
||||
height,
|
||||
caption,
|
||||
caption_starred,
|
||||
table,
|
||||
}),
|
||||
})
|
||||
@@ -1388,9 +1393,13 @@ fn figure_placement(spec: &str) -> FloatPlacement {
|
||||
FloatPlacement::Top
|
||||
}
|
||||
|
||||
fn figure_caption(inner: &str) -> Option<String> {
|
||||
fn figure_caption(inner: &str) -> Option<(String, bool)> {
|
||||
let index = inner.find("\\caption")?;
|
||||
let after = inner[index + "\\caption".len()..].trim_start();
|
||||
let (after, starred) = match after.strip_prefix('*') {
|
||||
Some(rest) => (rest.trim_start(), true),
|
||||
None => (after, false),
|
||||
};
|
||||
let after = match after.strip_prefix('[') {
|
||||
Some(short) => match short.find(']') {
|
||||
Some(end) => short[end + 1..].trim_start(),
|
||||
@@ -1399,7 +1408,7 @@ fn figure_caption(inner: &str) -> Option<String> {
|
||||
None => after,
|
||||
};
|
||||
let (body, _) = braced_group(after)?;
|
||||
Some(body.trim().to_string())
|
||||
Some((body.trim().to_string(), starred))
|
||||
}
|
||||
|
||||
fn figure_labels(inner: &str) -> Vec<String> {
|
||||
@@ -2726,6 +2735,32 @@ fn title_large_size(base_font_size: i64) -> f64 {
|
||||
}
|
||||
|
||||
/// Remove `\thanks{...}` groups and join multiple authors set with `\and`.
|
||||
/// Rewrite `\thanks{...}` notes as footnotes so they become page insertions
|
||||
/// instead of disappearing with the title block.
|
||||
fn thanks_to_footnotes(text: &str) -> String {
|
||||
let mut out = String::new();
|
||||
let mut rest = text;
|
||||
loop {
|
||||
let Some(index) = rest.find("\\thanks") else {
|
||||
out.push_str(rest);
|
||||
break;
|
||||
};
|
||||
out.push_str(&rest[..index]);
|
||||
let after = &rest[index + "\\thanks".len()..];
|
||||
let trimmed = after.trim_start();
|
||||
let Some((inner, consumed)) = braced_group(trimmed) else {
|
||||
out.push_str("\\thanks");
|
||||
rest = after;
|
||||
continue;
|
||||
};
|
||||
out.push_str("\\footnote{");
|
||||
out.push_str(&inner);
|
||||
out.push('}');
|
||||
rest = &trimmed[consumed..];
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
fn clean_title_value(text: &str) -> String {
|
||||
let mut out = String::new();
|
||||
let mut rest = text;
|
||||
@@ -2814,7 +2849,7 @@ fn expand_maketitle(blocks: &[SourceParagraph], geometry: &PageGeometry) -> Vec<
|
||||
}
|
||||
if let Some(title) = geometry.title.as_deref().filter(|title| !title.is_empty()) {
|
||||
let mut paragraph = title_block(
|
||||
clean_title_value(title),
|
||||
clean_title_value(&thanks_to_footnotes(title)),
|
||||
title_size(base),
|
||||
block.space_before,
|
||||
);
|
||||
@@ -2828,14 +2863,14 @@ fn expand_maketitle(blocks: &[SourceParagraph], geometry: &PageGeometry) -> Vec<
|
||||
.filter(|author| !author.is_empty())
|
||||
{
|
||||
result.push(title_block(
|
||||
clean_title_value(author),
|
||||
clean_title_value(&thanks_to_footnotes(author)),
|
||||
title_large_size(base),
|
||||
author_space,
|
||||
));
|
||||
}
|
||||
if let Some(date) = geometry.date.as_deref().filter(|date| !date.is_empty()) {
|
||||
result.push(title_block(
|
||||
clean_title_value(date),
|
||||
clean_title_value(&thanks_to_footnotes(date)),
|
||||
title_large_size(base),
|
||||
12 * SCALE,
|
||||
));
|
||||
@@ -3061,6 +3096,11 @@ fn number_floats(paragraphs: &mut [SourceParagraph]) -> Vec<(String, String)> {
|
||||
let Some(caption) = spec.caption.as_mut() else {
|
||||
continue;
|
||||
};
|
||||
let found = take_labels(caption);
|
||||
if spec.caption_starred {
|
||||
*caption = caption.trim().to_string();
|
||||
continue;
|
||||
}
|
||||
let number = match spec.kind {
|
||||
FloatKind::Figure => {
|
||||
figures += 1;
|
||||
@@ -3071,7 +3111,6 @@ fn number_floats(paragraphs: &mut [SourceParagraph]) -> Vec<(String, String)> {
|
||||
tables
|
||||
}
|
||||
};
|
||||
let found = take_labels(caption);
|
||||
*caption = format!(
|
||||
"{} {number}: {}",
|
||||
spec.kind.caption_prefix(),
|
||||
@@ -3135,6 +3174,28 @@ fn build_document(paragraphs: &[SourceParagraph], geometry: &PageGeometry) -> Do
|
||||
if let Some(spec) = ¶graph.float {
|
||||
let width = line_width;
|
||||
let mut content = Vec::new();
|
||||
let caption_unit = spec.caption.as_ref().map(|caption| {
|
||||
let style = ParagraphStyle {
|
||||
line_width: width,
|
||||
centered: true,
|
||||
par_indent: 0,
|
||||
font_size: 9 * SCALE,
|
||||
baselineskip: 11 * SCALE,
|
||||
line_height: 11 * SCALE,
|
||||
..ParagraphStyle::default()
|
||||
};
|
||||
Unit::paragraph_with_style_and_face(
|
||||
index as u64,
|
||||
caption.clone(),
|
||||
style,
|
||||
ParagraphFont::Roman,
|
||||
)
|
||||
});
|
||||
if spec.kind == FloatKind::Table {
|
||||
if let Some(caption) = &caption_unit {
|
||||
content.extend(caption.items.clone());
|
||||
}
|
||||
}
|
||||
if spec.height.is_some() || spec.width.is_some() {
|
||||
let rule_width = spec.width.unwrap_or(width).min(width);
|
||||
let rule_height = spec.height.unwrap_or(rule_width / 2);
|
||||
@@ -3155,23 +3216,10 @@ fn build_document(paragraphs: &[SourceParagraph], geometry: &PageGeometry) -> Do
|
||||
content.extend(table_unit.items);
|
||||
}
|
||||
}
|
||||
if let Some(caption) = &spec.caption {
|
||||
let style = ParagraphStyle {
|
||||
line_width: width,
|
||||
centered: true,
|
||||
par_indent: 0,
|
||||
font_size: 9 * SCALE,
|
||||
baselineskip: 11 * SCALE,
|
||||
line_height: 11 * SCALE,
|
||||
..ParagraphStyle::default()
|
||||
};
|
||||
let caption_unit = Unit::paragraph_with_style_and_face(
|
||||
index as u64,
|
||||
caption.clone(),
|
||||
style,
|
||||
ParagraphFont::Roman,
|
||||
);
|
||||
content.extend(caption_unit.items);
|
||||
if spec.kind == FloatKind::Figure {
|
||||
if let Some(caption) = &caption_unit {
|
||||
content.extend(caption.items.clone());
|
||||
}
|
||||
}
|
||||
let mut unit =
|
||||
Unit::float_with_content(index as u64, spec.placement, width, 0, content);
|
||||
@@ -3684,6 +3732,50 @@ mod source_tests {
|
||||
assert_eq!(*height, 100 * SCALE);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn table_captions_sit_above_their_tabular() {
|
||||
let source = "\\begin{table}[t]\n\\caption{Numbers}\n\\begin{tabular}{ll}\na & b \\\\\n\\end{tabular}\n\\end{table}";
|
||||
let (body, geometry) = geometry_and_body(source);
|
||||
let blocks = paragraphs(&body).expect("blocks");
|
||||
assert_eq!(blocks.len(), 1);
|
||||
let document = build_document(&blocks, &geometry);
|
||||
let VerticalItem::Float(float) = &document.units[0].items[0] else {
|
||||
panic!("expected a float");
|
||||
};
|
||||
let caption = match &float.content[0] {
|
||||
VerticalItem::Paragraph(paragraph) => paragraph,
|
||||
other => panic!("expected the caption first, got {other:?}"),
|
||||
};
|
||||
let text: String = caption
|
||||
.lines
|
||||
.iter()
|
||||
.flatten()
|
||||
.map(|item| match item {
|
||||
texrra_tex_core::paragraph::HorizontalItem::Box(character) => {
|
||||
character.character.unwrap_or(' ')
|
||||
}
|
||||
texrra_tex_core::paragraph::HorizontalItem::Glue { width, .. } if *width > 0 => ' ',
|
||||
_ => '\0',
|
||||
})
|
||||
.filter(|character| *character != '\0')
|
||||
.collect();
|
||||
assert_eq!(text, "Table 1: Numbers");
|
||||
assert!(matches!(float.content[1], VerticalItem::Halign(_)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn starred_captions_stay_unnumbered() {
|
||||
let source = "\\begin{figure}[t]\n\\caption*{Note}\n\\end{figure}\n\n\\begin{figure}[t]\n\\caption{Numbered}\n\\end{figure}";
|
||||
let (body, _) = geometry_and_body(source);
|
||||
let mut blocks = paragraphs(&body).expect("blocks");
|
||||
assert_eq!(blocks.len(), 2);
|
||||
number_floats(&mut blocks);
|
||||
let first = blocks[0].float.as_ref().expect("first float");
|
||||
assert_eq!(first.caption.as_deref(), Some("Note"));
|
||||
let second = blocks[1].float.as_ref().expect("second float");
|
||||
assert_eq!(second.caption.as_deref(), Some("Figure 1: Numbered"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn table_floats_carry_their_tabular() {
|
||||
let source = r"\begin{table}[t]
|
||||
@@ -4301,15 +4393,60 @@ A definition.
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn maketitle_joins_authors_and_drops_thanks() {
|
||||
fn maketitle_joins_authors_and_turns_thanks_into_footnotes() {
|
||||
let source = "\\title{Paper}\n\\author{A \\and B \\thanks{Support}}\n\n\\maketitle\n\nBody";
|
||||
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");
|
||||
assert_eq!(expanded[1].text, "A, B \\footnote{Support}");
|
||||
assert!(!expanded[1].text.contains("thanks"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn title_thanks_becomes_a_page_footnote() {
|
||||
let source = "\\title{Paper}\n\\author{Ada\\thanks{Funded}}\n\n\\maketitle\n\nBody";
|
||||
let (body, geometry) = geometry_and_body(source);
|
||||
let blocks = paragraphs(&body).expect("blocks");
|
||||
let document = build_document(&blocks, &geometry);
|
||||
let author = document
|
||||
.units
|
||||
.iter()
|
||||
.find(|unit| unit.source.starts_with("Ada"))
|
||||
.expect("author unit");
|
||||
assert_eq!(author.source, "Ada (1)");
|
||||
let insertion = document
|
||||
.units
|
||||
.iter()
|
||||
.find_map(|unit| {
|
||||
unit.items.iter().find_map(|item| match item {
|
||||
VerticalItem::Insertion(insertion) => Some(insertion),
|
||||
_ => None,
|
||||
})
|
||||
})
|
||||
.expect("footnote insertion");
|
||||
let text: String = insertion
|
||||
.items
|
||||
.iter()
|
||||
.find_map(|item| match item {
|
||||
VerticalItem::Paragraph(paragraph) => Some(
|
||||
paragraph
|
||||
.lines
|
||||
.iter()
|
||||
.flatten()
|
||||
.filter_map(|item| match item {
|
||||
texrra_tex_core::paragraph::HorizontalItem::Box(character) => {
|
||||
character.character
|
||||
}
|
||||
_ => None,
|
||||
})
|
||||
.collect::<String>(),
|
||||
),
|
||||
_ => None,
|
||||
})
|
||||
.expect("footnote paragraph");
|
||||
assert_eq!(text, "Funded");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn maketitle_paints_its_title_block() {
|
||||
let source = "\\title{Paper}\n\\author{Ada}\n\n\\maketitle\n\nBody";
|
||||
|
||||
Reference in new issue
Block a user