Latex \newcommand in kramdown

623 views Asked by At

I understand that cramdown does not support \newcommand type macros. Is there a workaround that does not involve Pandoc, so that it could be used with Jekyll in a GitHub blog?

This would be the input markdown:

---
layout: post
---

\newcommand{\a}{\alpha}

This is a test $$\a$$.

The output should be a Jekyll blog, with mathematical notation, like this.

1

There are 1 answers

1
Kyle Barbour On BEST ANSWER

There seems to be two questions here — first, can one define commands in kramdown similar to LaTeX's \newcommand syntax, and second, can kramdown support mathematics.

Regarding the first issue, kramdown itself doesn't support that syntax or anything like it that I'm aware of, and probably isn't going to in the future, so you can't define non-math kramdown commands. You'd need to use a language that's focused on completeness rather than ease and simplicity, like LaTeX, to get features like that.

For math, though, MathJax (the parser kramdown uses) does parse \newcommand: just go ahead and use it in the first code block. So for example,

$$\newcommand{\a}{\alpha} \a{}/2 $$

yields the expected fraction with the expected Computer Modern italic Greek alpha, and you'll also be able to use \a in every subsequent code block. You can also have a code block at the beginning of your document that contains all of your \newcommand definitions if you don't want them to be interspersed in random code blocks throughout your document.

Regarding the second issue, as I guess is obvious at this point, kramdown does handle math using similar notation to LaTeX:

Here is a paragraph with some inline math, $$ \pi{}/2 $$, followed by
some text and a math block.

$$ \int_{-\infty}^{\infty} e^{-x^2} dx = \sqrt{\pi} $$

This yields:

<p>Here is a paragraph with some inline math, <script
type="math/tex">\pi{}/2</script>, followed by some text and a math 
block.</p>

<script type="math/tex; mode=display">\int_{-\infty}^{\infty} e^{-x^2}
dx = \sqrt{\pi}</script>

If that isn't successfully rendering for you on your site, don't forget to include the MathJax library in the <head> of your HTML page:

<script type="text/javascript"
    src="https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
</script>