Skip to content

Set rules for default document format options - #8496

Merged
laurmaedje merged 26 commits into
mainfrom
format-options
Aug 31, 2026
Merged

laurmaedje merged 26 commits into
mainfrom
format-options

Conversation

@saecki

@saecki saecki commented Jun 12, 2026 •

Copy link
Copy Markdown
Member

Based on #8535, #8571, #8599

This allows specifying the default options for a specific output format, for example the PDF standard using

#set pdf(standard: ("ua-1", "a3-a"))

Options specified by the CLI, the web-app, or another external way will take precedence and override the defaults set by the document.

It's nice to be able to specify default options for single documents, but the main use case is bundle export, where currently there are limited configuration options.

Internal changes

  • This moves some format specific elements and functionality in the export crates: typst-html, typst-pdf, typst-svg, typst-render, and typst-bundle.
  • Formats need to be manually registered in the Library.
    • LibraryExt::default() has been renamed to LibraryExt::new()
    • LibraryExt::builder() as well as LibraryExt::new() require a list of formats to be passed in.
    • This should allow adding custom formats (but not targets) to Typst without changes to typst-library
    • All formats have an associated element which will be defined in the format module.

User facing changes

  • A new format module has been added
    • By default there are currently 5 format elements defined within it: html, pdf, svg, png, and bundle. Though bundle currently has no options.
  • The pdf and html modules have become (format) elements, which support set rules, but are still available in the global prelude.
  • Some CLI options have been changed to accommodate the override behavior
    • --no-pdf-tags has been deprecated, use --pdf-tagged instead, which takes an optional boolean value that has to be specified with an = due to parsing ambiguities. This allows the CLI to either explicitly disable or enable tagged PDF, which was is not possible with a single negated flag.
      • Alternative design: add both mutually exclusive --pdf-tagged and --no-pdf-tagged flags. Vim has a similar mechanism for boolean flags with no prefixes.
    • --pretty takes an optional boolean value (= required). This allows explicitly disabling pretty output.

TODO

@saecki
saecki force-pushed the feature-gated-bindings branch from f71c785 to f238dba Compare June 12, 2026 12:08
@saecki
saecki force-pushed the format-options branch 4 times, most recently from c02a52c to 0620724 Compare June 16, 2026 10:21
@saecki
saecki force-pushed the feature-gated-bindings branch 2 times, most recently from c9fbc15 to 5b2ef2d Compare June 19, 2026 12:51
@saecki
saecki force-pushed the format-options branch 7 times, most recently from dd0700c to dbf3f45 Compare June 25, 2026 14:52
@saecki
saecki force-pushed the feature-gated-bindings branch from 00049c9 to 10623db Compare June 25, 2026 15:38
@saecki
saecki force-pushed the feature-gated-bindings branch from 10623db to 440528d Compare June 25, 2026 16:13
@laurmaedje laurmaedje added styling About set and show rules or style properties model Related to model category, which is all about structure and semantics. waiting-on-review This PR is waiting to be reviewed. labels Jun 30, 2026
@saecki
saecki force-pushed the feature-gated-bindings branch from 440528d to c4a1149 Compare June 30, 2026 10:53
@saecki
saecki force-pushed the feature-gated-bindings branch from c4a1149 to 72c60f5 Compare July 1, 2026 09:59
@laurmaedje laurmaedje added waiting-on-author Pull request waits on author and removed waiting-on-review This PR is waiting to be reviewed. labels Jul 1, 2026
@saecki
saecki force-pushed the feature-gated-bindings branch 2 times, most recently from 7178a2d to b1b88f3 Compare July 2, 2026 21:55
@laurmaedje

laurmaedje commented Aug 31, 2026 •

Copy link
Copy Markdown
Member

I was wondering whether document(format: pdf, ..) instead of document(format: "pdf", ..) would make sense in bundle export... Not all formats are supported, but I suppose it could error if you pass format.bundle.

But it may also create false expectations that custom formats could be used there while in reality typst-bundle very much hardcodes support for the 4 well-known single-file formats.

@saecki

saecki commented Sep 1, 2026

Copy link
Copy Markdown
Member Author

But it may also create false expectations that custom formats could be used there while in reality typst-bundle very much hardcodes support for the 4 well-known single-file formats.

Yeah, I think I agree. If at some point the export mechanism has been generalized this would make sense.

And nice API for Warned::and_then :)

@Mc-Zen

Mc-Zen commented Sep 1, 2026

Copy link
Copy Markdown
Contributor

I'm sorry if this has been mentioned somewhere above but this thread is very long and difficult to browse.

Does this also add the capability of accessing these options, like context format.pdf.standard, for example to check whether ua-1 has been requested (either through a set rule or the CLI)?

@saecki

saecki commented Sep 1, 2026

Copy link
Copy Markdown
Member Author

Does this also add the capability of accessing these options, like context format.pdf.standard, for example to check whether ua-1 has been requested (either through a set rule or the CLI)?

Since format.pdf (and the others) are just regular elements, the set values are readable withing context just like any other set option. The additional trickery, like for document options, just allows retrieving the set options after the document has been compiled.
As for accessing options set externally, for example through the CLI, we've decided against that. The options here are the defaults for a document, and can be overridden. This means, exporting with different externally set options doesn't require recompiling a document and the document can stay cached.

@laurmaedje

laurmaedje commented Sep 1, 2026 •

Copy link
Copy Markdown
Member

I could sort of see making this work (not too concerned with caching) and it would be in line with the idea that floated around of replacing document.today() with context document.date.

However, there is at least one challenge: I the CLI styles are just added to the style chain, they are the outermost styles by default. So a set pdf would override them and context would observe that. For a consistent result, the export would thus have to respect the set pdf override as well.

However, this is counter-intuitive, too, since we want the set rules to represent defaults overridable via CLI, not final, untouchable values.

To make this work, we could probably give priority to CLI styles and just ignore the set rules. This would be another special case though.

@Mc-Zen

Mc-Zen commented Sep 1, 2026 •

Copy link
Copy Markdown
Contributor

I see.

I think it would be very beneficial to be able to determine whether a document is compiled for accessibility for two reasons:

  • user code and packages can generate expensive descriptions only in the case that accessibility is required
  • packages could provide error messages when translations to the current language is not supported but not throw any useless error messages for unsupported languages when accessibility is not required. With the current status, the only choices are to always error or to silently ignore and let Typst panic when it encounters an element without alt description but then there will be no hint to what is the actual reason.

@laurmaedje

Copy link
Copy Markdown
Member

I can definitely see situations where this would be useful, but I wanted to emphasize that the compiler always tries to produce accessible output. With UA-1, it just ensures that its potentially stricter requirements are met and writes additional metadata. Accessibility not being required due to a submission or institution guideline does not mean that there isn't a benefit for some consumers of the PDF in having better accessibility. In that same spirit, I am of the opinion that packages should generate descriptions whenever they can (it's hard for me to imagine a situation where that's prohibitively expensive unless you are embedding an LLM via WebAssembly). When the improved accessibility information cannot be auto-generated, it's a different story and that's were the context lookup would come in handy.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

model Related to model category, which is all about structure and semantics. styling About set and show rules or style properties

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants