Defining macros for Mathjax extension

⇠ Back to Blog:Hacks

\(\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 Config.js file. In the mediawiki extension version, the file is:


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:



There are good discussions on google groups: