Skip to content

feat(recipe): add --paper-size and --margin flags for LaTeX/Typst output - #382

Merged
dubadub merged 1 commit into
mainfrom
feat/latex-typst-paper-size-margin
Aug 3, 2026
Merged

dubadub merged 1 commit into
mainfrom
feat/latex-typst-paper-size-margin

Conversation

@dubadub

@dubadub dubadub commented Aug 3, 2026

Copy link
Copy Markdown
Member

Summary

  • Adds -p, --paper-size <a4|letter|a5|legal> and -m, --margin <CM> flags to cook recipe read
  • Applied to both LaTeX and Typst output, which previously hardcoded a4 paper and 2.5cm margins with no way to override them
  • Passing these flags with any other output format prints a warning to stderr and is otherwise a no-op

Fixes #379

Test plan

  • cargo fmt
  • cargo clippy --all-targets -- -D warnings
  • cargo test (full suite, including new tests for default/custom paper size/margin on both formats, and the unrelated-format warning)

Both formats hardcoded a4 paper and 2.5cm margins with no way to
override them, so PDFs couldn't be tuned for different paper sizes
or print layouts without hand-editing the generated LaTeX/Typst.

Fixes #379
@claude

claude Bot commented Aug 3, 2026

Copy link
Copy Markdown

Review: --paper-size / --margin flags for LaTeX/Typst output

Nice, focused change that closes #379 cleanly — the PaperSize enum, the format-mismatch warnings, and the test coverage for the happy paths are all well done and match the project's existing patterns (e.g. the OutputFormat enum right above it).

Correctness

No validation on --margin (src/recipe/read.rs:87-88, 194-195)
margin: Option<f64> is parsed straight from the CLI with no range/finiteness check, then interpolated directly into the LaTeX (cooklang_to_latex.rs:65) and Typst (cooklang_to_typst.rs:52) headers via {margin}. A few inputs slip through and produce broken documents with no warning:

  • --margin=-5 → \geometry{left=-5cm,...} / margin: (left: -5cm, ...) — negative margins are nonsensical here.
  • --margin=nan or --margin=inf → Rust's f64::from_str happily parses "nan"/"inf"/"infinity", so you'd get left=NaNcm or left=infcm embedded in the generated document, which will fail when the user actually compiles it with pdflatex/typst.
  • An absurdly large margin (e.g. bigger than the page itself) is also accepted silently.

Since clap 4.5 is already a dependency, a custom value_parser (e.g. value_parser = parse_margin where parse_margin rejects non-finite/negative values) would catch this at argument-parsing time with a clear error, rather than letting the CLI succeed and produce a document that only fails later in a different tool. Might be worth adding a test for this rejection alongside the existing default/custom-value tests.

Minor / nit

  • tests/output_formats_test.rs:190 only exercises --paper-size for the "warns on unrelated format" case; a similar assertion for --margin triggering "warning: --margin has no effect..." would round out that test (the code paths are independent if blocks, so it's not strictly redundant).
  • Not a blocker, but note that --margin -3 (space-separated negative value) will be rejected by clap as an unrecognized flag rather than parsed as -3.0 — users will need --margin=-3. Combined with the point above, it may be simplest to just disallow negative margins outright via the parser rather than document the = workaround.

Everything else looks solid

  • Short flags -p/-m don't collide with any existing flags in ReadArgs/RecipeInputArgs (-s scale, -o output, -f format).
  • The warning-then-continue behavior for unrelated formats matches the PR description ("no-op" rather than a hard error), and the ordering (format resolved before the warning check) is correct.
  • PaperSize::latex_name()/typst_name() mappings look right (legalpaper/us-legal, a5paper/a5, etc.).
  • Docs update in docs/recipe.md is clear and includes a usage example.
  • No security concerns — paper_size is constrained to a closed enum and margin is a numeric type, so there's no injection risk into the generated LaTeX/Typst source.

Given the only real issue is missing input validation on margin, this should be a quick fix.

@dubadub
dubadub merged commit b78fbd5 into main Aug 3, 2026
6 checks passed
@dubadub
dubadub deleted the feat/latex-typst-paper-size-margin branch August 3, 2026 12:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Unable to set paper size and margins

1 participant