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

[liquid_tags] Inline math is not rendered with MathJax #933

Closed
drorata opened this issue Sep 3, 2017 · 25 comments · Fixed by #936
Closed

[liquid_tags] Inline math is not rendered with MathJax #933

drorata opened this issue Sep 3, 2017 · 25 comments · Fixed by #936

Comments

@drorata
Copy link
Contributor

drorata commented Sep 3, 2017

liquid_tags fails to render inline math for me. I managed to have inline math rendered properly when using "plain" markdown. See for example this example with sources available here.

However, inline math in a Jupyter notebook is not rendered properly even though it does look OK inside the Jupyter environment. An example can be found in the following places:

As you can see, Github renders the inline math properly, but when included in a post, this breaks. I failed to understand where the problem is. Any help would be appreciated.

Update 1:
It seems like the function init_mathjax() is not executed when the HTML page is loaded. If I run it manually from the page's console, the inline math is rendered properly.

Update 2: I cross posted the issue also on SO

@avaris
Copy link
Member

avaris commented Sep 3, 2017

Well, it is executed. The issue is mathjax JS is loaded with async. So, when the init_mathjax is run, mathjax JS might not be loaded (and therefore no window.MathJax) which makes the if inside init_mathjax fail and the whole function does nothing basically.

Looks like async was added recently (see #882).

@drorata
Copy link
Contributor Author

drorata commented Sep 3, 2017

That's a little confusing for me... Should I have understood the solution from @avaris comment?

@avaris
Copy link
Member

avaris commented Sep 3, 2017

@drorata Sorry if it turned out confusing. My suggestion is to remove async from this line:

<script type="text/javascript" async src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.0/MathJax.js?config=TeX-AMS_HTML"></script>

so that it becomes:

<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.0/MathJax.js?config=TeX-AMS_HTML"></script>

async tells the browser to continue loading html (and running scripts) while it's fetching this file. However the next script init_mathjax depends on this file being loaded beforehand and it'll do nothing if it's not loaded (yet). So the asnyc kind of breaks it.

@drorata
Copy link
Contributor Author

drorata commented Sep 5, 2017

This was introduced in this commit be9e98b by @isms; any idea why? Shouldn't it simply be

<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.0/MathJax.js?config=TeX-AMS_HTML"></script>

?

drorata added a commit to drorata/pelican-plugins that referenced this issue Sep 5, 2017
@isms
Copy link
Contributor

isms commented Sep 6, 2017

@drorata "any idea why" - sure, it's the exact snippet from the MathJax documentation.

@drorata
Copy link
Contributor Author

drorata commented Sep 6, 2017

@isms But it broke my rendering. What is the best practice solution?

@pkra
Copy link

pkra commented Sep 7, 2017

@dorata the problem is that the MathJax configuration is pretty weird in general. It loads MathJax, then sets the configuration, and then re-renders the page -- that's pretty inefficient and breaks when loading MathJax asynchronously (as the function to configure and re-render will never hit the if check).

It would be better to configure MathJax before it is loaded. I'm guessing the weird setup stems from the days when MathJax could not be configured easily in plain JS; that's no longer a problem, cf http://docs.mathjax.org/en/latest/configuration.html#using-plain-javascript

drorata added a commit to drorata/pelican-plugins that referenced this issue Sep 7, 2017
See getpelican#933 (comment)
for more details on the configuration of MathJax
@drorata
Copy link
Contributor Author

drorata commented Sep 7, 2017

@pkra First, nice surprise to meet here :)

Secondly, would this attempt, drorata@d8b3ef2, address the problem correctly? @isms what do you think?

@pkra
Copy link

pkra commented Sep 7, 2017

@pkra First, nice surprise to meet here :)

Nice to run into you!

Secondly, would this attempt, drorata/pelican-plugins@d8b3ef2, address the problem correctly? @isms what do you think?

That looks good; I've added some comments.

@drorata
Copy link
Contributor Author

drorata commented Sep 7, 2017

I have updated the attempt based on @pkra comment. See drorata@139eac6

@isms Any reservations?

@isms
Copy link
Contributor

isms commented Sep 7, 2017

@drorata No reservations here.

drorata pushed a commit to drorata/pelican-plugins that referenced this issue Sep 12, 2017
See: getpelican#933
and in particular getpelican#933 (comment)
for more details.

Use improved MathJax setting and maintaining the async loading. This closes getpelican#933
drorata added a commit to drorata/pelican-plugins that referenced this issue Sep 13, 2017
See: getpelican#933
and in particular getpelican#933 (comment)
for more details.

This commit replaced getpelican#935

Use improved MathJax setting and maintaining the async loading. This closes getpelican#933
drorata pushed a commit to drorata/pelican-plugins that referenced this issue Sep 13, 2017
See: getpelican#933
and in particular getpelican#933 (comment)
for more details.

This commit replaced getpelican#935

Use improved MathJax setting and maintaining the async loading. This closes getpelican#933
drorata added a commit to drorata/pelican-plugins that referenced this issue Sep 13, 2017
See: getpelican#933
and in particular getpelican#933 (comment)
for more details.

Use improved MathJax setting and maintaining the async loading. This closes getpelican#933
drorata added a commit to drorata/pelican-plugins that referenced this issue Sep 13, 2017
See: getpelican#933
and in particular getpelican#933 (comment)
for more details.

Use improved MathJax setting and maintaining the async loading. This closes getpelican#933

Fixed async loading of page breaking mathjax

See getpelican#933
for more details.
xdze2 added a commit to xdze2/xdze2.github.io-src that referenced this issue Nov 10, 2017
@twiecki
Copy link

twiecki commented Mar 12, 2018

I am up-to-date but I still see this behavior (http://localhost:8000/blog/2015/11/10/mcmc-sampling/). I also tried your branch drorata:improve-mathjax-integration-2 but it also didn't fix the mathjax inline problem. @drorata any ideas?

@benlindsay
Copy link

I'm also up to date and see the same behavior (math works in notebook but not in webpage). I even tried deleting the async from line 170 of liquid_tags/notebook.py as mentioned above and that didn't fix it. Any ideas of what to do?

@drorata
Copy link
Contributor Author

drorata commented Mar 13, 2018

@twiecki Have you looked up my blog's sources? Maybe you can compare and find out the difference. In this example I believe everything is rendered correctly.

@yamalcaraz
Copy link

Hi, I am also having this problem.

I tried recreating your blog on my local computer by cloning this. I also cloned your version of pelican-plugins. But it seems that on my computer, the inline maths do not render as expected. I am not sure why this is happening. Can you help?

image

@drorata
Copy link
Contributor Author

drorata commented Mar 13, 2018

@yamalcaraz Are you sure you're using the same version of pelican?

@yamalcaraz
Copy link

Yes, I am sure, I am using pelican 3.7.1. I also tried using your environment.yml, but still does not work...

image

@twiecki
Copy link

twiecki commented Mar 13, 2018

For some odd reason, even though I'm quite sure I am up-to-date on pelican-plugins, the ipynb file was not up-to-date. I downloaded it manually (after @drorata's fix) and replaced it and it's working fine now.

@yamalcaraz
Copy link

Hi @twiecki, I'm not sure which ipynb file you are talking about, I searched the pelican-plugins for an *.ipynb file but I found only some ipython notebooks for testing. I am sorry I am just a beginner at building a website. Thank you for your help!

image

@twiecki
Copy link

twiecki commented Mar 13, 2018

@benlindsay
Copy link

benlindsay commented Mar 13, 2018

Wow, the same thing worked for me! Doing a diff of my notebook.py file and @drorata's, the only non-whitespace difference was on what for me was line 180. I had

displayAlign: 'left', // Change this to 'center' to center equations.

and @drorata had

displayAlign: 'center', // Change this to 'center' to center equations.

When I changed 'left' to 'center' that fixed the problem. Any idea why that would affect inline math? I don't have any standalone equations to center or align, just some inline math expressions. Anyway as long as it works I'm happy.

Thanks!

EDIT:
To clarify, I had also previously removed the async as mentioned above, but that wasn't enough. That in combination with the 'left' to 'center' change did the trick.

@yamalcaraz
Copy link

@twiecki I see, but I think I am also using the one fixed by @drorata correctly. See image below, the "async" was removed from the line as in (drorata@d8b3ef2), so it is supposed to work, but still does not on my machine. Tell me if there are more details that I can provide that can help. Thanks!

image

@yamalcaraz
Copy link

Ok, I found out about my problem, it seems that my problem was not about the async call nor the center. It was another issue about my theme pelican-bootstrap3. It turns out that the theme was using an older version of the mathjax source
src="https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML"
which should be:
src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.0/MathJax.js?config=TeX-AMS_HTML"

I fixed it in my fork of pelican-themes here.

Anyway thanks for the help!

@twiecki
Copy link

twiecki commented Mar 13, 2018

@yamalcaraz Please send a PR.

@yamalcaraz
Copy link

Ok, this will be actually my first pull request. I hope it helps!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants