-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Suggestion: When MathJax Is Used, the LaTeX Environments Included Should Be More Selective (and Put in Math Class) #2758
Comments
The original text here is moved to #2762, since although what said is closely related, the solution is completely different. |
If you want it to be recognized as math (and not passed as Try
|
How about \begin{equation}
...
\end{equation} , in this case there's no If you click markdown-variants, you can see a bunch of such examples where an extra math delimiter will cause an error in LaTeX output. |
You can just use
which you can refer to later as (@myeqn). +++ ickc [Mar 06 16 22:45 ]:
|
Put it in another way, could we make an exception to the follow cases
such that when If this is done, LaTeX and HTML+MathJax compatibility are maximized (and possibly ePub and other HTML related formats? I heard people embedding MathJax in ePub but never tried and don't know the details). |
--mathjax
Option is Used, to Maximize Compatibility Between LaTeX and HTML+MathJax Outputs
Just to add some more info: The Issue: How Latex Environments Should Be Used In MarkdownThe whole issue is really related to how LaTeX environments should be used in markdown (see the original title). MultiMarkdown treated this differently but not without issue: begin equation in $$ / Problems / Discussion Area - MultiMarkdown Support. What MultiMarkdown does is if In pandoc, it is always left as verbatim (except for adding classes in HTML and when ReferencesBoth approach has issues. It is discussed at length in
The Goal: Maximizing LaTeX and HTML+MathJax CompatibilityWhat I said and suggested make sense if we are talking about LaTeX and HTML+MathJax outputs (from single Markdown source). But I understand that pandoc is about multiple output formats not restricted to these 2 in particular (and in fact pandoc have many ways of getting HTML+math). But given that
|
Hi, @jgm, I am thinking about a different way to solve the problem I mentioned. According to my test I mentioned, the following LaTeX environments are supported by MathJax, and should not be put in Math delimiters ( \begin{align}
...
\end{align}
\begin{align*}
...
\end{align*}
\begin{alignat}
...
\end{alignat}
\begin{alignat*}
...
\end{alignat*}
\begin{eqnarray}
...
\end{eqnarray}
\begin{eqnarray*}
...
\end{eqnarray*}
\begin{equation}
...
\end{equation}
\begin{equation*}
...
\end{equation*}
\begin{gather}
...
\end{gather}
\begin{gather*}
...
\end{gather*}
\begin{multline}
...
\end{multline}
\begin{multline*}
...
\end{multline*} I understand from your previous comments that this is not really something pandoc supports. pandoc support math in many output formats, while the support I'm asking above is concerning HTML+MathJax and LaTeX output dual compatibility only. I have 2 different solutions in my mind. I previously suggested that not only the dollar sign delimiters are regarded as math in pandoc, but anyone of the above pairs of The other possible solution is this: any of the above pairs requires an extra display math delimiter in pandoc, like this: $$\begin{align}
...
\end{align}$$
$$\begin{align*}
...
\end{align*}$$
$$\begin{alignat}
...
\end{alignat}$$
$$\begin{alignat*}
...
\end{alignat*}$$
$$\begin{eqnarray}
...
\end{eqnarray}$$
$$\begin{eqnarray*}
...
\end{eqnarray*}$$
$$\begin{equation}
...
\end{equation}$$
$$\begin{equation*}
...
\end{equation*}$$
$$\begin{gather}
...
\end{gather}$$
$$\begin{gather*}
...
\end{gather*}$$
$$\begin{multline}
...
\end{multline}$$
$$\begin{multline*}
...
\end{multline*}$$ Then pandoc will regard them as math automatically. However LaTeX output will resulted in error (because of the extra math delimiter). And although MathJax will produce correct result from the HTML output with the extra math delimiter, it is a better practice to not having those extra delimiter. The solution is then to create a filter to detect the above patterns, once matched, the extra So if such filter is created, are you interested in including it in the official pandoc? |
--mathjax
Option is Used, to Maximize Compatibility Between LaTeX and HTML+MathJax Outputs
I just found out that pandoc LaTeX to Markdown conversion will turn
|
+++ ickc [Apr 17 16 04:20 ]:
The reason for this is that
is equivalent to
|
No, it is not... The former one is not numbered. As I said, I know you commented earlier about pandoc do not support numbered equation. But MathJax and TeX both supported it and at least when a cross compatibility between the 2, TeX and HTML+MathJax, keeping In other words, there's no current way to really mean The solution I was suggesting is, for those environments, an extra pair of And since it deviates from the current behavior (where pandoc do some sophisticated stuff to turn one environment to another closest one, e.g. |
Another test: TeX to Markdown by pandoc: \begin{align}
...
\end{align}
\begin{align*}
...
\end{align*}
\begin{alignat}{2}
...
\end{alignat}
\begin{alignat*}{2}
...
\end{alignat*}
\[
\begin{aligned}
...
\end{aligned}
\]
\[
\begin{alignedat}{2}
...
\end{alignedat}
\]
\[
\begin{array}{lcl}
...
\end{array}
\]
\[
\begin{Bmatrix}
...
\end{Bmatrix}
\]
\[
\begin{bmatrix}
...
\end{bmatrix}
\]
\[
\begin{cases}
...
\end{cases}
\]
\[
\begin{CD}
...
\end{CD}
\]
\begin{eqnarray}
...
\end{eqnarray}
\begin{eqnarray*}
...
\end{eqnarray*}
\begin{equation}
...
\end{equation}
\begin{equation*}
...
\end{equation*}
\begin{gather}
...
\end{gather}
\begin{gather*}
...
\end{gather*}
\[
\begin{gathered}
...
\end{gathered}
\]
\[
\begin{matrix}
...
\end{matrix}
\]
\begin{multline}
...
\end{multline}
\begin{multline*}
...
\end{multline*}
\[
\begin{pmatrix}
...
\end{pmatrix}
\]
\[
\begin{smallmatrix}
...
\end{smallmatrix}
\]
\[
\begin{split}
...
\end{split}
\]
\[
\begin{subarray}
...
\end{subarray}
\]
\[
\begin{Vmatrix}
...
\end{Vmatrix}
\]
\[
\begin{vmatrix}
...
\end{vmatrix}
\] becomes $$\begin{aligned}
...\end{aligned}$$
$$\begin{aligned}
...\end{aligned}$$
$$\begin{aligned}
{2}
...\end{aligned}$$
$$\begin{aligned}
{2}
...\end{aligned}$$
$$\begin{aligned}
...
\end{aligned}$$
$$\begin{alignedat}{2}
...
\end{alignedat}$$
$$\begin{array}{lcl}
...
\end{array}$$
$$\begin{Bmatrix}
...
\end{Bmatrix}$$
$$\begin{bmatrix}
...
\end{bmatrix}$$
$$\begin{cases}
...
\end{cases}$$
$$\begin{CD}
...
\end{CD}$$
$$\begin{aligned}
...\end{aligned}$$
$$\begin{aligned}
...\end{aligned}$$
$$...$$
$$...$$
$$\begin{gathered}
...\end{gathered}$$
$$\begin{gathered}
...\end{gathered}$$
$$\begin{gathered}
...
\end{gathered}$$
$$\begin{matrix}
...
\end{matrix}$$
$$\begin{gathered}
...\end{gathered}$$
$$\begin{gathered}
...\end{gathered}$$
$$\begin{pmatrix}
...
\end{pmatrix}$$
$$\begin{smallmatrix}
...
\end{smallmatrix}$$
$$\begin{split}
...
\end{split}$$
$$\begin{subarray}
...
\end{subarray}$$
$$\begin{Vmatrix}
...
\end{Vmatrix}$$
$$\begin{vmatrix}
...
\end{vmatrix}$$ In short, So clearly pandoc is very sophisticated and can translate some TeX environments to something pandoc prefers. The other way of looking at my feature request is the opposite effects of those translations above e.g. from Markdown's I understand that |
I guess I just ran into this issue as well. I wrote an equation in Markdown: \begin{equation}
f\left(k\right) = \binom{n}{k} p^k\left(1-p\right)^{n-k}
\label{eq:binom}
\end{equation} This can be rendered correctly for both PDF and HTML output, but it disappears in EPUB(3). It will be nice if the equation environment can be preserved for EPUB output. |
Hi, @yihui I assume you use MathJax in HTML? How about ePub? Which option did you use? I heard people embedded MathJax in ePub. With mathjax/MathJax-grunt-cleaner: A grunt file to reduce the footprint of a MathJax installation it can be much smaller than the original(mine is 1.7MB). I created this script to temporary fix the problem to work with these extra environments that pandoc doesn't support: ickc/pandoc-mathjax-extended: Using the LaTeX environments supported by MathJax in pandoc. And I described the problem in Testing LaTeX Environments Usage in MathJax From Markdown Convertion. What I did with that script is basically I assume the $$\begin{equation}
f\left(k\right) = \binom{n}{k} p^k\left(1-p\right)^{n-k}
\label{eq:binom}
\end{equation}$$ and the script do very simple regex substitution to transform it into \begin{equation}
f\left(k\right) = \binom{n}{k} p^k\left(1-p\right)^{n-k}
\label{eq:binom}
\end{equation} Since MathJax accept either of these (with extra Only in LaTeX output this script are applied before as a preprocessor and output the result in stdout to be used in conjunction with pandoc. pandoc will then recognize it as raw LaTeX. If you do use this script watch out for how I did it: I created a new file from the original source by appending a |
@ickc LaTeX math written in
I think the easiest solution is that Pandoc does not simply throw away these math environments when rendering EPUB. Thanks for your idea anyway! |
…(not an optimal solution, and hoping Pandoc can address the real issue)
hi, @yihui Yes, it is the same problem as I original described. Basically related to pandoc only treat things in math delimiter as math. But environments like equation/align are not supposed to used the extra math delimiter. But if it is not used, pandoc treat it as raw LaTeX. This is the current problem. The potential solutions are:
My temporary solution is the 2nd one since it ganrantee pandoc will regard it as math currently. I am hoping an official solution will appear, and hopefully take the first approach. By the way, probably you are not supposed to have them in the HTML output but it is a bug that let the raw LaTeX slip through the HTML output even if |
which means it is already doing part of what I was suggesting. And it also suggests that the correct way of putting those environments in pandoc syntax is raw LaTeX (i.e. So the issues are then: Not Mentioned in the User GuideQuoting from the pandoc user guide:
Not Putting Those LaTeX Environments in Math ClassRelated to the quote in the user guide above, those LaTeX environments are not being
Including Too Much LaTeX EnvironmentsQuoting from MathJax documentation:
By including all raw LaTeX in HTML output with MathJax option means other environments like \begin{flushright}
text flushed to the right
\end{flushright} will also be included in HTML output with MathJax option. See #2860. |
Can you provide a more detailed MWE and commands used? |
Sure. Here is a MWE: test.md $\alpha + \beta$
\begin{equation}
f\left(k\right) = \binom{n}{k} p^k\left(1-p\right)^{n-k}
\label{eq:binom}
\end{equation} I used this command: pandoc -f markdown -t epub3 --mathjax -o test.epub test.md Output: test.zip When I open test.epub, only the math expression in
|
With epub3, pandoc uses mathml for math (even if you specify That explains the difference between I'm open to suggestions as to how this should be handled. If we parsed an "equation" environment as Math, then we'd
which isn't legal. We could strip off the Neither seems ideal. +++ Yihui Xie [May 08 16 23:34 ]:
|
My initial request was to not drop the equation environments all together when writing the EPUB output, i.e. write them verbatim in the output just like what Pandoc does for HTML and LaTeX. It is up to the EPUB reader whether the equations can be rendered, e.g. iBooks cannot render equations, but Calibre can (using MathJax). I think it is better to have equations in EPUB that cannot be rendered (readers can still see the LaTeX source) than just dropping them silently. |
+++ Yihui Xie [May 09 16 13:32 ]:
Yes, I understand the request. This is exactly what pandoc |
Oh okay, I see. I don't have a suggestion on what to do then. Thanks! |
Hi, @jgm, Could you summarize the math pandoc "natively" support? (e.g. it seems like pandoc not only support Thanks. Edit: from pandoc/src/Text/Pandoc/Readers/LaTeX.hs line 444-460 and 1115-1127 it seems there's 2 native environments, gathered and aligned. |
Sorry, a sidetrack:
I thought iBooks can use MathJax too, can't it? (I personally don't use ePub but see it in How to include MathJax in an epub3 file to work with iBooks (and possibly others) · Peter Krautzberger, quite an old post though.) |
@jgm said,
I'm brainstorming an alternative approach to handle this case: could we use mathjax/MathJax-node: Mathjax for Node to convert the math to CommonHTML/SVG if The reasons are:
So in the case |
If the mathjax-node rendered HTML really does work on all Perhaps it would make sense to add a generic math option This could provide a good path for portable math in EPUB. +++ ickc [May 10 16 21:39 ]:
|
@jgm said,
I ran some tests of MathJax-node in ickc/MathJax-node-test: Testing the output of MathJax Node. I think the contenders are CommonHTML and SVG output (since the status of MML across different browsers is bad). Here's a few points I noticed: CommonHTML vs SVG Output
Looks like the MathJax-node SVG output is a winner. Probably the only remaining question is about how well ePub covers it. But most definitely it will be very useful in other output format (HTML). Especially it can have equation numbering and referencing. Probably the MathJax-node MML output can be used instead of the current MML output algorithm too (or as an option). File SizeFile size of all 4 test files in KB:
To put it into perspective, loading the MathJax.js would takes about 400KB. TimeAnother point worth testing perhaps is the rendering time. I didn't time it but the impression is: MML output took the longest time, CommonHTML and SVG comparable while SVG seems faster. I didn't test the time pandoc generates other output like PNG or MML though. Edit: I should have mentioned this: the MathJax-node rendered output will be rendered by browser almost instantly (and from experience we all know how the usual MathJax rendering page took a hit in render times and is typically in the order of seconds). Edit2: I forgot to mention an obvious fact: it requires node.js installation. Hope that won't matters too much. |
Thanks! |
Hi, since MathJax support some of the environments in LaTeX, how should the environment be used in pandoc? I didn't find it mentioned in the documentation.
One example is
I found that currently if I put no delimiter around the above code, HTML and LaTeX output would be visually correct because MathJax can process the LaTeX environment like that (related to mathjax/MathJax-docs#146), and pandoc will pass it in both HTML and LaTeX output.
But looking into the HTML output code, it doesn't parse it with any math class nor put it in
<p>
.For an MWE, I opened a repository to test these things: markdown-variants.
By the way, if we write it like
The LaTeX output will be incorrect and cause errors.
So are there a recommended way to use such environments in pandoc so that both HTML and LaTeX output are correct and pandoc can recognize it and parse it as math rather than raw?
Thanks.
The text was updated successfully, but these errors were encountered: