Number standalone captionof captions
This commit is contained in:
1 parent
7d05268983
commit
3a089c6175
3 files changed
+47
-2
No files matched your search
@@ -5084,6 +5084,38 @@ fn take_labels(text: &mut String) -> Vec<String> {
|
||||
labels
|
||||
}
|
||||
|
||||
/// Parse a standalone `\captionof{kind}{caption}` as a here-float caption.
|
||||
fn caption_of_block(raw: &str) -> Option<SourceParagraph> {
|
||||
let trimmed = raw.trim();
|
||||
let rest = trimmed.strip_prefix("\\captionof")?.trim_start();
|
||||
let rest = rest.strip_prefix('{')?;
|
||||
let kind_end = rest.find('}')?;
|
||||
let kind = rest[..kind_end].trim();
|
||||
if kind != "figure" && kind != "table" {
|
||||
return None;
|
||||
}
|
||||
let after = rest[kind_end + 1..].trim_start();
|
||||
let after = after.strip_prefix('{')?;
|
||||
let mut depth = 0_usize;
|
||||
let mut end = None;
|
||||
for (offset, character) in after.char_indices() {
|
||||
match character {
|
||||
'{' => depth += 1,
|
||||
'}' => {
|
||||
if depth == 0 {
|
||||
end = Some(offset);
|
||||
break;
|
||||
}
|
||||
depth -= 1;
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
let caption = &after[..end?];
|
||||
let synthetic = format!("\\begin{{{kind}}}[h]\n\\caption{{{caption}}}\n\\end{{{kind}}}");
|
||||
float_block(&synthetic)
|
||||
}
|
||||
|
||||
/// Parse a `figure` environment into a single float block.
|
||||
fn float_block(raw: &str) -> Option<SourceParagraph> {
|
||||
let trimmed = raw.trim();
|
||||
@@ -6132,6 +6164,9 @@ fn parse_blocks_raw(
|
||||
if let Some(block) = float_block(raw) {
|
||||
return vec![block];
|
||||
}
|
||||
if let Some(block) = caption_of_block(raw) {
|
||||
return vec![block];
|
||||
}
|
||||
if let Some(block) = standalone_graphic(raw) {
|
||||
return vec![block];
|
||||
}
|
||||
@@ -11936,6 +11971,16 @@ See \ref{fig:a} and \ref{tab:b}.";
|
||||
assert!(!json.contains("multirow"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn captionof_creates_a_numbered_caption() {
|
||||
let source = "\\captionof{figure}{Standalone}\\label{cap:x}\n\nSee Figure~\\ref{cap:x}.";
|
||||
let json = preview_json(source).expect("preview");
|
||||
assert!(json.contains("\"warnings\":[]"), "warnings: {json}");
|
||||
assert!(!json.contains("captionof"));
|
||||
assert!(!json.contains("??"), "unresolved reference: {json}");
|
||||
assert!(json.contains(">S</text>"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn array_column_modifiers_stay_out_of_the_text() {
|
||||
let source =
|
||||
|
||||
Reference in new issue
Block a user