# MathJax custom commands

Sort of crossposted to https://math.meta.stackexchange.com/questions/3845/mathjax-custom-commands

MathJax allows the use of \newcommand in TeX. For example, this command:

\newcommand{\lorentz}[1]{\frac{#1}{\sqrt{1-\frac{v^2}{c^2}}}}

would be pretty useful in lots of situations. If this was placed somewhere in the body (AFAIK before it is called), then \lorentz{m_0} would give the expression for relativistic mass. \lorentz{1} would give the normal lorentz factor. lorentz{m_0c^2} gives us the expression for energy.

Like this, I can think up quite a lot of TeXcodes that are tedious to type, but crop up often. \frac{1}{2} and \frac{1}{\sqrt{2}} are some (smaller and less tedious) examples. A few more commands that quickly give expressions for the nabla operator, div, grad, curl would be nice as well.

Basically, is there any way that we can have a user-dictated commandset?

IMO, it's easy to code it, just keep "$$*insert commands here*$$" at the top of the <body> tag. Less easy to code would be the way to make it user-dictated, but I'm thinking something akin to the way Community Promotion Ads - 2012 works would be easy to implement, as the code is already there. Just modify it so that it takes all the mathjax in the body of an answer with enough upvotes and appends it to some div in the beginning of the body tag (set display:none just to be safe).

• PS: I think the guys at math.SE would like this as well. Is it OK to crosspost this? Mar 1 '12 at 8:21
• Any reason for math not working on the meta? It works on mathmeta.. Mar 1 '12 at 8:22
• It works only in places where people were complaining that it is not working.
– user68
Mar 1 '12 at 11:02
• I don't think we really need it here though, do we? Mar 1 '12 at 11:06
• It wouldn't hurt, but we don't have that much need for it. Mar 1 '12 at 20:49
• I'm beginning to find quite tiresome writing all those \rangles. Can we at least get \ket{x}? May 6 '12 at 12:16
• @episanty I can give you a bookmarklet that provides a keyboard shortcut for \ket{..}.. And any other thingy you can think of. Its better if you tell me which key combination for which function(alt or ctrl+key). Just make sure the markdown editor doesn't use 'em already. May 6 '12 at 14:48
• You mean something that will implement Alt+k(say)-> \ket{}? That won't render, though. The beauty of TeX commands is that they read (more or less) like equations sound in one's head. My ideal solution would be a central core of customized commands (i.e. including \ket{}), which one could then personalize on the profile page. (Though I'm aware this is getting close to the pony tag.) May 6 '12 at 15:35
• @episanty: It will be similar to how this new script works. Actually, it'll be the same--since I'll be copying the keyboard-shortcut lambdas into a bookmarklet. It will do alt-k-->|(cursor)\rangle. Remember the cursor will auto-position itself. So, the flow is like this: dollar symbol;psi;=;alt-k;1;right arrow to leave ket;+;alt-k;0;right arrow;dollar for $\psi=|1\rangle+|0\rangle$. OK, I guess it can get complicated :/ (But it fits beautifully into the flow when typing chemical equations on chem.SE, so it may work) May 6 '12 at 15:42
• @Manishearth: That overlooks the point - I hold that being able to write \ket{} and have it come out right is a valuable end in itself. Given that putting $\newcommand{\ket}[1]{|#1\rangle}$ on top of a post currently works, it shouldn't be unthinkable to define a core+personalized header that gets included on all posts, or all posts that use it. May 6 '12 at 15:59
• @episanty: I know, I was talking about a temporary solution to help you. Actually, I may think of adding such customizable custom commands script. But it'll take a while. May 6 '12 at 16:01
• @Manishearth Wow, it's been a while since this. Any chance of bras and kets in that nice script of yours? Sep 10 '13 at 12:58
• @EmilioPisanty /me has no time :/ Sep 10 '13 at 14:44
• Sorry to revive this (but as it is not fixed yet). I'd love to see a library of commonly used macros happen. Any pointers how I can help to make this happen? May 15 '15 at 23:58
• This question is not visible enough — it should be immediately discoverable by anybody who is looking for braket/Feynman/etc. notation. I had to click 3-4 links to check that this is known by the community. Should we create some questions and link them with each other? Jun 15 '16 at 23:18

If you have a set of commands you use frequently, perhaps just save $insert commands here$ in a text file and copy it into the top of your post whenever you need it (I'm not sure if that works, but it's worth a try).
If the SE people are amenable to site-specific customizations, though, I do have a LaTeX package of physics symbols that contains a few things we might consider for inclusion. These would be a limited number of highly standardized notations, like \ket{x} for |x\rangle, for instance, or \ud{y}{x} for the derivative \frac{\mathrm{d}y}{\mathrm{d}x}. It would also be fantastic to have the \SI command from siunitx, though admittedly that is a large and complex package that is probably not practical to implement in MathJaX (at least not in a way that faithfully reproduces the original typesetting, though that wouldn't necessarily be a requirement).
You face a significant problem here in that notation varies across sub-disciplines. In particular I can assure you that the Lorentz factor is $\frac{1}{sqrt{1 - v^2}}$ because everyone knows that $c$---like $G$ and $\hbar$--is a dimensionless unit constant.
• You'd only ever have to write the expression for the lorentz factor while explaining a bit of SR to a novice. And a novice won't know the notation $c=1$. In other places, $\gamma$ is fine. Mar 1 '12 at 17:23