How to define custom macros in MathJax

纵饮孤独 提交于 2019-11-30 15:18:37

问题


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.


回答1:


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, ""]
}},


来源:https://stackoverflow.com/questions/24628668/how-to-define-custom-macros-in-mathjax

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!