Skip to content
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

Pandoc doesn't include MathJax CDN in HTML when document contains no math #8469

Closed
rnwst opened this issue Dec 4, 2022 · 5 comments
Closed
Labels

Comments

@rnwst
Copy link
Sponsor Contributor

rnwst commented Dec 4, 2022

Explain the problem.

Currently, pandoc only includes the MathJax CDN in the HTML output when the document contains math. I am using a pandoc filter which injects math into a page which otherwise contains no math. Since pandoc does not include the MathJax CDN, this math isn't being rendered when the page is loaded (since MathJax never gets loaded). My initial expectation was that when the --mathjax option is passed to pandoc, it will include the MathJax CDN, no matter what. In particular, when the URL is specified according to --mathjax=URL, I most certainly expect the resulting HTML file to contain the MathJax URL that I have explicitly passed to pandoc.

Test command:

pandoc test.md -o test.html -f markdown -t html --mathjax --standalone

The resulting HTML file contains no link to the MathJax CDN.

test.md:

---
title: A Test.
---

A document without math!

Pandoc version?

$ pandoc --version
pandoc 2.19.2
Compiled with pandoc-types 1.22.2.1, texmath 0.12.5.4, skylighting 0.13.1.1,
citeproc 0.8.0.2, ipynb 0.2, hslua 2.2.1
Scripting engine: Lua 5.4

OS: Linux

@rnwst rnwst added the bug label Dec 4, 2022
@rnwst rnwst changed the title Always include MathJax CDN even when document contains no math Pandoc doesn't include MathJax CDN in HTML when document contains no math Dec 4, 2022
@jgm
Copy link
Owner

jgm commented Dec 4, 2022

It's seems a desirable feature not to include the link if the document doesn't contain math. (That avoids some unnecessary network activity.) Yes, you could omit --mathjax, but you might be processing a large number of files with the same options, and only some of them have math.

But if you're injecting Math elements using a filter, the writer should include the link. The writer is seeing the AST, as modified by the filter, not the original document. Are you perhaps including these elements as RawInline or RawBlock instead of Math?

@jgm
Copy link
Owner

jgm commented Dec 4, 2022

I did notice one flaw in the HTML writer: it doesn't set the "math" template variable when it renders raw LaTeX "math environments" (these are things, like the align environment, that shouldn't be enclosed in Math elements because in LaTeX they aren't valid in math mode). I'll fix this, maybe that's your issue if you're using RawBlock/RawInline?

jgm added a commit that referenced this issue Dec 4, 2022
commands or environments that can be interpreted as math e.g. by
MathJax.  See #8469.
@rnwst
Copy link
Sponsor Contributor Author

rnwst commented Dec 4, 2022

Thank you @jgm for your quick response. I hadn't considered the use case of processing a large amount of files with the same options, and I agree that it is sensible to avoid unnecessary network activity. In my particular use case, the filter injects SVGs inside of RawBlocks, and those SVGs contain <foreignObject> elements, which contain math (in the format \(math\)), which is later rendered by MathJax. So it seems this is unrelated to the issue relating to "math environments" that you have just discovered. Can you think of a way to include the MathJax CDN (other than providing a custom HTML template)?

@jgm
Copy link
Owner

jgm commented Dec 4, 2022

One option would be to set the math variable directly.
The template contains

$if(math)$
  $math$
$endif$

so you could use --variable math="HTML code for mathjax link here" on the command line or under variables in a defaults file. Alternatively, use header-includes in metadata.

@rnwst
Copy link
Sponsor Contributor Author

rnwst commented Dec 4, 2022

Perfect, thanks!

@rnwst rnwst closed this as completed Dec 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants