How to define and use macro in Mathjax?

125 views
Skip to first unread message

joh...@gmail.com

unread,
Nov 18, 2018, 3:33:24 AM11/18/18
to MathJax Users
I often use vector operation, and normally vector is written by Bold font, e.g.

    $$ \boldsymbol x = \boldsymbol a \times \boldsymbol b + \boldsymbol c $$

which is somehow too long, so I like to define some new commands \bx, \ba, \bb first, 

    $$ 
    \newcommand{\bx}{\boldsymbol x} 
    \newcommand{\ba}{\boldsymbol a} 
    \newcommand{\bb}{\boldsymbol b}
    \newcommand{\bc}{\boldsymbol c} 
    $$

then above equation can be written shortly as:

    $$ \bx = \ba \times \bb + \bc $$


----------

Because I use these Bold fonts so often, I don't want to type them time by time, I plan to define them as macro in a file: boldfont.js , when I need to type vector, I just require boldfont.js.

I write file as follows (save the file as: /config/TeX/boldfont.js, other file (such as color.js) under the same directory):

    MathJax.Hub.Config({
    TeX: {
    Macros: {
    ba: '{\\boldsymbol a}',
        bb: '{\\boldsymbol b}',
    bc: '{\\boldsymbol c}',
      bd: '{\\boldsymbol d}',
        be: '{\\boldsymbol e}',
    bf: '{\\boldsymbol f}',
    bg: '{\\boldsymbol g}',
    bh: '{\\boldsymbol h}',
    bi: '{\\boldsymbol i}',
    bj: '{\\boldsymbol j}',
    bk: '{\\boldsymbol k}',
    bl: '{\\boldsymbol l}',
    bm: '{\\boldsymbol m}',
    bn: '{\\boldsymbol n}',
    bo: '{\\boldsymbol o}',
    bp: '{\\boldsymbol p}',
    bq: '{\\boldsymbol q}',
    br: '{\\boldsymbol r}',
    bs: '{\\boldsymbol s}',
    bt: '{\\boldsymbol t}',
    bu: '{\\boldsymbol u}',
    bv: '{\\boldsymbol v}',
    bw: '{\\boldsymbol w}',
    bx: '{\\boldsymbol x}',
    by: '{\\boldsymbol y}',
    bz: '{\\boldsymbol z}',
      }
        }
    });
  
And I try to use the file (using \require command) as

    $$ 
    \require{boldfont} 
    \bf=\bu+\bv-\bw
    $$

But it doesn't work, what's wrong? How to define macro and use it?

Help me, please.


20181118094337.png

Davide Cervone

unread,
Nov 19, 2018, 3:56:15 PM11/19/18
to mathja...@googlegroups.com
The TeX configuration block is read when the TeX input jax is first loaded, so if you call MathJax.Hub.Config() after that, the changes you make will not be seen by the TeX input jax.  So any macros you add that way will not have an effect.

Instead you should use

    MathJax.InputJax.TeX.Macro('bx', '\\boldsymbol{x}');
    MathJax.InputJax.TeX.Macro('ba', '\\boldsymbol{a}');
    ...

If you have a macro that takes arguments, you can add a third parameter that is the number of arguments needed.  E.g.

    MathJax.InputJax.TeX.Macro('bs', '\\boldsymbol{#1}', 1);


The file should be stored in the MathJax/extensions/TeX folder (not MathJax/config/TeX), and if you call it boldfont.js, then at the end of the file, you need to add the line

MathJax.Ajax.loadComplete('[MathJax]/extensions/TeX/boldfont.js');

With those changes, I think you should be able to get it to work.  If not, check your console log for messages, and also use

MathJax.Message.Log()

to see if there are any file-load failures listed.

Davide



--
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/d/optout.
<20181118094337.png>

joh...@gmail.com

unread,
Nov 19, 2018, 10:49:07 PM11/19/18
to MathJax Users
Dear Davide,

Great thanks to you, it really works.

Another two mini questions:

1. it seems take some time to interpret boldfont.js. 
is it the reason of so many (26)    MathJax.InputJax.TeX.Macro(...)   ?  is it possible to put them inside one   MathJax.InputJax.TeX.Macro(... ,  ...  ,   ... ) to improve the speed?

2. I normally use 

<script type="text/javascript" async
</script>

to download MathJax.js from other site for the fast speed.

now my extensions boldfont.js can only be saved my local site. how to write the script in order to require boldfont.js, 
such that 

<script type="text/javascript" async
  src="/mathjax/extensions/TeX/...?  xxxxxxxxxxx">
</script>

I want to require boldfont.js when it is necessary, not load boldfont.js all the time.

