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.
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).
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.
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
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, ""]
}},
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With