28

To make a numbered equation in Pandoc, I defer to using pure LaTeX:

\begin{equation}
a^2 + b^2 = c^2
\end{equation}

In PDF form, it gives a nice numbered equation, however, this equation doesn't show up at all in the HTML output because the equation is within the \begin environment and is ignored. I would like to keep the numbering in the PDF, but would like the equation to show up in the HTML, with or without the numbering.

6 Answers 6

21

An extension exists on github: pandoc-crossref

install it with

cabal update
cabal install pandoc-crossref

or in archlinux using ArchHaskell

pacman -Sy pandoc-crossref

you can use it by doing

$$ math $$ {#eq:label}
[@eq:label]

and compiling with

pandoc file.md --filter pandoc-crossref -o file.pdf

for more information see the documentation

Alternatively, as described in https://github.com/jgm/pandoc/issues/1938#issuecomment-74011358 you can use --mathjax for HTML rendering

if your equations are in math.txt

$$ a^2 + b^2 = c^2 $$

create a file header

<script type="text/x-mathjax-config">
  MathJax.Hub.Config({ TeX: { equationNumbers: {autoNumber: "all"} } });
</script>

and compile with

pandoc math.txt -t html -s -o test.html --mathjax=https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML -H header 
1
  • Welcome to TeX.SX! You can have a look at our starter guide to familiarize yourself further with our format.
    – Symbol 1
    Apr 1, 2015 at 9:27
14

In pandoc you can do:

(@foo)  $a^2 + b^2 = c^2$

As (@foo) says, ...

(@bar)  $e = x + y$

This will give you running equation numbers in both PDF and HTML. However, the equations will not be centered or display-formatted. (You can use $$ for display math, but then the numbers probably won't be lined up right -- though maybe this can be fixed with CSS.)

5
  • Doing it this way, the equation ends up being placed on the left and slightly above the equation itself. Is there a way to have the equation number on the right just like in the regular latex format? May 3, 2013 at 20:11
  • No, there's no way to get the number on the right. You could probably fix alignment issues with display math using CSS. May 4, 2013 at 2:06
  • I meant in the PDF output, the equation number gets moved to the left. As you mentioned, the HTML shouldn't be an issue with some CSS. May 4, 2013 at 16:25
  • @JohnMacFarlane just wondering if this (displaymath, right-aligned eqs) is something that might be addressed in later pandoc releases, or if explicit LaTeX environment remains the way to go for pdf output?
    – cboettig
    Jun 23, 2014 at 18:20
  • Mathjax supports \ref and \eqref in plain HTML: cdn.mathjax.org/mathjax/latest/test/sample-eqrefs.html --- can Pandoc users get access to this functionality? (I'm converting multimarkdown to html.) Jun 29, 2014 at 4:26
10

Another option for equation numbers and references is the pandoc-eqnos filter.

Install pandoc-eqnos as root using the bash command

pip install pandoc-eqnos 

To tag an equation with the label eq:description, use

$$ y = mx + b $$ {#eq:description}

The prefix #eq: is required whereas description can be replaced with any combination of letters, numbers, dashes, slashes and underscores.

To reference the equation, use

@eq:description

To apply the filter, use the following option with pandoc:

--filter pandoc-eqnos

Full details and demos are given on the pandoc-eqnos Web page at github.

1
  • Just a note: I suspect that pando-eqnos broke in the meantime. In spite of [INFO] Running filter pandoc-eqnos [INFO] Completed filter pandoc-eqnos it will for me result in [INFO] Parsing unescaped # at <file> line <line> column <column> just as it would if pandoc-eqnos was not included. The parsing-unescaped-# error happens before the filter is run, so I suspect the equation labels and tags no longer arrive at pandoc-eqnos and pandoc-eqnos does consequently not do anything any more. I do not see this error reported anywhere else, so I suspect nobody is using pandoc-eqnos any more.
    – 0range
    Dec 22, 2022 at 16:10
3

If you use css styling you can add the following in your css file:

/*create an equation counter variable */

body {
   counter-reset:  EQU_COUNT 0 ;
}

and:

.MathJax_Display:after {
      content: "      (" counter(EQU_COUNT) ")";    
      counter-increment: EQU_COUNT;           
    }

This will automatically add a an incremented counter value after each equation.

2

Using math2svg & CSS

The math2svg Lua filter for Pandoc converts LaTeX math to MathJax generated scalable vector graphics (SVG) for insertion into the output document in a standalone manner. SVG output is in any of the available MathJax fonts.

This is useful when a CSS paged media engine (such as Prince XML) cannot process complex JavaScript as required by MathJax. See: https://www.print-css.rocks for information about CSS paged media, a W3C standard.

Below example CSS code centers display math whilst adding equation numbers to the right.

span.math.display {
    display: flex;
    justify-content: flex-end;
    align-items: center;
    }

span.math.display svg {
    margin: 0 auto;
    max-width: 85%;
    }

span.math.display:after {
    counter-increment: equation;
    content: '(' counter(equation) ')';
    font-weight: bold;
    }

More in-depth information is available from the math2svg documentation.

Using ConTeXt

If ConTeXt is used as the engine to produce the PDF output, then one can simply precede the LaTeX display formula with a \placeformula ConTeXt command in the Pandoc Markdown input:

\placeformula
$$ a^2 + b^2 = c^2 $$

When multiple output formats are desired, it is better to use a GNU makefile containing a sed command:

SHELL := /usr/bin/env bash
NAME := $(basename $(wildcard *.md))

all: $(NAME).pdf $(NAME).docx

$(NAME).pdf: $(NAME).md
    pandoc \
    <(sed 's/^$$$$.*/\\placeformula\n&/' $<) \
    --smart --output=$(NAME).tex --to=context
    context $(NAME).tex

$(NAME).docx: $(NAME).md
    pandoc $< --smart --output=$(NAME).docx
0

Another alternative is to render the equations as PDF minipages, convert to SVG, and display those on the Web.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .