I like to write rmd files that can have html output as well as pdf output. In most of my use cases that works. Just the aligning of formulas does not work as I need the exta dollar signs in html:
The following works in HTML:
---
title: "Test"
author: "RW"
date: "Monday, February 15, 2016"
output: html_document
---
Testing formulae
$$
\begin{align}
y &= x \\
z &= a
\end{align}
$$
and I have to remove the $
to make it work in pdf (which is natural as this would be too much for latex):
---
title: "Test"
author: "RW"
date: "Monday, February 15, 2016"
output: pdf_document
---
Testing formulae
\begin{align}
y &= x \\
z &= a
\end{align}
Is there a way to make it work in html and pdf ?
You could do this with a custom pandoc filter. This one removes the math environment for latex output if and only it uses align
. Save the script as math.py
in the path or in the same directory as your Rmd file
#!/usr/bin/env python
from pandocfilters import toJSONFilter, RawInline, stringify
import re
align = re.compile("\\\\begin{align}")
def math(k, v, f, meta):
if k == 'Math' and f == 'latex' and re.search(align, v[1]):
return RawInline('latex', v[1])
if __name__ == "__main__":
toJSONFilter(math)
And add it to your yaml front matter:
---
title: "Test"
author: "RW"
date: "Monday, February 15, 2016"
output:
pdf_document:
pandoc_args:
- --filter
- definition_to_bold.py
---
Testing formulae
$$
\begin{align}
y &= x \\
z &= a
\end{align}
$$
Now, all the equations written with surrounding $$
and align
environment will work in latex.
You will need python and the pandocfilters
library (pip install pandocfilters
).