How can I preprocess the posts? #1888

Closed
MartinThoma opened this Issue Dec 28, 2013 · 8 comments

Projects

None yet

5 participants

@MartinThoma

I would like to use LaTeX in my posts. LaTeX gets rendered by MathJax and it works fine. But Markdown parsers (kramdown, redkarpet) parse this

$$
\begin{align}
m &= |r|\\
n &= |h|\\
\end{align}
$$

and replace & by & and add e.g. CDATA.
(I already filed an kramdown issue for that)

When I write

<div>
$$
\begin{align}
m &= |r|\\
n &= |h|\\
\end{align}
$$
</div>

it works fine. So that could be a quick fix.

But I don't want to encapsulate my LaTeX in divs manually. I would rather like to preprocess every file in _posts. (How) can I do that? Is it possible to write a plugin for that? (I don't want to change anything else. Markdown should still get parsed by kramdown / redcarpet)

Owner
parkr commented Dec 28, 2013

Presently? You can't. I know there are folks who have gotten LaTeX to work in Jekyll, so this shouldn't block you.

#1414 #1771 #1212

@parkr parkr closed this Dec 28, 2013

Could you please give me an article how to get it work or names of "folks who have gotten LaTeX to work in Jekyll"?

I only find variations of LaTeX Math Magic (from C. Woebker) which does not work for me.

Owner
parkr commented Dec 28, 2013

Maruku has it built-in.

I've just tried it and encountered an issue of Maruku:

bhollis/maruku#121

kramdown doesn't seem to be the problem since plain kramdown without jekyll process the input correctly.

@parkr: gettalong just showed to me that kramdown can't be the cause of the CDATA-issue (see link). Do you have any idea what could add the unnecessary CDATA?

Here is the relevant part of my _posts/2013-11-15-word-error-rate-calculation.md:

I've understood it after I saw this on the German Wikipedia:

$$
\begin{align}
m &= |r|\\
n &= |h|\\
\end{align}
$$

$$
\begin{align}
D_{0, 0} &= 0\\
D_{i, 0} &= i, 1 \leq i \leq m\\
D_{0, j} &= j, 1 \leq j \leq n
\end{align}$$

$$
\text{For } 1 \leq i\leq m, 1\leq j \leq n\\
D_{i, j} = \min \begin{cases}
D_{i - 1, j - 1}&+ 0 \ {\rm if}\ u_i = v_j\\
D_{i - 1, j - 1}&+ 1 \ {\rm(Replacement)} \\
D_{i, j - 1}&+ 1 \ {\rm(Insertion)} \\
D_{i - 1, j}&+ 1 \ {\rm(Deletion)} 
\end{cases}
$$

This is the part of _site/word-error-rate-calculation/index.html:

I’ve understood it after I saw this on the German Wikipedia:

<script type="math/tex; mode=display">
<![CDATA[% <![CDATA[

\begin{align}
m &= |r|\\
n &= |h|\\
\end{align}
 %]]]]><![CDATA[>]]>
</script><script type="math/tex; mode=display">
<![CDATA[% <![CDATA[

\begin{align}
D_{0, 0} &= 0\\
D_{i, 0} &= i, 1 \leq i \leq m\\
D_{0, j} &= j, 1 \leq j \leq n
\end{align} %]]]]><![CDATA[>]]>
</script><script type="math/tex; mode=display">
<![CDATA[% <![CDATA[

\text{For } 1 \leq i\leq m, 1\leq j \leq n\\
D_{i, j} = \min \begin{cases}
D_{i - 1, j - 1}&+ 0 \ {\rm if}\ u_i = v_j\\
D_{i - 1, j - 1}&+ 1 \ {\rm(Replacement)} \\
D_{i, j - 1}&+ 1 \ {\rm(Insertion)} \\
D_{i - 1, j}&+ 1 \ {\rm(Deletion)} 
\end{cases}
 %]]]]><![CDATA[>]]>
</script>

This is what cat 2013-11-15-word-error-rate-calculation.md| kramdown > output.html generates:

I’ve understood it after I saw this on the German Wikipedia:

<script type="math/tex; mode=display">% <![CDATA[

\begin{align}
m &= |r|\\
n &= |h|\\
\end{align}
 %]]></script>

<script type="math/tex; mode=display">% <![CDATA[

\begin{align}
D_{0, 0} &= 0\\
D_{i, 0} &= i, 1 \leq i \leq m\\
D_{0, j} &= j, 1 \leq j \leq n
\end{align} %]]></script>

<script type="math/tex; mode=display">% <![CDATA[

\text{For } 1 \leq i\leq m, 1\leq j \leq n\\
D_{i, j} = \min \begin{cases}
D_{i - 1, j - 1}&+ 0 \ {\rm if}\ u_i = v_j\\
D_{i - 1, j - 1}&+ 1 \ {\rm(Replacement)} \\
D_{i, j - 1}&+ 1 \ {\rm(Insertion)} \\
D_{i - 1, j}&+ 1 \ {\rm(Deletion)} 
\end{cases}
 %]]></script>

As you can see, something inserts <![CDATA[ ... ]]]]><![CDATA[>.
Do you have any idea what could be the cause of this bug?

I've just fixed the problem for me by adding some Python preprocessing (done with Makefile and _preprocess.py, see my blog sources). This is by no means an ideal solution, but it almost solves the problem for me.

@MartinThoma Kramdown + MathJax currently seems to work fine on your initial example: http://cirosantilli.github.io/jekyll-cheat/#math

The usual slow MathJax rendering of course...

@jekyllbot jekyllbot locked and limited conversation to collaborators Feb 27, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.