When writing quantum mechanics answers I typically start with something like this at the top:

$\renewcommand{ket}[1]{\left| #1 \right\rangle}$

Using a custom macro like this means that I can type \ket{\Psi} instead of \left| \Psi \right\rangle when I want to indicate a quantum state. This has several benefits

  • Less typing for me, and I'm less likely to make a mistake.
  • Easier for other people to edit my post because it's easier for them to read the TeX.
  • Other folks editing the TeX may learn something new about TeX!

Is it appropriate to add a note explaining the use of \renewcommand (or \newcommand) to the help center page on MathJax?

  • 1
    $\begingroup$ Maybe the accepted answer should be reconsidered in light of these recent changes. The scopes of \newcommand and \renewcommand are now restricted to the post they appear in. $\endgroup$ – Emilio Pisanty Apr 1 '20 at 12:56

Note: This answer is outdated, and does not reflect the current behaviour of the Stack Exchange software. Since January 2019, the MathJax environment is limited to every individual post instead of the complete question-and-answer thread. As such, the concerns voiced in this answer are no longer a problem.

Absolutely not.

In fact, \renewcommand really shouldn't be used at all.

Why? Because the title, question, answers, and comments on a page are not in separate MathJax environments. If you use \renewcommand (or even \newcommand, though that's not quite as bad) you risk clobbering others' contributions. All of a sudden their text will be rendered differently from what they were shown in the preview.

Even if you check that no one else has used the command, you've silently altered the MathJax environment for any future posters. Imagine the frustration from a user unfamiliar with this feature when faced with their answer giving rendering errors in one place that are not reproduceable elsewhere.

\renewcommand in a multi-user, evolving environment is the equivalent of re#defineing key parts of a language in a public header file. Anyone who loads the file will be unknowingly using your personal variant of the language rather than the standard one.

If you want \ket to have some particular meaning, the first step is to have this as a MathJax package (I don't know whether or not this exists), and the second step is to convince the SE team to automatically load this package on physics pages. The other option is to have the team somehow implement MathJax in isolated environments, one per post. (I don't even know if this is feasible.)

Otherwise, a little verbosity never hurt anyone. Besides, I'm not sure adding another layer of obfuscation via macros is the best way to help newcomers learn a language. It seems more like giving them a fish ("here, just use this header; don't ask what's in it") than teaching them to fish.

On the side, I don't think the proposed definition of \ket is itself bad (aside from the erroneous use of | rather than \lvert...). It's just that this sets a precedent for others to develop more and more complicated macros, probably just copying and pasting from an ever-growing file of personal preferences with more and more ways to potentially break things.

  • $\begingroup$ If anything this answer highlights that the mathjax script is not particularly well suited to SE's pages. The fact that there's one global environment already kind of bad, but the fact that we can change that environment is really bad. I've actually used this to prank the chat before be redefining \int :) Anyway, it sounds like there's a deeper issue here that should be discussed. $\endgroup$ – DanielSank Aug 2 '15 at 15:53
  • 1
    $\begingroup$ Indeed, it seems like this is a pretty dangerous "feature" and it would be easy to break every answer. I'm surprised MathJax took this route since LaTeX allows local macro definitions in math mode which do not persist outside that scope. $\endgroup$ – tpg2114 Aug 2 '15 at 18:00
  • 1
    $\begingroup$ @tpg2114 Never, ever underestimate the crappiness of software written for the web. I'm not saying mathjax authors are crappy or lazy programmers, but the web has evolved as hacks upon hacks so doing the Right Thing is some times so difficult that even good programmers produce crappy web software. $\endgroup$ – DanielSank Aug 2 '15 at 18:11
  • $\begingroup$ @DanielSank Indeed there is a deeper issue. Unfortunately, my knowledge of computers begins and ends at low-level languages, and I have no notion of how feasible sandboxing different posts on the same page would be. And to reiterate, it's not your redefinitions I'm worried about, but rather some other hypothetical person's. $\endgroup$ – user10851 Aug 2 '15 at 20:58
  • 2
    $\begingroup$ Case in point: OP on this post redefined \vec to be bold face, rather than the normal over-arrow (see the source page). IDK if Qmechanic tried using \vec in place of the \bf instead, but seems to be a bad choice here. $\endgroup$ – Kyle Kanos Aug 3 '15 at 16:34
  • $\begingroup$ @ChrisWhite I'd be surprised if somebody hasn't already suggested that as feature request in MathJax. It certainly could be done, but I don't think it would be straightforward. $\endgroup$ – David Z Aug 3 '15 at 21:35
  • $\begingroup$ @ChrisWhite I just noticed your tex.se link. It seems that \lvert itself is also not always right, with \rvert being the correct usage for bras, and no clear candidate for matrix elements (unless \mvert is a thing?). $\endgroup$ – Emilio Pisanty Dec 10 '15 at 18:25
  • $\begingroup$ See this meta.math post for an older discussion about this issue. There is also the safe extension that was not available at the time of that post that might also be useful for SE. $\endgroup$ – Davide Cervone Dec 10 '15 at 18:50
  • 1
    $\begingroup$ This answer is outdated. The scope of \newcommand and \renewcommand has been changed to only the post that calls them, removing this concern. $\endgroup$ – Emilio Pisanty Apr 1 '20 at 12:59

I'll say this here in case it's useful.

MathJax is unlike TeX in that it recognizes as valid characters a bunch of unicode junk that TeX will rightfully call out as jibberish. This includes greek letters, used to great effect on Mathematica.SE, clever sundries like ≠ and ±, and the ever useful

⟨ and ⟩.

My solution for easy bra-ket input is to have ⟨ and ⟩ hard keylinks on my keyboard (AltGr+[ and AltGr+], if you're wondering). Typing something like ⟨\phi|\psi⟩ takes more keystrokes on the meat than on the bread, which is as it should be, and it gets rendered correctly. You can argue that this is terrible form, but it works.

For an example in action see the source for this post.

  • 1
    $\begingroup$ That would make editing annoying for others. I've lost track of the number of times I've replaced unicode Greek letters and symbols with TeX. $\endgroup$ – DanielSank Aug 3 '15 at 17:12
  • 1
    $\begingroup$ If others find my source annoying to edit, that's sort of their problem. In fact, I would be pretty annoyed if someone did barge into my posts and littered the source with \langles and \rangles. (I wouldn't litter their source with ⟨s and ⟩s either.) Note that this is distinct from un-texified unicode from new users, but there's no need to \alpha their αs - simply encase them in dollar signs and let MathJax do its thing. Spending extra effort getting upset at the cleanliness of other people's source on SE sounds like a great way to shorten one's lifespan for no reason at all. $\endgroup$ – Emilio Pisanty Aug 3 '15 at 17:18
  • 1
    $\begingroup$ In fact, that's sort of the point of markdown: having a document with a readable source which will compile into something that looks nicer. There's nothing to be gained by making the source uglier if the rendered version looks exactly the same. $\endgroup$ – Emilio Pisanty Aug 3 '15 at 17:19
  • 1
    $\begingroup$ Consistency matters, and if I edit someone's question (you seem to assume we're only talking about answers) you better believe I'm going to replace the unicode with TeX so that it's all consistent. This habit is formed out of years of working on multi-developer software projects. Source style matters a lot. $\endgroup$ – DanielSank Aug 3 '15 at 17:21
  • 2
    $\begingroup$ That only works on community wiki posts. Otherwise, it's still their post, and you definitely should not barge into others' posts and change their source to impose your consistency standards. This is not a multi-developer software project - it's their content, which they may go back to and edit later. The only exception is new users who have not used any TeX, but there you're changing the rendered post, for its rendered consistency with the rest of the site, not the source. $\endgroup$ – Emilio Pisanty Aug 3 '15 at 17:30
  • 2
    $\begingroup$ Then why does SE have an edit button which I can use on your posts? This is not a flip question I am now confused. $\endgroup$ – DanielSank Aug 3 '15 at 17:49
  • 1
    $\begingroup$ To allow you to edit my posts. If you do so only to trim the source to conform to your consistency standards, with no change on the rendered post, I will most likely be annoyed. Other people's sources are theirs and you should only make stylistic edits where warranted. $\endgroup$ – Emilio Pisanty Aug 3 '15 at 17:51
  • 1
    $\begingroup$ As far as consistency goes: the platform here is not TeX, it is markdown + MathJax. I also find it curious that you're being so aggressive on other people's source consistency when you started this thread with a proposal that's terrible MathJax standards. $\endgroup$ – Emilio Pisanty Aug 3 '15 at 17:53
  • $\begingroup$ (I'm signing off on this, though. It's honestly not that important.) $\endgroup$ – Emilio Pisanty Aug 3 '15 at 17:54
  • $\begingroup$ I didn't know it was a terrible idea until I asked. $\endgroup$ – DanielSank Aug 3 '15 at 17:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .