Y. Pei Y. Pei - 1 month ago 20
Markdown Question

mathjax processed differently in title and content of posts with Jekyll

I am trying to understand Jekyll.

I'm using Kramdown as the markdown processor.

There is a bizzarre difference in the way mathjax is processed in title and content.

Say I want to write \sqrt{x}

In the front matters of a post I have to write something like this:

---
title: \(\sqrt x\)
---


But in the content of the post I have to write:

\\(\sqrt x\\)


If I do
\\(\sqrt x\\)
in the title the appearence in the webpage will be mangled.

If I do
\(\sqrt x\)
in the content of the post I'll get single brakets with the literal inside
(\sqrt x)


Why? Can someone explain how strings in these two are treated and why they are treated differently?

Answer

Text in the frontmatter (metadata) is not parsed as Markdown. In fact, Jekyll strips the frontmatter from the document before passing it to Kramdown. Kramdown never even sees the frontmatter. Therefore your "title" is passed through as-is, no escaping needed. If it helps, the frontmatter is not part of Markdown. It is an add-on that is Jekyll specific.

In contrast, within the Markdown text, the mathjax needs to be escaped. Some mathjax syntax conflicts with Markdown syntax, so the parser needs to be told what is mathjax and what is not. That is accomplished by surrounding the mathjax with the proper deliminators. The Markdown parser then strips the escape characters from the content, but otherwise leaves the content unchanged.

Remember that the mathjax syntax is actually parsed and converted to a properly formatted equation by JavaScript within the browser. The HTML generated by Kramdown/Jekyll still contains the unparsed mathjax syntax. But as we have two different paths there are two slightly different ways to get to the same output.

Comments