Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content

Support template-partials for docx output #15399

Open

Description

@cderv

Following some discussion with 🤖

template-partials works for html, revealjs, pdf, beamer and typst, but not for docx. Since Pandoc 3.2.1, the docx writer has an OpenXML template for the body of the document (word/document.xml), and plain Pandoc resolves partials in it. Quarto passes a user template: to Pandoc for docx, but refuses template-partials.

What works today

quarto pandoc -D docx prints Pandoc's default.openxml: one w:p each for title, subtitle, authors, date and abstract (with variables like $title-style-id$), then include-before, toc, lof, lot, body, include-after and sectpr.

A copy of it given as format: docx: template: tpl.openxml is used by Quarto, and Pandoc finds a partial placed next to it ($logo-block.openxml()$ with logo-block.openxml in the same directory). This is what https://github.com/mcanouil/quarto-better-word does with its title-block.xml and toc.xml partials.

Metadata reaches the template as OpenXML runs: institute: NSO used as $institute$ gives <w:r><w:t xml:space="preserve">NSO</w:t></w:r>, so it must be placed inside a w:p. Quarto's normalized by-author and labels are available too ($it.name.literal$, affiliations, $labels.abstract$). An image in metadata (logo: "![](logo.png){width=1cm}") used as $logo$ is embedded in the package.

What fails

---
title: Hello
format:
  docx:
    template: tpl.openxml
    template-partials:
      - logo-block.openxml
---

Body text.
ERROR: The format docx does not support providing any template partials.

The error comes from the else branch for formats without a templateContext. docx declares none, so partials are refused even when the user gives a template.

} else {
// ipynb is allowed to have templates without warning
if (userPartials.length > 0 && !isIpynbOutput(options.format.pandoc)) {
// The user passed partials to a format that doesn't support
// staging and partials.
throw new Error(
`The format ${allDefaults.to} does not support providing any template partials.`,
);
} else if (userTemplate) {
// Use the template provided by the user
allDefaults[kTemplate] = userTemplate;
}
}

export function docxFormat(): Format {
return mergeConfigs(
createWordprocessorFormat("MS Word", "docx"),
{
formatExtras: () => {
return {
[kFilterParams]: {
[kIconCaution]: iconPath("caution.png"),
[kIconImportant]: iconPath("important.png"),
[kIconNote]: iconPath("note.png"),
[kIconTip]: iconPath("tip.png"),
[kIconWarning]: iconPath("warning.png"),
},
};
},
extensions: {
book: {
selfContainedOutput: true,
},
},
},
);
}

Use case

I wrote an example where one document renders to HTML, Typst and Word with the same title block and a logo. HTML needed a 21-line title-block.html partial and Typst an override of article(). For Word there was no hook in the body, so the logo went into a page header of a reference document built by a script (source). A title-block partial for docx would have been enough for that part.

Proposal

We could support docx the way Typst and LaTeX do: Quarto keeps its own copy of Pandoc's template, split into named partials, and format-docx.ts declares a templateContext.

// Provide a template and partials
// For Typst books, a book extension overrides these partials
const templateDir = formatResourcePath("typst", join("pandoc", "quarto"));
const templateContext = {
template: join(templateDir, "template.typ"),
partials: [
"numbering.typ",
"definitions.typ",
"typst-template.typ",
"page.typ",
"typst-show.typ",
"notes.typ",
"biblio.typ",
].map((partial) => join(templateDir, partial)),
};