Give me some more instruction. 

With best regards,

John 


Message has been deleted
Message has been deleted

Peter Krautzberger

unread,
Nov 21, 2018, 5:47:32 AM11/21/18
to mathja...@googlegroups.com
> it seems has one error, see screenshot,

You misspelled MathJax as Mathjax in the code (check what Davide wrote).



Am Mi., 21. Nov. 2018 um 11:08 Uhr schrieb <joh...@gmail.com>:
Dear Davide,

I find your method works ,but the speed is too slow, and it seems has one error, see screenshot,

20181121175855.png

Thanks,

John


On Tuesday, November 20, 2018 at 4:56:15 AM UTC+8, Davide Cervone wrote:
The TeX configuration block is read when the TeX input jax is first loaded, so if you call MathJax.Hub.Config() after that, the changes you make will not be seen by the TeX input jax.  So any macros you add that way will not have an effect.

Instead you should use

    MathJax.InputJax.TeX.Macro('bx', '\\boldsymbol{x}');
    MathJax.InputJax.TeX.Macro('ba', '\\boldsymbol{a}');
    ...

If you have a macro that takes arguments, you can add a third parameter that is the number of arguments needed.  E.g.

    MathJax.InputJax.TeX.Macro('bs', '\\boldsymbol{#1}', 1);


The file should be stored in the MathJax/extensions/TeX folder (not MathJax/config/TeX), and if you call it boldfont.js, then at the end of the file, you need to add the line

MathJax.Ajax.loadComplete('[MathJax]/extensions/TeX/boldfont.js');

With those changes, I think you should be able to get it to work.  If not, check your console log for messages, and also use

MathJax.Message.Log()

to see if there are any file-load failures listed.

Davide


Davide Cervone

unread,
Nov 21, 2018, 7:49:36 AM11/21/18
to mathja...@googlegroups.com
A Peter points out, you have misspelled "MathJax" as "Mathjax" (as the error message indicates).  That means that the last command is never executed.  It is the one that tells MathJax that your extension has completed running, and so MathJax doesn't know that it has.  MathJax waits for that signal before proceeding, but has a 10 second timeout, so if the signal doesn't come in 10 seconds, it gives up waiting and goes on.  Since the other commands DID run, the macros actually have been defined, so your output is what you want, but there is a 10 second delay due to the typo in your file.  That is the source of the extra time that you are complaining about. If you fix that typo, that should eliminate the timing issue.

Davide


On Nov 21, 2018, at 5:47 AM, Peter Krautzberger <pe...@krautzource.com> wrote:

> it seems has one error, see screenshot,

You misspelled MathJax as Mathjax in the code (check what Davide wrote).



Am Mi., 21. Nov. 2018 um 11:08 Uhr schrieb <joh...@gmail.com>:
Dear Davide,

I find your method works ,but the speed is too slow, and it seems has one error, see screenshot,

Davide Cervone

unread,
Nov 21, 2018, 8:34:59 AM11/21/18
to mathja...@googlegroups.com
1. it seems take some time to interpret boldfont.js. 
is it the reason of so many (26)    MathJax.InputJax.TeX.Macro(...)   ?  is it possible to put them inside one   MathJax.InputJax.TeX.Macro(... ,  ...  ,   ... ) to improve the speed?

My other message to you explains why this is happening (due to a typo in your boldfont.js file), so I think fixing that will take care of the issue for you.

2. I normally use 

<script type="text/javascript" async
</script>

to download MathJax.js from other site for the fast speed.

now my extensions boldfont.js can only be saved my local site. how to write the script in order to require boldfont.js, 
such that 

<script type="text/javascript" async
  src="/mathjax/extensions/TeX/...?  xxxxxxxxxxx">
</script>

I want to require boldfont.js when it is necessary, not load boldfont.js all the time.

The \require{} macro in TeX only allows loading extensions from the source MathJax directory, for security reasons, so you can't use \require{} to load your macros from another server.

But you can have a local configuration file that you load from your own server even when you load MathJax from a CDN.  See


for details.  So your boldfont.js could be used as a configuration file that you include only on pages that need it.


but you will need to modify the loadComplete() call in boldfont.js in order to match the URL that is used to load the file.  So in the example above, it should be


You all also need to put

MathJax.Hub.Register.StartupHook('TeX Jax Ready', function () {
    ...
  });

around the lines that perform the Macro() calls, in order to make sure they aren't performed before the TeX jax is loaded.  The loadComplete() commands should still be the last line of the file (OUTSIDE the braces above).

Hope that does what you are looking for.

Davide



Give me some more instruction. 

With best regards,

John 



Reply all
Reply to author
Forward
0 new messages