A New Typst Template for Pandoc
Recorded: May 28, 2026, 12:03 a.m.
| Original | Summarized |
A New Typst Template for Pandoc Imaginary Text Posts About Reading Archives Weather RSS A New Typst Template for Pandoc JMax • Last summer I spent a lot of time with Typst (at that point v0.11) and Pandoc, working on a flexible and reusable workflow to typeset markdown-formatted articles to PDF. I designed a Pandoc template for Typst that would do nice things with layout and typography, and I posted about that here. Note that this looks kinda similar asking Pandoc to use one of its templates, but note that this is actually -V passing a variable to Pandoc… Pandoc’s own template.typst watches for this to be set, and then imports the named template. That’s a little syntactically confusing (I was confused), but it does separate out Pandoc’s logic from Typst’s logic. As it should be. This ought to be a little more robust long-term, too. The Typst template I’ve produced assumes you’re running it this way, and it gathers all the metadata Pandoc can recognize from your markdown source. I’m assuming you have a metadata block at the top of your markdown source with things like title, author, date, and so on. Download article.typ The Template You could follow suit and style whatever labels you want to put in your content. |
JMax developed a new workflow for typesetting markdown-formatted articles to PDF by designing a Pandoc template for Typst. This approach was necessitated by changes in Typst and Pandoc versions, as updates caused the original workflow to fail because Pandoc’s built-in Typst output template changed its assumptions and Typst introduced major logical shifts since version 0.12. To rebuild the workflow, JMax created new templates that leverage Pandoc’s default Typst output template while incorporating a custom Typst Template. This is achieved by instructing Pandoc to execute the process using the command structure file.md -o file.pdf -V template=article.typ --pdf-engine=typst, allowing Pandoc's template logic to import the specified Typst template. This separation allows Pandoc's logic to be distinct from Typst's logic, aiming for greater long-term robustness. The custom Typst template is designed to gather metadata from the source markdown file and establish document formatting defaults, including specifications for page size, font choices, and numbering options. The template implements several layers of customization. Initially, it sets up content-to-string mappings, drawing from Pandoc’s minimal Typst template to format multi-part metadata, such as authors’ names and emails, for Typst ingestion. Subsequent sections define configuration settings for the template, establishing base parameters like page format and font details. A significant customization involves routines for running heads and footers, addressing the distinct requirements for left and right facing pages in book layouts. The template handles alignment options, allowing for left and right alignment, or centered alignment, based on whether the page number is odd or even, and offers the flexibility to define different content for each page. Further styling is applied to basic text properties, utilizing Typst’s support for OpenType font options to control features like ligatures. The template also defines specific styling parameters for various content elements, including block quotations, code blocks, images, captions, and ad footnotes, providing aesthetically pleasing enhancements over the defaults. Heading structures are defined across three distinct levels, allowing for flexible and mixable stylistic variations. JMax further implements specific mechanisms for labeling content, enabling the creation of distinct sections like epigraphs and references using a syntax similar to HTML IDs, such as ::: {#epigraph}. This allows users to apply specific styling, such as hanging indents for references. The template includes text processing routines, utilizing regular expressions to perform specific text manipulations, such as formatting dates followed by “CE” into small caps and styling URLs. Finally, the layout of the document is explicitly controlled, starting with a dedicated block for displaying the title, authors, date, and abstract with appropriate vertical spacing and separation from the body. The main body text is formatted with justified alignment and first-line indents, which are applied separately from the title block formatting. The process concludes with a colophon acknowledging the involvement of Typst and Pandoc. |