8

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.

tarikakyol
  • 513
  • 7
  • 13
  • What kind of problems are you seeing? This seems to work, see http://codepen.io/pkra/pen/AqdGs. (IIRC, the [Braket package](http://ctan.org/pkg/braket) is more complicated though.) – Peter Krautzberger Jul 08 '14 at 17:51
  • @Peter Krautzberger I want to define that macro in Mathjax settings instead of adding "\newcommand{\Abs}.." to every single HTML file. – tarikakyol Jul 10 '14 at 12:04

1 Answers1

9

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, ""]
}},
Peter Krautzberger
  • 5,145
  • 19
  • 31
  • thanks Peter it works with the third empty parameter in the array. I'm curious where you got that information from, I couldnt manage to find it on MathJax docs. – tarikakyol Jul 11 '14 at 08:28
  • 1
    I admit I had to dive into the code of a few TeX extensions. I filed an issue on our docs https://github.com/mathjax/MathJax-docs/issues/73 – Peter Krautzberger Jul 11 '14 at 16:24