pandoc-crossref filter
pandoc-crossref is a pandoc filter for numbering figures, equations, tables and cross-references to them.
Input file (like demo.md) can be converted into html, latex, pdf, md or other formats.
Optionally, you can use cleveref for latex/pdf output, e.g. cleveref pdf, cleveref latex, and listings package, e.g. listings pdf, listings latex
You can also enable per-chapter numbering (as with --chapters
for latex output). You need to specify -M chapters
for non-latex/pdf output however. Examples: html, markdown, latex, pdf.
Tested with pandoc 1.13.2 and 1.14.
This work is inspired by pandoc-fignos and pandoc-eqnos by @tomduck.
This package tries to use latex labels and references if output type is LaTeX. It also tries to supplement rudimentary LaTeX configuration that should mimic metadata configuration by setting header-includes
variable.
Syntax
Syntax is loosely based on discussion in https://github.com/jgm/pandoc/issues/813
Image labels
![Caption](file.ext){#fig:label}
To label an (implicit) figure, append {#fig:label}
(with label
being something unique to reference this figure by) immediately after image definition.
This only works on implicit figures, i.e. an image occurring by itself in a paragraph (which will be rendered as a figure with caption by pandoc)
Image block and label can be separated by one or more spaces.
Equation labels
$$ math $$ {#eq:label}
To label a display equation, append {#eq:label}
(with label
being something unique to reference this equation by) immediately after math block.
This only works if display math and label specification are in a paragraph of its own.
Math block and label can be separated by one or more spaces.
Table labels
a b c
--- --- ---
1 2 3
4 5 6
: Caption {#tbl:label}
To label a table, append {#tbl:label}
at the end of table caption (with label
being something unique to reference this table by). Caption and label must be separated by at least one space.
Code Block labels
There are a couple options to add code block labels. Those work only if code block id starts with lst:
, e.g. {#lst:label}
caption
attribute
caption
attribute will be treated as code block caption. If code block has both id and caption
attributes, it will be treated as numbered code block.
```{#lst:code .haskell caption="Listing caption"}
main :: IO ()
main = putStrLn "Hello World!"
```
Table-style captions
Enabled with codeBlockCaptions
metadata option. If code block is immediately
adjacent to paragraph, starting with Listing:
or :
, said paragraph will be
treated as code block caption.
Listing: Listing caption
```{#lst:code .haskell}
main :: IO ()
main = putStrLn "Hello World!"
```
or
```{#lst:code .haskell}
main :: IO ()
main = putStrLn "Hello World!"
```
: Listing caption
Wrapping div
Wrapping code block without label in a div with id lst:...
and class, starting with listing
, and adding paragraph before code block, but inside div, will treat said paragraph as code block caption.
<div id="lst:code" class="listing">
Listing caption
```{.haskell}
main :: IO ()
main = putStrLn "Hello World!"
```
</div>
References
[@fig:label1;@fig:label2;...] or [@eq:label1;@eq:label2;...] or [@tbl:label1;@tbl:label2;...] or @fig:label or @eq:label or @tbl:label
Reference syntax heavily relies on citation syntax. Basic reference is created by writing @
, then basically desired label with prefix. It is also possible to reference a group of objects of the same type, by putting them into brackets with ;
as separator. Sequential reference numbers will be shortened, e.g. 1,2,3
will be shortened to 1-3
.
Lists
It's possible to use raw latex commands \listoffigures
, \listoftables
and listoflistings
, which will produce ordered list of figure/table/listings titles, in order of appearance in document.
\listoflistings
depends on other options, and is defined in preamble, so it will work reliably only with standalone/pdf output.
Installation
Assuming you already installed Haskell platform, you can install pandoc-crossref with cabal:
cabal update
cabal install pandoc-crossref
Usage
Run pandoc with --filter
option, passing path to pandoc-crossref executable, or simply pandoc-crossref
, if it's in PATH:
pandoc --filter pandoc-crossref
If you installed with cabal, it's most likely located in $HOME/.cabal/bin
on *NIX systems, or in %AppData%\cabal\bin
on Windows.
Customization
There are several parameters that can be set via YAML metadata (either by passing -M
to pandoc
, or by setting it in source markdown)
Following variables are supported:
cref
: if True, latex export will use \cref
from cleveref package. Only relevant for LaTeX output. \usepackage{cleveref}
will be automatically added to header-includes
.
chapter
: if True, number elements as chapter.item
, and restart item
on each first-level heading (as --chapters
for latex/pdf output)
listings
: if True, generate code blocks for listings
package. Only relevant for LaTeX output. \usepackage{listings}
will be automatically added to header-includes
. You need to specify --listings
option as well.
codeBlockCaptions
: if True, parse table-style code block captions.
figureTitle
, default Figure
: Word(s) to prepend to figure titles, e.g. Figure 1: Description
tableTitle
, default Table
: Word(s) to prepend to table titles, e.g. Table 1: Description
listingTitle
, default Listing
: Word(s) to prepend to listing titles, e.g. Listing 1: Description
titleDelimiter
, default :
: What to put between object number and caption text.
figPrefix
, default fig.
, figs.
: Prefix for references to figures, e.g. figs. 1-3
eqnPrefix
, default eq.
, eqns.
: Prefix for references to equations, e.g. eqns. 3,4
tblPrefix
, default tbl.
, tbls.
: Prefix for references to tables, e.g. tbl. 2
lstPrefix
, default lst.
, lsts.
: Prefix for references to lists, e.g. lsts. 2,5
chapDelim
, default .
: Delimiter between chapter number and item number.
rangeDelim
, default -
: Delimiter between reference ranges, e.g. eq. 2-5
lofTitle
, default # List of Figures
: Title for list of figures (lof)
lotTitle
, default # List of Tables
: Title for list of tables (lot)
lolTitle
, default # List of Listings
: Title for list of listings (lol)
figureTemplate
, default \\[figureTitle\\] \\[i\\]\\[titleDelim\\] \\[t\\]
: template for figure captions, see Templates
tableTemplate
, default \\[tableTitle\\] \\[i\\]\\[titleDelim\\] \\[t\\]
: template for table captions, see Templates
listingTemplate
, default \\[tableTitle\\] \\[i\\]\\[titleDelim\\] \\[t\\]
: template for listing captions, see Templates
figPrefix
, eqnPrefix
, tblPrefix
, lstPrefix
can be YAML arrays. That way, value at index corresponds to total number of references in group, f.ex.
fixPrefix:
- "fig."
- "figs."
Will result in all single-value references prefixed with "fig.", and all reference groups of two and more prefixed with "figs.":
[@fig:one] -> fig. 1
[@fig:one; @fig:two] -> figs. 1, 2
[@fig:one; @fig:two; @fig:three] -> figs. 1-3
They can be YAML strings as well. In that case, prefix would be the same regardless of number of references.
Templates
pandoc-crossref supports advanced caption customization via caption templates. Templates are specified as YAML metadata variables (see Customization), and are parsed as default Pandoc Markdown. Variables are specified with display math syntax, i.e. $$var$$
in a template will be replaced with value of variable var
.
Variables can be specified in YAML metadata block, or from command line (with -M
switch). There are two special variables, that are set internally:
i
-- object number, possibly with chapter number (if chapter=True
)
t
-- object caption, as given in source Markdown
Settings file
It is also possible to set variables used by pandoc-crossref with a separate YAML file. If a given variable is not set in metadata, then pandoc-crossref will attempt to read it from file specified by crossrefYaml
metadata variable, or, if not set, from pandoc-crossref.yaml
from current working directory. This allows for reusable configurations. One possible application is ad-hoc internationalization.
For example, consider $HOME/misc/pandoc-crossref-es.yaml
:
figureTitle: "Figura"
tableTitle: "Tabla"
figPrefix: "fig."
eqnPrefix: "ec."
tblPrefix: "tbl."
loftitle: "# Lista de figuras"
lotTitle: "# Lista de tablas"
One could use this with pandoc-crossref as follows:
pandoc -F pandoc-crossref.hs -M "crossrefYaml=$HOME/misc/pandoc-crossref-es.yaml"