Load text/x-mathjax-config from file?

858 views
Skip to first unread message

tomas.aken...@gmail.com

unread,
Jan 9, 2013, 4:02:02 AM1/9/13
to mathja...@googlegroups.com
Hi all,

How do I put the stuff inside <script type="text/x-mathjax-config"> into a file?

I.e., I would like to 
<script type="text/x-mathjax-config" src="mymathjaxhubconfig.txt"></script>
and then put my "MathJax.Hub.Config( ...);" stuff into the mymathjaxhubconfig.txt-file.

I tried to to exactly this, put it does not work. Could anyone help?

/Tomas

Peter Krautzberger

unread,
Jan 10, 2013, 1:34:41 AM1/10/13
to mathja...@googlegroups.com
Here's a relevant part of our documentation  http://docs.mathjax.org/en/latest/configuration.html#using-a-configuration-file -- most of it is at the end of that section.

The often overlooked part is to make sure that the path in the last line of your configuration file matches the path used for MathJax.js?config=...

Peter.

tomas.aken...@gmail.com

unread,
Jan 10, 2013, 3:07:51 AM1/10/13
to mathja...@googlegroups.com
Thanks, Peter!

I assume you proposed that I create a new myconfig.js file and load it like this:
<script type="text/javascript" src="../../mathjax/MathJax.js?config=myconfig"></script>

However, I'm very happy to use the TeX-AMS-MML_HTMLorMML config file that is provided, so
what I would like to do is the following:

<script type="text/x-mathjax-config" src="../../mathjax/config/myconfig.js"></script>
<script type="text/javascript" src="../../mathjax/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>

where the myconfig.js file may contain (just an example):

MathJax.Hub.Config(
{
    tex2jax: {inlineMath: [['$','$'], ['\\(','\\)']]},
    TeX:
    {
        Macros:
        {
            T:  ["\\mathrm{T}",0],
            R:  ["\\mathbb{R}",0],
            // and more here...
        }
    }
});

Is there a way to get that working?

Thanks in advance!

/Tomas

Davide P. Cervone

unread,
Jan 10, 2013, 6:55:35 AM1/10/13
to mathja...@googlegroups.com
You can use BOTH the main configuration file and your local one via

<script src="../../mathjax/MathJax.js?config=TeX-AMS-MML_HTMLorMML,myconfig"></script>

if you put myconfig.js in the mathjax/config directory.  It should contain

MathJax.Hub.Config(
{
    tex2jax: {inlineMath: [['$','$'], ['\\(','\\)']]},
    TeX:
    {
        Macros:
        {
            T:  ["\\mathrm{T}",0],
            R:  ["\\mathbb{R}",0],
            // and more here...
        }
    }
});
MathJax.Ajax.loadComplete("[MathJax]/config/myconfig.js");

See the file mathjax/config/local/local.js for an example.

Note that you can also do this sort of thing even if you are using the CDN.  In that case, your configuration file will be on another server where you have access to edit files, and you will need to give the complete URL to your configuration file (both in the config= line, and in the loadComplete() call).

Davide

Peter Krautzberger

unread,
Jan 10, 2013, 12:11:48 PM1/10/13
to mathja...@googlegroups.com

Yes, it is. That's in the very next section. Just pass a comma separated list after config?...

tomas.aken...@gmail.com

unread,
Jan 11, 2013, 2:57:22 AM1/11/13
to mathja...@googlegroups.com
Davide,

This works fine! Thanks a lot! And thanks Peter, for pointing me to the right place in the manual.

/Tomas

michae...@gmail.com

unread,
Sep 3, 2013, 2:40:56 AM9/3/13
to mathja...@googlegroups.com, tomas.aken...@gmail.com
What if I cannot put the custom config file on a server? I'm using MathJax in local html files (auto-generated documentation for a software project). I'd like to define some additional TeX-macros that are used in all the html files. Therefore, it would be extremely convenient to reference a local config file giving a relative path, just like the original poster suggested. Would anything like that be possible?

Thanks,
Michael

Peter Krautzberger

unread,
Sep 3, 2013, 1:11:48 PM9/3/13
to mathja...@googlegroups.com, tomas.aken...@gmail.com
Hi Michael,

Yes, that will work just as well.

Peter.


--
You received this message because you are subscribed to the Google Groups "MathJax Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mathjax-user...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

michae...@gmail.com

unread,
Sep 3, 2013, 2:50:11 PM9/3/13
to mathja...@googlegroups.com, tomas.aken...@gmail.com
It will?

I had this in my header:

<script type="text/x-mathjax-config">
MathJax.Hub.Config({
tex2jax: {inlineMath: [['$','$'], ['\\(','\\)']]},
TeX: { Macros: {
avg: ['{\\left<#1\\right>}', 1],
bra: ['{\\left<#1\\right|}', 1],
ket: ['{\\left|#1\\right>}', 1]
} }
});
</script>
 
