Lay minipage bodies out at their declared width

This commit is contained in:
gloamxun committed 2026-10-25 02:06:12 +08:00
1 parent 54efd62e40
commit bb26825e12
4 files changed
+88 -6

No files matched your search

+84 -2
View File
@@ -2075,6 +2075,8 @@ struct SourceParagraph {
list_label: i64,
/// Extra right margin in points, used by quotation blocks.
right_skip: i64,
/// Overrides the paragraph measure inside a `minipage` environment.
width: Option<i64>,
}
/// The kind of a LaTeX float environment.
@@ -2284,11 +2286,12 @@ fn paragraphs_with_geometry(
/// Blank-line splitter that keeps environments which may contain blank lines
/// (quotation, quote, verse, abstract) in one chunk.
fn split_display_chunks(source: &str) -> Vec<&str> {
const OPENINGS: [&str; 4] = [
const OPENINGS: [&str; 5] = [
"\\begin{quotation}",
"\\begin{quote}",
"\\begin{verse}",
"\\begin{abstract}",
"\\begin{minipage}",
];
fn depth(text: &str) -> i64 {
let mut depth = 0;
@@ -2341,6 +2344,7 @@ fn paragraphs_with_spacing(
for chunk in paragraphs {
if let Some(index) = table_reference(chunk) {
result.push(SourceParagraph {
width: None,
list_depth: None,
list_label: 0,
right_skip: 0,
@@ -2526,6 +2530,7 @@ fn float_block(raw: &str) -> Option<SourceParagraph> {
figure_graphic(inner).unwrap_or((None, None, None, None, false));
let table = embedded_table(inner);
Some(SourceParagraph {
width: None,
list_depth: None,
list_label: 0,
right_skip: 0,
@@ -2847,6 +2852,7 @@ fn theorem_block(raw: &str, definitions: &[TheoremDefinition]) -> Option<Vec<Sou
};
let theorem_title = if name == "proof" { None } else { title.clone() };
let heading = SourceParagraph {
width: None,
list_depth: None,
list_label: 0,
right_skip: 0,
@@ -2886,6 +2892,7 @@ fn theorem_block(raw: &str, definitions: &[TheoremDefinition]) -> Option<Vec<Sou
"theorem" | "lemma" | "corollary" | "proposition" | "proof"
);
let statement = SourceParagraph {
width: None,
list_depth: None,
list_label: 0,
right_skip: 0,
@@ -3186,6 +3193,7 @@ fn list_paragraph(
) -> SourceParagraph {
let (before, stretch, shrink) = space.unwrap_or((0, 0, 0));
SourceParagraph {
width: None,
text,
font: ParagraphFont::Roman,
line_height: 12,
@@ -3226,6 +3234,7 @@ fn verbatim_blocks(raw: &str) -> Option<Vec<SourceParagraph>> {
for (index, line) in inner.lines().enumerate() {
let text = line.trim_end();
blocks.push(SourceParagraph {
width: None,
list_depth: None,
list_label: 0,
right_skip: 0,
@@ -3361,6 +3370,7 @@ fn standalone_graphic(raw: &str) -> Option<SourceParagraph> {
}
let (width, height, image, scale, keep_aspectratio) = figure_graphic(trimmed)?;
Some(SourceParagraph {
width: None,
list_depth: None,
list_label: 0,
right_skip: 0,
@@ -3461,6 +3471,43 @@ fn rule_block(raw: &str) -> Option<SourceParagraph> {
Some(block)
}
fn minipage_blocks(
raw: &str,
definitions: &[TheoremDefinition],
spacing: ListSpacing,
) -> Option<Vec<SourceParagraph>> {
let body = raw
.trim()
.strip_prefix("\\begin{minipage}")?
.strip_suffix("\\end{minipage}")?;
let mut rest = body.trim_start();
if let Some(after) = rest.strip_prefix('[') {
let close = after.find(']')?;
rest = after[close + 1..].trim_start();
}
let (width, consumed) = braced_group(rest)?;
let width = parse_dimension(width.trim())?;
let content = rest[consumed..].trim();
if content.is_empty() {
return None;
}
let mut blocks = Vec::new();
for chunk in split_display_chunks(content) {
if chunk.trim().is_empty() {
continue;
}
let mut parsed = parse_blocks_raw(chunk, definitions, spacing);
for block in &mut parsed {
block.width = Some(width);
block.space_before = 0;
block.space_stretch = 0;
block.space_shrink = 0;
}
blocks.extend(parsed);
}
(!blocks.is_empty()).then_some(blocks)
}
fn parse_blocks_raw(
raw: &str,
definitions: &[TheoremDefinition],
@@ -3475,6 +3522,9 @@ fn parse_blocks_raw(
if let Some(block) = rule_block(raw) {
return vec![block];
}
if let Some(blocks) = minipage_blocks(raw, definitions, spacing) {
return blocks;
}
if let Some(blocks) = theorem_block(raw, definitions) {
return blocks;
}
@@ -3484,6 +3534,7 @@ fn parse_blocks_raw(
.and_then(|rest| rest.strip_suffix("\\end{abstract}"))
{
let heading = SourceParagraph {
width: None,
list_depth: None,
list_label: 0,
right_skip: 0,
@@ -3512,6 +3563,7 @@ fn parse_blocks_raw(
font_size: Some((12.0 * SCALE as f64) as i64),
};
let body = SourceParagraph {
width: None,
list_depth: None,
list_label: 0,
right_skip: 0,
@@ -3608,6 +3660,7 @@ fn parse_blocks_raw(
let text = body.trim();
if !text.is_empty() {
return vec![SourceParagraph {
width: None,
list_depth: None,
list_label: 0,
text: text.to_string(),
@@ -4386,6 +4439,7 @@ fn parse_paragraph(raw: &str) -> SourceParagraph {
})
.collect();
return SourceParagraph {
width: None,
list_depth: None,
list_label: 0,
right_skip: 0,
@@ -4417,6 +4471,7 @@ fn parse_paragraph(raw: &str) -> SourceParagraph {
}
if let Some((font, font_size, body)) = nfss_font_with_size(raw, ParagraphFont::Roman) {
return SourceParagraph {
width: None,
list_depth: None,
list_label: 0,
right_skip: 0,
@@ -4450,6 +4505,7 @@ fn parse_paragraph(raw: &str) -> SourceParagraph {
.and_then(|text| text.strip_suffix("\\end{equation}"))
{
return SourceParagraph {
width: None,
list_depth: None,
list_label: 0,
right_skip: 0,
@@ -4483,6 +4539,7 @@ fn parse_paragraph(raw: &str) -> SourceParagraph {
.and_then(|text| text.strip_suffix("\\end{equation*}"))
{
return SourceParagraph {
width: None,
list_depth: None,
list_label: 0,
right_skip: 0,
@@ -4520,6 +4577,7 @@ fn parse_paragraph(raw: &str) -> SourceParagraph {
})
{
return SourceParagraph {
width: None,
list_depth: None,
list_label: 0,
right_skip: 0,
@@ -4550,6 +4608,7 @@ fn parse_paragraph(raw: &str) -> SourceParagraph {
}
if let Some((level, starred, title, short)) = heading_command(raw) {
return SourceParagraph {
width: None,
list_depth: None,
list_label: 0,
right_skip: 0,
@@ -4600,6 +4659,7 @@ fn parse_paragraph(raw: &str) -> SourceParagraph {
for (command, font, line_height) in candidates {
if let Some(text) = command_body(raw, command) {
return SourceParagraph {
width: None,
list_depth: None,
list_label: 0,
right_skip: 0,
@@ -4630,6 +4690,7 @@ fn parse_paragraph(raw: &str) -> SourceParagraph {
}
}
SourceParagraph {
width: None,
list_depth: None,
list_label: 0,
right_skip: 0,
@@ -4812,6 +4873,7 @@ fn clean_title_value(text: &str) -> String {
fn title_block(text: String, size: f64, space_before: i64) -> SourceParagraph {
SourceParagraph {
width: None,
list_depth: None,
list_label: 0,
right_skip: 0,
@@ -5184,6 +5246,7 @@ fn number_floats(
/// Append `thebibliography` entries as numbered paragraphs.
fn bibliography_heading(text: &str) -> SourceParagraph {
SourceParagraph {
width: None,
list_depth: None,
list_label: 0,
right_skip: 0,
@@ -5224,6 +5287,7 @@ fn append_bibliography(
paragraphs.push(bibliography_heading(heading));
for (index, (_, entry)) in bibliography.iter().enumerate() {
paragraphs.push(SourceParagraph {
width: None,
list_depth: None,
list_label: 0,
right_skip: 0,
@@ -5290,6 +5354,7 @@ fn expand_toc(paragraphs: &mut Vec<SourceParagraph>, tocdepth: u32, contents_nam
toc.push(heading);
for (level, number, text) in entries.iter() {
toc.push(SourceParagraph {
width: None,
list_depth: None,
list_label: 0,
right_skip: 0,
@@ -5373,6 +5438,7 @@ fn expand_float_lists(
list.push(heading);
for (prefix, text) in entries.iter() {
list.push(SourceParagraph {
width: None,
list_depth: None,
list_label: 0,
right_skip: 0,
@@ -5665,7 +5731,7 @@ fn build_document(paragraphs: &[SourceParagraph], geometry: &PageGeometry) -> Do
),
};
let style = ParagraphStyle {
line_width,
line_width: paragraph.width.unwrap_or(line_width),
left_skip,
centered: paragraph.centered,
right_aligned: paragraph.right_aligned,
@@ -6639,6 +6705,22 @@ mod source_tests {
assert!(first.contains(">2</text>"), "got {first}");
}
#[test]
fn minipage_paragraphs_use_their_declared_width() {
let source = "\\begin{minipage}{120pt}\nAlpha beta.\n\nGamma delta.\n\\end{minipage}";
let blocks = paragraphs(source).expect("paragraphs");
assert_eq!(blocks.len(), 2);
assert_eq!(blocks[0].text, "Alpha beta.");
assert_eq!(blocks[1].text, "Gamma delta.");
assert_eq!(blocks[0].width, Some(120 * SCALE));
let document = build_document(&blocks, &PageGeometry::default());
let style = document.units[0]
.paragraph_style
.as_ref()
.expect("paragraph style");
assert_eq!(style.line_width, 120 * SCALE);
}
#[test]
fn addtocounter_offsets_the_printed_page_number() {
let offset = preview_json("\\setcounter{page}{5}\n\n\\addtocounter{page}{2}\n\nAlpha")