MathJax load package

234 views
Skip to first unread message

Kuo Kan Liang

unread,
Sep 30, 2016, 5:15:20 AM9/30/16
to knitr
Hi, all,
I use math as well as chemistry in my web pages created with R Markdown. Therefore I wish to have mhchem package loaded with MathJax all the time so that I do not have to say "\require{mhchem}" everywhere. In MathJax documentation, it explains something about loading extension when MathJax is loaded. http://docs.mathjax.org/en/latest/tex.html#tex-and-latex-extensions
Nevertheless, I suppose that this has to be done by knitr instead of myself. Moreover, by inserting the javascript to load extension, as described by the previous documentation page, it does not work.
Is there an instruction of how to add mathjax options in the header of an RMD file? Thanks!

Yihui Xie

unread,
Sep 30, 2016, 11:09:48 AM9/30/16
to Kuo Kan Liang, knitr
I don't know what exactly you tried (you should provide a minimal,
self-contained, and reproducible example), but this should work:

<script type="text/x-mathjax-config">
MathJax.Hub.Config({
TeX: {extensions: ["mhchem.js"]}
});
</script>

Regards,
Yihui
--
Yihui Xie <xiey...@gmail.com>
Web: http://yihui.name
> --
> You received this message because you are subscribed to the Google Groups
> "knitr" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to knitr+un...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Kuo Kan Liang

unread,
Oct 2, 2016, 11:02:54 PM10/2/16
to knitr
I am sorry but I cannot reply after Xie's message. I am not familiar with Google groups.

Sorry for being not quite clear. I did try to add scripts to include extensions. It works, if I work on the html file directly. Nevertheless, I guess one of the philosophy behind R Markdown is that one works on the Markdown script instead of on the html file. Therefore, I added the same script in the Markdown file, right below the header part of the R Markdown. The script appears in the html file, somewhere in the middle. However, mhchem does not work, this way. I suppose that while converting the Markdown to html, the script was not put at the right place. I was wondering if one can add such settings in the title, just like:
    output:
       html_document:
          mathjax:
            ....[some mathjax settings].....

I wonder if things can work this way. Thanks again for the help.

Best regards,
Kuo Kan Liang

Kuo Kan Liang於 2016年9月30日星期五 UTC+8下午5時15分20秒寫道:

Kuo Kan Liang

unread,
Oct 3, 2016, 8:39:53 PM10/3/16
to knitr
Thank you, Mr. Xie,
I tried the script you gave me. It is simpler than other example scripts I found for this problem, and your version works. Much appreciation.
Sincerely,
Kuo Kan Liang

Kuo Kan Liang於 2016年9月30日星期五 UTC+8下午5時15分20秒寫道:
Hi, all,

Qiang Chen

unread,
May 3, 2017, 3:01:27 PM5/3/17
to knitr, kuokan...@gmail.com
Yihui,
    Thanks for you response, I also have the similar issue. I made it in R markdown by adding your script, but mhchem extension still does not work in bookdown, I tried to add the script in the book page and index.rmd in bookdown, both do not work. I tried to search in google with keyword "bookdown + mhchem", no luck.
Thank you.

Yihui Xie

unread,
May 3, 2017, 3:54:18 PM5/3/17
to Qiang Chen, knitr, Kuo Kan Liang
Could you provide a reproducible example? You can make one based on
https://github.com/yihui/bookdown-minimal

Regards,
Yihui
--
https://yihui.name

Qiang Chen

unread,
May 3, 2017, 6:00:57 PM5/3/17
to Yihui Xie, knitr, Kuo Kan Liang
Yihui,
   I finally made it by adding the script in the beginning of index.rmd of bookdown. If I add it in the end of index.rmd, it dose not work.
Although I do not know why, maybe it is due to my poor background in R. I started to learn how to use bookdown last night. 

On Wed, May 3, 2017 at 4:57 PM Qiang Chen <chenqia...@gmail.com> wrote:
Yihui,
     Thanks, I made one based on your sample as the attached zip, and added the script for mhchem you mentioned, however it does not work. And I used the same script in regular Rmarkdown (the second attachment), it works very well and shows me the right format of H2O.

Qiang Chen

Qiang Chen

unread,
May 3, 2017, 7:01:23 PM5/3/17
to Yihui Xie, knitr, Kuo Kan Liang
I have a new issue.

After I added the following script in the beginning of index.rmd,

<script type="text/x-mathjax-config"> 
MathJax.Hub.Config({ 
  TeX: {extensions: ["mhchem.js"]} 
}); 
</script> 

I can load the mhchem extension, which is good. 
But when I try build book, I got failed information as follow:
==> rmarkdown::render_site(encoding = 'UTF-8')

Error in rmarkdown::render_site(encoding = "UTF-8") : 
  No site generator found.
Execution halted
 
Exited with status 1.

If I remove the script, it runs smoothly.

Please help check the attachment as an example.
Thanks.
bookdown-minimal-master 4.zip

Yihui Xie

unread,
May 3, 2017, 10:11:18 PM5/3/17
to Qiang Chen, knitr
It seems you lack some basic knowledge about R Markdown. YAML metadata
should start from the very beginning of a Markdown document. There
should not be anything before it.

Regards,
Yihui
--
https://yihui.name


Qiang Chen

unread,
May 5, 2017, 11:13:33 AM5/5/17
to Yihui Xie, knitr
Yihui,
    Yes, I do lack of basic knowledge. Still thanks for your help.
I am wondering whether I could enable the support of mhchem package in any online markdown editor like stackedit, since I know that stackedit support MathJax and mhchem is a thrid-party extension for MathJax.
If yes, how can I make it?
I am more like a end-user, just want to use stackedit or bookdown as a tool to finish my writing online and publish in the future.

ps, introduction to mhchem from its website:

mhchem is a third-party extension for MathJax. For information on how to load the extension and make the \ce command available, see the official MathJax docs. In short, use this config:

MathJax.Ajax.config.path["mhchem"] =
  "https://cdnjs.cloudflare.com/ajax/libs/mathjax-mhchem/3.1.0";
MathJax.Hub.Config({
  TeX: {
    extensions: ["[mhchem]/mhchem.js"]
  }
});

Reply all
Reply to author
Forward
0 new messages