Use short captions in float lists

This commit is contained in:
gloamxun committed 2026-10-25 01:07:14 +08:00
1 parent 0ea4bd65e8
commit 22c81cbb88
4 files changed
+57 -20

No files matched your search

+53 -16
View File
@@ -2022,6 +2022,10 @@ struct FloatSpec {
/// `keepaspectratio` when both width and height are requested.
keep_aspectratio: bool,
caption: Option<String>,
/// The optional `[short]` caption used by float lists.
caption_short: Option<String>,
/// Numbered float-list entry derived from the short caption.
list_caption: Option<String>,
caption_starred: bool,
/// `\includegraphics` path for a figure.
image: Option<String>,
@@ -2323,9 +2327,9 @@ fn float_block(raw: &str) -> Option<SourceParagraph> {
}
None => (FloatPlacement::Top, inner),
};
let (mut caption, caption_starred) = match figure_caption(inner) {
Some((text, starred)) => (Some(text), starred),
None => (None, false),
let (mut caption, caption_starred, caption_short) = match figure_caption(inner) {
Some((text, starred, short)) => (Some(text), starred, short),
None => (None, false, None),
};
let labels = figure_labels(inner);
if !labels.is_empty() && !caption_starred {
@@ -2369,6 +2373,8 @@ fn float_block(raw: &str) -> Option<SourceParagraph> {
scale,
keep_aspectratio,
caption,
caption_short,
list_caption: None,
caption_starred,
image,
table,
@@ -2396,22 +2402,25 @@ fn figure_placement(spec: &str) -> FloatPlacement {
FloatPlacement::Top
}
fn figure_caption(inner: &str) -> Option<(String, bool)> {
fn figure_caption(inner: &str) -> Option<(String, bool, Option<String>)> {
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('[') {
let (after, short) = match after.strip_prefix('[') {
Some(short) => match short.find(']') {
Some(end) => short[end + 1..].trim_start(),
None => after,
Some(end) => (
short[end + 1..].trim_start(),
Some(short[..end].trim().to_string()),
),
None => (after, None),
},
None => after,
None => (after, None),
};
let (body, _) = braced_group(after)?;
Some((body.trim().to_string(), starred))
Some((body.trim().to_string(), starred, short))
}
fn figure_labels(inner: &str) -> Vec<String> {
@@ -3079,6 +3088,8 @@ fn standalone_graphic(raw: &str) -> Option<SourceParagraph> {
scale,
keep_aspectratio,
caption: None,
caption_short: None,
list_caption: None,
caption_starred: false,
image,
table: None,
@@ -4722,11 +4733,11 @@ fn number_floats(
tables
}
};
*caption = format!(
"{} {number}: {}",
spec.kind.caption_prefix(figure_name, table_name),
caption.trim()
);
let prefix = spec.kind.caption_prefix(figure_name, table_name);
*caption = format!("{prefix} {number}: {}", caption.trim());
if let Some(short) = &spec.caption_short {
spec.list_caption = Some(format!("{prefix} {number}: {short}"));
}
for label in found {
labels.push((label, number.to_string()));
}
@@ -4872,14 +4883,18 @@ fn expand_float_lists(
let figures: Vec<String> = paragraphs
.iter()
.filter_map(|paragraph| match &paragraph.float {
Some(spec) if spec.kind == FloatKind::Figure => spec.caption.clone(),
Some(spec) if spec.kind == FloatKind::Figure => {
spec.list_caption.clone().or_else(|| spec.caption.clone())
}
_ => None,
})
.collect();
let tables: Vec<String> = paragraphs
.iter()
.filter_map(|paragraph| match &paragraph.float {
Some(spec) if spec.kind == FloatKind::Table => spec.caption.clone(),
Some(spec) if spec.kind == FloatKind::Table => {
spec.list_caption.clone().or_else(|| spec.caption.clone())
}
_ => None,
})
.collect();
@@ -7050,6 +7065,28 @@ A definition.
assert_eq!(paragraphs[0].equation_number, Some(3));
}
#[test]
fn short_captions_follow_into_float_lists() {
let source = "\\listoffigures\n\n\\begin{figure}\n\\caption[Short caption]{A long caption}\n\\end{figure}";
let (body, geometry) = geometry_and_body(source);
assert!(body.contains("figure"));
let blocks = paragraphs(&body).expect("paragraphs");
let document = build_document(&blocks, &geometry);
let texts: Vec<String> = document
.units
.iter()
.map(|unit| unit.source.clone())
.collect();
assert!(texts.iter().any(|text| text == "List of Figures"));
assert!(texts.iter().any(|text| text == "Figure 1: Short caption"));
let spec = blocks
.iter()
.find_map(|block| block.float.as_ref())
.expect("float");
assert_eq!(spec.caption.as_deref(), Some("A long caption"));
assert_eq!(spec.caption_short.as_deref(), Some("Short caption"));
}
#[test]
fn float_list_names_follow_renewcommand() {
let source = "\\renewcommand{\\listfigurename}{Abbildungen}\n\n\\renewcommand{\\listtablename}{Tabellen}\n\n\\listoffigures\n\n\\begin{figure}\n\\caption{Plot}\n\\end{figure}\n\n\\listoftables\n\n\\begin{table}\n\\begin{tabular}{l}\nA\n\\end{tabular}\n\\caption{Numbers}\n\\end{table}";