Pandoc uses the KaTeX renderer to convert mathematics in documents to PDF. By default, Pandoc recognizes the … syntax for display mathematics and the … syntax for inline mathematics.
It seems like you're using the MathJax syntax, which is … for both inline and display mathematics. Unfortunately, Pandoc doesn't support the MathJax syntax out of the box.
To make Pandoc recognize the … syntax for both inline and display mathematics, you can create a custom Pandoc filter that replaces the … syntax with the … syntax for display mathematics. Here's how to do it:
- First, create a new file called
mathjax2pandoc.js with the following content:
function pandocMathmlToPandoc(mathml) {
// Implement your custom conversion from MathML to KaTeX syntax here.
// For simplicity, we'll just wrap the MathML in the KaTeX display syntax.
return '\\(' + mathml + '\\)';
}
module.exports = function() {
return {
Tree: {
Math: function(node) {
var mathml = node.toString();
var pandoc = pandocMathmlToPandoc(mathml);
return {
type: 'InlineMath',
math: pandoc
};
}
}
};
};
- Install the required libraries:
pip install pandocfilters
pip install katex
- Create a new Pandoc filter using the
pandocfilters package:
pip install --user pandocfilters
- Create a new file called
custom_filter.py with the following content:
from pandocfilters import walk, Element, Str
def replace_math_dollars(element):
if isinstance(element, Str) and element.text.startswith('$') and element.text.endswith('$'):
element.text = '$$' + element.text[1:-1] + '$$'
walk(element)
def main(doc, **kwargs):
walk(doc)
for elem in doc.walk(Str):
replace_math_dollars(elem)
- Add the custom filter to your Pandoc command:
pandoc -s input.md --filter custom_filter.py -o output.pdf
With this setup, your document should now correctly convert the … syntax for both inline and display mathematics.
Note: Make sure your MathJax code can be successfully converted to KaTeX syntax. The provided conversion in mathjax2pandoc.js is a simple wrapper around the MathML content, so it might not work for all cases. You may need to implement a more sophisticated conversion from MathML to KaTeX syntax.