<span class="mw-page-title-namespace">Blog</span><span class="mw-page-title-separator">:</span><span class="mw-page-title-main">Hacks/Defining macros for Mathjax extension</span>
Fabrice P. Laussy's Web

Defining macros for Mathjax extension

From laussy.org's Blog about Hacks.
Published: 15:28, 7 June 2012.

\(\newcommand{\pt}{\partial_t}\)With MathJax, you can use \newcommand in enclosed $...$ characters to define a macro in any given page (e.g., \newcommand{\pt}{\partial_t} turns $\pt$ into $\pt$). If you want a popular macro to be defined once and for all, without having to include the definition each time, you have to declare it in a javascript file, e.g., Config.js.

In the standard mediawiki extension version, the file is:

mwMathJaxConfig.js

There's already a list of definitions. The syntax is not the most obvious as it needs to translates $\mathrm{\TeX}$ syntax into javascript. Macros I use all the times are:

\newcommand{\ud}[1]{{#1^{\dagger}}}
\newcommand{\bra}[1]{\left\langle #1\right|}
\newcommand{\ket}[1]{\left| #1\right\rangle}
\newcommand\Tr{\mathrm{Tr}}

And this needs be written:

bra: ['{\\left\\langle #1\\right|}',1],
ket: ['{\\left| #1\\right\\rangle}',1],
ud: ['{#1^{\\dagger}}',1],
Tr: '\\mathrm{Tr}'

This turns, e.g., $$\ud{a}\ket{n}=\sqrt{n+1}\ket{n+1}$$ into:

$$\ud{a}\ket{n}=\sqrt{n+1}\ket{n+1}$$

For some reasons (mainly that I want to use $$ instead of <math>), I also use the SimpleMathJax extension. In this case, look for:

laussywiki/extensions/SimpleMathJax/resources/ext.SimpleMathJax.js

and add:

      Z: "{\\mathbb{Z}}",
      Zeta: "{\\mathrm{Z}}",
      // Custom macros
	  ud: ['{#1^{\\dagger}}', 1],
	  la: '{\\circlearrowleft}',
	  ra: '{\\circlearrowright}',

(it so happens that ket, bra, etc., are already defined in this case).

Links

There are good discussions on google groups: