Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define custom macros in MathJax

I'm trying to define custom macros used in LaTeX files in MathJax.

Can define simple macros (single parameter) without any issue such as;

\newcommand{\braket}[1]{\langle #1 \rangle}

as

Macros: {
    braket: ['{\\langle #1 \\rangle}', 1]
}

But struggle with complicated ones;

\newcommand{\Abs}[2][]{\left\lvert#2\right\rvert_{\text{#1}}}

trying to define it like;

Macros: {
    Abs: ['{\\left\\lvert#2\\rvert_{\\text{#1}}}', 2]
}

but no luck.

This is how it is used in LaTeX file

\begin{align}\nonumber
    p_e = \Abs{\braket{e|\psi(t)}}^2 = \sin^2\Omega t\, .
\end{align}

Not sure where I did wrong.

I'm not a LaTeX expert, but just a developer trying to display LaTeX files on a web app (for Quantum Physics community), so I would greatly appreciate your help. thanks.

P.S this question was asked and closed on SE they redirected me to SO.

like image 728
tarikakyol Avatar asked Jul 08 '14 09:07

tarikakyol


People also ask

How do I create macros and environments in MathJax?

You can use the \def, ewcommand, enewcommand , ewenvironment, enewenvironment, and \let commands to create your own macros and environments. Unlike actual TeX, however, in order for MathJax to process such definitions, they must be enclosed in math delimiters (since MathJax only processes macros in math-mode).

Why does MathJax require math delimiters?

Unlike actual TeX, however, in order for MathJax to process such definitions, they must be enclosed in math delimiters (since MathJax only processes macros in math-mode). For example would define \RR to produce a bold-faced “R”, and \bold {...} to put its argument into bold face.

How do I add macros to my Tex block?

You can include macro definitions in the macros section of the tex blocks of your configuration, but they must be represented as javascript objects. For example, the two macros above can be pre-defined in the configuration by


1 Answers

I've updated the codepen from my comment.

Primarliy, you forgot a \\right; I also modified your macro definition so that it has an optional parameter. In other words, something along the lines of:

Macros: {
    braket: ['{\\langle #1 \\rangle}', 1],
   Abs: ['\\left\\lvert #2 \\right\\rvert_{\\text{#1}}', 2, ""]
}},
like image 143
Peter Krautzberger Avatar answered Oct 26 '22 17:10

Peter Krautzberger