Place the page body below geometry's top margin

The geometry package's top/bottom margins were only used to derive a text
height; the laid-out body still started at the very top of the sheet, so the
arXiv template's 1in top margin was ignored and text sat on the paper edge.

Record top/bottom margins on PageGeometry and add the top margin to the page
body's vertical offset, which both the SVG preview and the PDF writer honour;
the page number and footer baseline move with the body. An explicit textheight
still wins over the margin-derived one.

The arXiv template's first baseline now sits at 1in + 12pt, still with zero
warnings in two pages. Unit tests cover the margin parse/derive and a preview
whose first baseline shifts by exactly one inch.
This commit is contained in:
gloamxun committed 2026-10-26 23:25:20 +08:00
1 parent 21d48866de
commit 7a615a8639
3 files changed
+50 -4

No files matched your search

+42 -1
View File
@@ -148,6 +148,10 @@ struct PageGeometry {
paper_height: Option<i64>,
vsize: Option<i64>,
textheight: Option<i64>,
/// `geometry`'s `top`/`bottom`: the page body starts this far below the
/// sheet edge, and the footer margin stays clear below it.
top_margin: Option<i64>,
bottom_margin: Option<i64>,
/// `\textwidth` / `\hsize` / `\linewidth`, in scaled points.
line_width: Option<i64>,
par_indent: Option<i64>,
@@ -1552,6 +1556,8 @@ fn apply_geometry_package(geometry: &mut PageGeometry, options: &str) {
_ => {}
}
}
geometry.top_margin = top;
geometry.bottom_margin = bottom;
let explicit_width = geometry.line_width;
let explicit_height = geometry.textheight;
if left.is_some() || right.is_some() || top.is_some() || bottom.is_some() {
@@ -4895,7 +4901,9 @@ fn resolve_citations(paragraphs: &mut [SourceParagraph], bibliography: &[(String
fn apply_geometry(document: &mut Document, geometry: &PageGeometry) {
document.h_offset = geometry.origin.h_offset;
document.v_offset = geometry.origin.v_offset;
// geometry's `top` places the text block below the sheet edge; the
// display list and the PDF writer both honour the offset.
document.v_offset = geometry.origin.v_offset + geometry.top_margin.unwrap_or(0);
if let Some(width) = geometry.paper_width {
document.paper_width = width;
}
@@ -14083,6 +14091,39 @@ A definition.
);
}
#[test]
fn geometry_reads_vertical_margins() {
let source = "\\documentclass{article}\n\\usepackage[top=1in,bottom=1.5in,left=1in,right=1in]{geometry}\n\\begin{document}\n\nText.\n\n\\end{document}\n";
let (_body, geometry) = geometry_and_body(source);
assert_eq!(geometry.top_margin, parse_dimension("1in"));
assert_eq!(geometry.bottom_margin, parse_dimension("1.5in"));
assert_eq!(
geometry.textheight,
Some(792 * SCALE - parse_dimension("1in").unwrap() - parse_dimension("1.5in").unwrap())
);
}
#[test]
fn geometry_top_margin_offsets_the_page_body() {
let first_y = |json: &str| -> f64 {
let value: serde_json::Value = serde_json::from_str(json).expect("JSON");
let svg = value["pages"][0]["svg"].as_str().expect("svg");
let index = svg.find("y=\"").expect("text y");
let rest = &svg[index + 3..];
let end = rest.find('"').expect("y end");
rest[..end].parse().expect("numeric y")
};
let plain = preview_json("Hello").expect("plain preview");
let shifted =
preview_json("\\usepackage[top=1in]{geometry}\n\nHello").expect("shifted preview");
let delta = first_y(&shifted) - first_y(&plain);
let inch = parse_dimension("1in").unwrap() as f64 / SCALE as f64;
assert!(
(delta - inch).abs() < 0.2,
"top margin shifted text by {delta}pt, expected {inch}pt"
);
}
#[test]
fn lipsum_expands_to_filler_paragraphs() {
let source = "\\documentclass{article}\n\\begin{document}\n\n\\lipsum[1]\n\n\\lipsum[2-3]\n\n\\lipsum\n\n\\end{document}\n";