Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
quarto-dev
GitHub Repository: quarto-dev/quarto-cli
Path: blob/main/llm-docs/pandoc-quarto-latex-templates.md
6451 views

Pandoc and Quarto LaTeX Templates

This document describes how Quarto integrates Pandoc's LaTeX templates and transforms them into a modular structure suitable for Quarto's extended functionality.

1. How Pandoc Templates Are Copied into Quarto

The writePandocTemplates function in package/src/common/update-pandoc.ts handles copying Pandoc's templates into Quarto's resources during the Pandoc update process.

Source and Destination

  • Source: Pandoc templates are downloaded from the Pandoc GitHub repository's data/templates/ directory

  • Destination: src/resources/formats/pdf/pandoc/

Files Copied

Seven files are copied from Pandoc for LaTeX support:

Pandoc SourceQuarto DestinationNotes
default.latexlatex.templateMain Pandoc template
common.latexlatex.commonReference copy (Quarto maintains modified version)
after-header-includes.latex(unchanged)Post-header-includes processing
hypersetup.latex(unchanged)Hyperref configuration
font-settings.latex(unchanged)User font configuration
fonts.latex(unchanged)Font engine detection
passoptions.latex(unchanged)Package options passthrough

Purpose of Each Pandoc File

default.latex (becomes latex.template): The main Pandoc LaTeX template. Quarto uses its own template.tex as the main orchestrator instead.

common.latex (becomes latex.common): Common preamble setup from Pandoc including paragraph formatting, verbatim settings, highlighting, tables, graphics, citations, and bibliography. Quarto keeps this as a reference copy.

after-header-includes.latex: Processing after user-provided header includes. Sets up xspace, bookmark, and URL packages.

hypersetup.latex: Hyperref configuration for PDF links, colors, and metadata.

font-settings.latex: User font configuration for different TeX engines (PDFTeX, XeTeX, LuaTeX). Handles mainfont, sansfont, monofont, mathfont, and CJK fonts.

fonts.latex: Font engine detection and setup. Loads fontspec for XeTeX/LuaTeX, handles font fallbacks.

passoptions.latex: Package options passthrough mechanism for hyperref, xcolor, and xeCJK.

2. Quarto's Modular Template Structure

Quarto breaks the Pandoc templates into a modular structure in src/resources/formats/pdf/pandoc/. This allows for:

  • Better separation of concerns

  • Easier customization and extension

  • Support for Quarto-specific features (author normalization, bibliography control)

File Extension Convention

Quarto uses file extensions to distinguish template origin:

  • .tex files: Quarto-maintained partials

  • .latex files: Pandoc-derived partials

Template Files

The pdfFormat function in src/format/pdf/format-pdf.ts configures the template context, specifying template.tex as the main template with partials from both Quarto (.tex) and Pandoc (.latex).

template.tex - The Orchestrator

Assembles the document by including all partials in order. This is Quarto's main template, distinct from Pandoc's latex.template.

The "common" Files Architecture

Quarto maintains two versions of the common preamble:

latex.common (264 lines): Pandoc's original, kept as reference. Contains the full common preamble: paragraph formatting, verbatim, highlighting, tables, graphics, strikeout, CSL citations, Babel, page style, tight lists, subfigure support, text direction, natbib/biblatex configuration.

common.latex (66 lines): Quarto's shortened version that:

  • Keeps Pandoc's paragraph formatting, verbatim, and listings setup

  • Calls $pandoc.tex()$ to delegate the rest to Quarto partials

This allows Quarto to selectively override specific parts of the common preamble while maintaining compatibility with Pandoc updates.

Quarto Partials (*.tex)

Document Structure:

FilePurpose
doc-class.texDocument class declaration with babel languages, fontsize, papersize, and class options
before-title.texEmpty placeholder for pre-title content
title.texTitle, subtitle, author, and date setup. Uses Quarto's authors normalization (it.name.literal)
before-body.texFrontmatter, \maketitle, and abstract rendering
toc.texTable of contents, list of figures, list of tables
after-body.texEmpty placeholder for post-body content

Bibliography:

FilePurpose
before-bib.texEmpty placeholder before bibliography
biblio.texBibliography rendering with natbib or biblatex
biblio-config.texBibliography package configuration. Adds $biblio-config$ conditional (Quarto-only variable) to allow class files to handle bibliography configuration

Content Elements (called via pandoc.tex):

FilePurpose
tables.texTable packages (longtable, booktabs, array, multirow) and footnote handling
graphics.texgraphicx package and \pandocbounded command for auto-scaling images
citations.texCSL citation definitions (\citeproc, CSLReferences environment)
babel-lang.texBabel language configuration and font setup
tightlist.tex\tightlist command for compact list spacing

Hub Partial:

FilePurpose
pandoc.texHub that calls other Quarto partials. Contains highlighting, strikeout/underline, page style, subfigure support, text direction. Calls: $tables.tex()$, $graphics.tex()$, $citations.tex()$, $babel-lang.tex()$, $tightlist.tex()$, $biblio-config.tex()$

3. Template Invocation Order

The template.tex orchestrates the full document:

Preamble (before \begin{document})

  1. $passoptions.latex()$ - Package options passthrough

  2. $doc-class.tex()$ - \documentclass[...]{...} declaration

  3. (inline) - beamerarticle, xcolor, geometry, amsmath

  4. (inline) - Section numbering configuration

  5. $fonts.latex()$ - Font engine detection (ifPDFTeX, ifXeTeX, ifLuaTeX)

  6. $font-settings.latex()$ - User font configuration

  7. $common.latex()$ - Common preamble setup, which internally calls:

    • $pandoc.tex()$$tables.tex()$, $graphics.tex()$, $citations.tex()$, $babel-lang.tex()$, $tightlist.tex()$, $biblio-config.tex()$

  8. $after-header-includes.latex()$ - Bookmark, URL packages

  9. $hypersetup.latex()$ - Hyperref configuration

  10. $before-title.tex()$ - Pre-title content

  11. $title.tex()$ - \title{}, \author{}, \date{}

Document Body

  1. \begin{document}

  2. $before-body.tex()$ - Frontmatter, \maketitle, abstract

  3. (loop) - include-before content

  4. $toc.tex()$ - Table of contents

  5. (inline) - linestretch, mainmatter

  6. $body$ - Document content

  7. $before-bib.tex()$ - Pre-bibliography content

  8. (inline) - backmatter, nocite

  9. $biblio.tex()$ - Bibliography rendering

  10. (loop) - include-after content

  11. $after-body.tex()$ - Post-body content

  12. \end{document}

4. TypeScript Configuration

The pdfFormat function in src/format/pdf/format-pdf.ts (lines 236-284) defines the template configuration:

// Quarto-maintained partials (.tex) const partialNamesQuarto: string[] = [ "babel-lang", "before-bib", "biblio", "biblio-config", "citations", "doc-class", "graphics", "after-body", "before-body", "pandoc", "tables", "tightlist", "before-title", "title", "toc", ]; // Pandoc-derived partials (.latex) const partialNamesPandoc: string[] = [ "after-header-includes", "common", "font-settings", "fonts", "hypersetup", "passoptions", ];

The template context maps these to files:

  • Quarto partials: pandoc/${name}.tex

  • Pandoc partials: pandoc/${name}.latex

5. Parameter Summary Table

Font Parameters (XeTeX/LuaTeX)

ParameterTypeDescription
mainfontstringMain text font family
mainfontoptionsarrayFont options for main font
mainfontfallbackarrayFallback fonts for main font
sansfontstringSans-serif font family
sansfontoptionsarrayFont options for sans font
sansfontfallbackarrayFallback fonts for sans font
monofontstringMonospace font family
monofontoptionsarrayFont options for mono font
monofontfallbackarrayFallback fonts for mono font
mathfontstringMath font family
mathfontoptionsarrayFont options for math font

Font Parameters (PDFTeX)

ParameterTypeDescription
fontfamilystringFont package name (e.g., "libertinus")
fontfamilyoptionsarrayOptions for font package
fontencstringFont encoding (default: "T1")

CJK Font Parameters

ParameterTypeDescription
CJKmainfontstringCJK main font
CJKsansfontstringCJK sans font
CJKmonofontstringCJK mono font
CJKoptionsarrayCJK font options

Layout Parameters

ParameterTypeDescription
documentclassstringDocument class (default: "scrartcl" for Quarto)
classoptionarrayClass options (e.g., "12pt", "twoside")
papersizestringPaper size (e.g., "letter", "a4")
geometryarrayGeometry package options
linestretchnumberLine spacing multiplier

Section Parameters

ParameterTypeDescription
numbersectionsbooleanEnable section numbering
secnumdepthintegerDepth of section numbering
fontsizestringBase font size (e.g., "11pt")

Color Parameters

ParameterTypeDescription
linkcolorstringColor for internal links
citecolorstringColor for citations
urlcolorstringColor for URLs
toccolorstringColor for TOC links

Bibliography Parameters

ParameterTypeDescription
natbibbooleanUse natbib for citations
biblatexbooleanUse biblatex for citations
biblio-stylestringBibliography style
biblio-titlestringBibliography section title
bibliographyarrayBibliography file paths
biblio-configbooleanEmit bibliography configuration (Quarto-only, defaults to true)

Quarto-Specific Parameters

ParameterQuarto NameDescription
number-sectionsnumbersectionsEnable section numbering
shift-heading-level-byshift-heading-level-byAuto-set to -1 when numbersections and no L1 headings
toctocInclude table of contents
toc-depthtoc-depthTOC depth
toc-titletoc-titleTOC heading
loflofInclude list of figures
lotlotInclude list of tables

Brand.yaml Integration

Important: Unlike HTML and Typst formats, LaTeX does NOT have automatic brand.yaml fallback for typography. Font and color settings must be specified explicitly via Pandoc metadata variables:

format: pdf: mainfont: "Inter" sansfont: "Outfit" monofont: "Fira Code" fontsize: 11pt