and tried to replace it with

<script type="text/x-mathjax-config" src="../mathjax_config.js"></script>

where ../mathjax_config.js now contains exactly the code that was between the  initial <script> tags. In the second example the configuration no longer seems to be used, \avg etc are no longer expanded.

Peter Krautzberger

unread,
Sep 3, 2013, 2:54:33 PM9/3/13
to mathja...@googlegroups.com, tomas.aken...@gmail.com
Hi,
You still have to follow the documentation on how to write a custom configuration file

You're probably missing the line starting with MathJax.Ajax.loadComplete.
Peter.

michae...@gmail.com

unread,
Sep 3, 2013, 3:08:10 PM9/3/13
to mathja...@googlegroups.com, tomas.aken...@gmail.com
Indeed I was wondering about that. But what would I put in as an argument, since I only have a relative URL (the site being simple local HTML files, not hosted on a server)?

My mathjax_config.js now looks like this:

MathJax.Hub.Config({
tex2jax: {inlineMath: [['$','$'], ['\\(','\\)']]},
TeX: { Macros: {
avg: ['{\\left<#1\\right>}', 1],
bra: ['{\\left<#1\\right|}', 1],
ket: ['{\\left|#1\\right>}', 1]
} }
});
MathJax.Ajax.loadComplete("mathjax_config.js");

which also doesn't seem to do anything (neither with "../mathjax_config.js"

Peter Krautzberger

unread,
Sep 3, 2013, 3:22:18 PM9/3/13
to mathja...@googlegroups.com, Tomas Akenine-Möller
This really depends a lot on your setup. Often a complete URL works best (in your case starting with file://...). 

You might want to use a local copy of MathJax and just keep your file in the config folder -- and follow the example of the combined configuration files.

Peter.

michae...@gmail.com

unread,
Sep 3, 2013, 3:36:10 PM9/3/13
to mathja...@googlegroups.com, Tomas Akenine-Möller
OK. Neither of these options is very appealing ;-)

The "setup" in my case is the the doc folder of a software project, with auto-generated HTML files, for the user to be read "offline" (well, online for loading from the MathJax CDN). I was using a full local copy of MathJax before, but I wanted to slim down the doc folder as much as possible and use the MathJax CDN instead. Using file://.. URLs is out because I don't know in advance where the doc folder will end up on the user's hard drive.

I think what I'll do for now is have a perl script run over the generated HTML files as part of the Make process, look for <script type="text/x-mathjax-config" src="../mathjax_config.js"></script> lines and insert the content of the referenced file directly inline. Maybe loading a config file using only relative URLs is something I could hereby submit as a feature request for future versions of MathJax.

Thanks for you help!
Michael

Davide P. Cervone

unread,
Sep 3, 2013, 4:16:13 PM9/3/13
to mathja...@googlegroups.com, Tomas Akenine-Möller
You can't use the SRC attribute on a script of type text/x-mathjax-config (or any type other than type text/javascript or its variants); the browser will not load the script in that case.

Here is one way to accomplish your goal, however: use the delayStartupUntilConfigured option on the script that loads MathJax.js, and then use a regular <script> right after that to load the configuration script, then add MathJax.Hub.Configured() to the end of that external configuration file.

For example:

<!DOCTYPE html>
<html>
<head>
<title>Test local config with CDN</title>
<script src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML&delayStartupUntilConfig"></script>
<script src="local.js"></script>
</head>
<body>
$x+1$
</body>
</html>

where local.js (in the same directory) consists of

MathJax.Hub.Config({
tex2jax: {
inlineMath: [['$','$'],['\\(','\\)']]
}
});
MathJax.Hub.Configured();

That should allow you to put the local.js file anywhere on the disk, and still use the CDN for MathJax.

If you were using a local copy of MathJax, Peter would be right, you could put the configuration file in the config directory (there is a sample in config/local/local.js), and load it along with one of MathJax's predefined configuration files:

<script src="path-to-mathjax/MathJax.js?config=TeX-AMS_HTML,local/local.js"></script>

See the documentation at

http://docs.mathjax.org/en/latest/configuration.html#using-a-configuration-file

(see the last paragraph of the section).

Davide

michae...@gmail.com

unread,
Sep 3, 2013, 5:07:30 PM9/3/13
to mathja...@googlegroups.com, Tomas Akenine-Möller, dp...@union.edu
Awesome! That works perfectly! Thanks!

Victor Ivrii

unread,
Sep 3, 2013, 8:15:28 PM9/3/13
to mathja...@googlegroups.com

IMHO  < and > should not be used as left and right delimiters; use \langle and \rangle instead
Reply all
Reply to author
Forward
0 new messages