Pandoc: --self-contained unfriendly towards --mathjax

Created on 3 Dec 2012  路  15Comments  路  Source: jgm/pandoc

I'd like to distribute my notes as standalone html files. They contain both maths to be rendered with mathjax and figures.

The problem is that enabling both --self-contained and --mathjax ends with error message:

pandoc: user error (https not supported)

But even if I provide an url without https, mathjax fails to load any of its assets.

Most helpful comment

Seconding Jared's request. I don't mind pulling stuff off of CDNs, but I'd like to have a distributable HTML file that includes embedded images, which Pandoc is amazing for.

All 15 comments

I am also having this issue. Running with --mathjax but without --self-contained will make the mathjax formulas render properly. Also running with --stand-alone but without --mathjax will also stop the error. It seems that the combination of both is causing the issue.

To follow up, I believe the issue is related to how pandoc embeds the MathJax.js script. By default pandoc tries to fetch the MathJax.js script from the https address, which does not appear to be supported by Haskell. If you pass the http address for the script it will be embedded but the embedding seems to have issues.

A quick hack is to replace the line which embeds the script with <script type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js"></script> to get the page to render properly. The file is no longer viewable offline though.

We get the error message: "Failed to load: /extensions/MathZoom.js" in the browser.

Given the way these extensions are loaded by the script, I don't see a good way for --self-contained to handle them. We might have to be content with a note in README that --mathjax won't work with --self-contained.

Added note to README with 68272fad661d28135915aa6a66b01548c063eb8a.
I can't think of a way to get --self-contained to work properly with --mathjax, so this bit of documentation will have to do.

I understand that --self-contained does not work with --mathjax and have accepted that limitation, though I would like to request a halfway measure.

Is there an option to URL encode all the images and CSS while still forcing mathjax to link to external files?

That way my HTML document can contained almost all the necessary files and be easier to pass around or post on a webpage. Is this currently possible or easy to implement?

Seconding Jared's request. I don't mind pulling stuff off of CDNs, but I'd like to have a distributable HTML file that includes embedded images, which Pandoc is amazing for.

There is a hack you can do to get everything self-contained except for MathJax. Put the following code into your default.html template:

<script type="text/javascript">
  var fileref=document.createElement('script')
  fileref.setAttribute("type","text/javascript")
  fileref.setAttribute("src", "http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML")
  document.getElementsByTagName("head")[0].appendChild(fileref)
</script>

I surrounded it in a $if(mathjaxcdn)$...$endif$ block and activate it by setting a key later, but that's not necessary. Then you run pandoc --self-contained --mathjax=""

The idea is that you dynamically insert the call to load MathJax AFTER pandoc does its javascript muckery, you prevent pandoc from inserting its own MathJax.js, and you tell pandoc to format math for MathJax.

I guess you could also put that script in a file (dynoload.js) and then pass that to pandoc (pandoc --self-contained --mathjax=dynoload.js). Maybe.

SORRY I got mixed up with -s (--standalone). I'll not delete my comment because this still might be useful for somebody wanting to create a presentation/document that has all files locally installed.


I don't see what the problem is (except that the approach I'm using is very verbose).

Setup: clone Reveal.js and MathJax (actually I have them as submodules), and then create an example.md

% My Talk

# Stuff

* blah
* $f(x)=\sum_{n=0}^\infty\frac{f^{(n)}(a)}{n!}(x-a)^n$

and type

 pandoc --mathjax=MathJax/MathJax.js?config=TeX-AMS-MML_HTMLorMML -t revealjs -s example.md -o example.html

and I seem to have both revealjs and mathjax running from my local copies, so should work offline.

I understand that reveal.js tries to pull in mathjax automatically as an async plugin, I've not confirmed that it is not trying to use the CDN instead of my local copy.

I think there could be a self-contained document with mathjax:

What about letting mathjax render the math-blocks (i.e. as svg) when the document is created and embed only the rendered blocks, not mathjax itself?

@balachia Putting your script into an js-file and pass it with --mathjax=dynoload.js works, not nice but at least it works...

@jgm is it still impossible with the current state of Pandoc for --self-contained to not work with --mathjax?

I can't think of a way to get --self-contained to work properly with --mathjax, so this bit of documentation will have to do.

@jaredlander's comment is ideal for me:

I understand that --self-contained does not work with --mathjax and have accepted that limitation, though I would like to request a halfway measure.

Is there an option to URL encode all the images and CSS while still forcing mathjax to link to external files?

That way my HTML document can contained almost all the necessary files and be easier to pass around or post on a webpage. Is this currently possible or easy to implement?

Kyle Barron notifications@github.com writes:

@jgm is it still impossible with the current state of Pandoc for --self-contained to not work with --mathjax?

Nothing has changed in that respect. The problem is that
mathjax does some dynamic loading of modules.

Yes, but I think what @jaredlander and I desire is to be permitted to have everything _but_ Mathjax be self-contained.

For example, I want to distribute notes with image attachments that were created on a private server. Having the images encoded in the HTML document is great because I can't expect the recipients to have access to that private server, but I can expect them to have access to the internet and Cloudflare's Mathjax CDN.

So it would be ideal for images to be Base64 encoded and for CSS stylesheets included, but for the Mathjax CDN external linking to still be there.

I do most of my pandoc conversions using rmarkdown now. The way they handle this is they preprocess the document (not with pandoc I believe) to URI encode and embed everything (pictures, css, JavaScript) then does a second pass with pandoc to convert to the final format. Would be awesome to do a similar two-pass with pandoc.

Probably best not to comment on closed issues.
If you want to propose a specific change, then a new issue would be appropriate. Or better, bring it up on pandoc-discuss first.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

timtroendle picture timtroendle  路  3Comments

tolot27 picture tolot27  路  5Comments

cnblogs-dudu picture cnblogs-dudu  路  5Comments

johnridesabike picture johnridesabike  路  4Comments

acate picture acate  路  3Comments