Cheatsheets:Pandoc

From Wikibase
Revision as of 07:30, 24 August 2026 by Rongzhou (talk | contribs) (→Standalone documents, TOC & metadata)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Languages: English · français · Esperanto

Quick reference for smart people — part of our dev cheatsheets collection.

Pandoc is a universal document converter: write once in Markdown, output to HTML, PDF, DOCX, EPUB, LaTeX, or slides — and convert back from nearly any format.

New to Pandoc? Try the 20-minute tutorial or skim the manual.

Commands assume Pandoc 3.x — check yours with pandoc --version.

Basic example

pandoc pancake.md -o pancake.html

turns

!pancake.md
# Pancakes

A quick pancake recipe for two.

into

!pancake.html
<h1 id="pancakes">Pancakes</h1>
<p>A quick pancake recipe for two.</p>

Pandoc automatically infers the file formats from file extensions!

Converting between formats

Command Conversion
pandoc report.md -o report.html Markdown → HTML
pandoc report.md -o report.pdf Markdown → PDF (see PDF output)
pandoc report.md -o report.docx Markdown → Word
pandoc book.md -o book.epub Markdown → EPUB e-book
pandoc slides.md -t revealjs -s -o slides.html Markdown → HTML slide deck
pandoc page.html -o page.md HTML → Markdown
pandoc data.csv -t markdown CSV → Markdown table
pandoc ch1.md ch2.md -o book.html several files, concatenated

When -o is not provided, output goes to stdout

pandoc notes.docx -t markdown | less

Similarly, when no input file is specified, pandoc reads from stdin

echo "# Hi" | pandoc -t html

Since pandoc infers the file format from the file extension and not from file content, a file without extension (e.g., -o out) causes Unknown output format error.

PDF output

Pandoc needs a typesetting engine to output PDF.

The default engine is LaTeX.

pandoc report.md -o report.pdf                            # default engine: pdflatex
pandoc report.md -o report.pdf --pdf-engine=xelatex
pandoc report.md -o report.pdf --pdf-engine=weasyprint    # HTML-based, no LaTeX needed
pandoc report.md -o report.pdf --pdf-engine=typst         # pandoc >= 3.1.5

If LaTeX is not installed, you may get pdflatex not found error. --pdf-engine=weasyprint or --pdf-engine=typst circumvents the problem.

Standalone documents, TOC & metadata

A bare conversion emits a fragment — no <head>, no title, no TOC. Add -s for a full document:

pandoc report.md -s -o report.html            # full HTML page
pandoc report.md -s --toc -o report.html      # ... with a table of contents
pandoc report.md -s --toc -N -o report.docx   # TOC + numbered sections

Title, author, date must be specified in a YAML block at the top of the source Markdown file:

---
title: "Pancake report"
author: Ada
date: 2025-06-01
---

...or on the command line:

pandoc report.md -s -o report.html -M title="Pancake report"

--toc has no effect without -s — a fragment has no place to put the table of contents.

Also, Pandoc reads its own pandoc markdown dialect by default. Other dialects: -f gfm (GitHub) or -f commonmark.

Common options

Option Effect
-f FORMAT / --from=FORMAT input format: markdown, gfm, commonmark, html, docx, epub, csv, ...
-t FORMAT / --to=FORMAT output format — same list, plus pdf, revealjs, beamer, typst
-o FILE write to FILE instead of stdout
-s / --standalone full document, not a fragment
--toc table of contents; --toc-depth=2 limits the depth
-N / --number-sections number the headings
-M key=value set metadata, e.g. -M title="Report"
--pdf-engine=ENGINE pdflatex (default), xelatex, lualatex, weasyprint, typst
--reference-doc=FILE style template for docx / pptx / odt
--highlight-style=NAME code highlight theme: pygments (default), tango, zenburn, monochrome, ...
--wrap=auto|none|preserve line wrapping in text output
--extract-media=DIR where images from docx / epub are saved
--citeproc process citations and bibliography
--lua-filter=FILE transform the document with a Lua script
--list-input-formats / --list-output-formats list every supported format

docx → Markdown keeps the images — Pandoc extracts them into a media/ folder next to your output by default; --extract-media=DIR changes that directory.

For more