Design questions, with what I think for each:

  1. Minimal change or full support ? Accepting template-partials whenever the user gives a template would not bring much for docx, as a user template already finds partials next to it. Full support is what lets a user override only the title block. One thing to handle: when a format has a templateContext, Quarto copies the user template into a temporary directory, and sibling partials are no longer found. With format: html and a template.html calling $foo.html()$ next to it, render fails with Could not find data file 'templates\foo.html'. If docx gets a templateContext, templates written for today's behavior (like the extension above) would break the same way. We could keep passing a user template to Pandoc unstaged when no template-partials is given, or copy its directory. Open.

  2. Partials and names. Following title-block.html and LaTeX's toc.tex, before-body.tex, after-body.tex:

    • template.openxml: the orchestrator;
    • title-block.openxml: title, subtitle, authors, date, abstract;
    • toc.openxml: toc, lof, lot;
    • before-body.openxml and after-body.openxml: empty, around body.

    Whether sectpr gets its own partial (page size and margins, see Support setting page size in ODT and DOCX output #11163 and .landscape does not keep page size for Word (enforce "A4" in landscape whilst default doc is US Letter) #12917) is open. The first version should produce the same document.xml as today.

  3. File extension. Quarto stages the main template as template.patched, so Pandoc's rule that a partial without extension takes the extension of the template does not apply. $foo()$ in a user HTML template fails with Could not find data file 'templates\foo.patched'. Quarto templates call partials with an explicit extension, so the extension is only a naming choice. .openxml matches Pandoc's default.openxml. User partials replace built-in ones by file name, so title-block.xml would not replace title-block.openxml: the docs need to say it.

  4. Maintenance. The writer and the template are coupled: default.openxml changed five times since 3.2.1, including the switch to style ids ($title-style-id$) in Pandoc 3.6.1 (Loss of style for titles and/or subtitles in .docx output document jgm/pandoc#10282). An outdated Quarto copy would give wrong styles without an error. So:

    • package/src/common/update-pandoc.ts: copy default.openxml as src/resources/formats/docx/pandoc/openxml.template, next to Quarto's template.openxml and partials (same naming as other formats);
    • dev-docs/update-pandoc-checklist.md: a Docx entry in the template list;
    • the updating-pandoc skill: add docx to the template port step, and an llm-docs page like the Typst and LaTeX ones.

    const templateDirFiles: Record<string, Array<{ from: string; to?: string }>> =
    {
    [jatsOutDir]: [
    { from: "affiliations.jats" },
    { from: "article.jats_publishing" },
    { from: "default.jats_archiving" },
    { from: "default.jats_articleauthoring" },
    { from: "default.jats_publishing" },
    ],
    [htmlOutdir]: [
    { from: "default.html5", to: "html.template" },
    { from: "styles.html", to: "html.styles" },
    ],
    [revealOutdir]: [
    { from: "default.revealjs", to: "revealjs.template" },
    ],
    [latexOutdir]: [
    { from: "default.latex", to: "latex.template" },
    // Template we need to tweak
    { from: "common.latex", to: "latex.common" },
    // Template kept unchanged
    { from: "after-header-includes.latex" },
    { from: "hypersetup.latex" },
    { from: "font-settings.latex" },
    { from: "fonts.latex" },
    { from: "passoptions.latex" },
    ],
    [beamerOutdir]: [
    { from: "default.beamer", to: "beamer.template" },
    // Template we need to tweak
    { from: "common.latex", to: "latex.common" },
    // Template kept unchanged
    { from: "after-header-includes.latex" },
    { from: "hypersetup.latex" },
    { from: "font-settings.latex" },
    { from: "fonts.latex" },
    { from: "passoptions.latex" },
    ],
    [asciidocOutdir]: [
    { from: "default.asciidoc", to: "asciidoc.template" },
    ],
    [typstOutdir]: [
    { from: "default.typst", to: "typst.template" },
    { from: "template.typst" }
    ]
    };

  5. Interactions.

    • Title block: Quarto does not write its own title block for docx today, Pandoc's template does it from metadata. Quarto's author normalization only sets variables, so moving the title block into a partial changes nothing by itself. Showing affiliations like the other formats could come later (they are dropped today, see Proof that affiliations are thrown away with --to docx #14435).
    • Books: a docx book renders to a single document with one title block from book: metadata. A two-chapter book with template: uses it once, with a sibling partial resolved, and template-partials fails with the same error. Partials would apply to the whole book, nothing book-specific is needed.
    • Extensions: a docx format extension can set template: today (see item 1). With full support, it could also ship template-partials, like Typst and LaTeX extensions do.
    • include-before-body files have no effect for docx in Quarto, unlike plain Pandoc. This is a separate bug, I'll open its own issue.
  6. Documentation: the template partials page on quarto.org (https://quarto.org/docs/journals/templates.html) lists formats and their partials, docx would be added there, with a note that partials are OpenXML fragments and that variables are runs to place inside a w:p.

Checked

Checked on main at cda4102 with Pandoc 3.13, on Windows. Outputs with a custom template, a partial and an embedded image opened in Word 16.0 (Microsoft 365) without errors. Not checked on Linux or macOS.

Related: #14695 (discussion asking for docx templates), #4511, #11014 and #6114 (title or cover page for Word), #15381 (_brand.yml for docx).

Activity

  1. added
    enhancement
    docx
    on Nov 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    docxenhancementtemplates

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions