7
$\begingroup$

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).

Thoughts? Suggestions? Rants? Cookies?

$\endgroup$
16
  • 1
    $\begingroup$ PS: I think the guys at math.SE would like this as well. Is it OK to crosspost this? $\endgroup$ Commented Mar 1, 2012 at 8:21
  • $\begingroup$ Any reason for math not working on the meta? It works on mathmeta.. $\endgroup$ Commented Mar 1, 2012 at 8:22
  • $\begingroup$ It works only in places where people were complaining that it is not working. $\endgroup$
    – user68
    Commented Mar 1, 2012 at 11:02
  • $\begingroup$ I don't think we really need it here though, do we? $\endgroup$ Commented Mar 1, 2012 at 11:06
  • $\begingroup$ It wouldn't hurt, but we don't have that much need for it. $\endgroup$
    – David Z Mod
    Commented Mar 1, 2012 at 20:49
  • 3
    $\begingroup$ I'm beginning to find quite tiresome writing all those \rangles. Can we at least get \ket{x}? $\endgroup$ Commented May 6, 2012 at 12:16
  • $\begingroup$ @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. $\endgroup$ Commented May 6, 2012 at 14:48
  • 1
    $\begingroup$ 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.) $\endgroup$ Commented May 6, 2012 at 15:35
  • $\begingroup$ @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) $\endgroup$ Commented May 6, 2012 at 15:42
  • $\begingroup$ @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. $\endgroup$ Commented May 6, 2012 at 15:59
  • 1
    $\begingroup$ @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. $\endgroup$ Commented May 6, 2012 at 16:01
  • $\begingroup$ @Manishearth Wow, it's been a while since this. Any chance of bras and kets in that nice script of yours? $\endgroup$ Commented Sep 10, 2013 at 12:58
  • $\begingroup$ @EmilioPisanty /me has no time :/ $\endgroup$
    – Manishearth Mod
    Commented Sep 10, 2013 at 14:44
  • $\begingroup$ 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? $\endgroup$ Commented May 15, 2015 at 23:58
  • $\begingroup$ 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? $\endgroup$ Commented Jun 15, 2016 at 23:18

3 Answers 3

5
$\begingroup$

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).

$\endgroup$
2
  • $\begingroup$ AFAICT MathJax only works with amsmath. A new package, in the general case, would require a whole bunch of new AJAX+fonts. Even if the package just condenses amsmath commands, it won't work. But yes, we can choose the good ones from there and use 'em. $\endgroup$ Commented Mar 2, 2012 at 1:14
  • $\begingroup$ WRT text document: that would work, but that might make the post huge in the case of a large commandset. $\endgroup$ Commented Mar 2, 2012 at 1:16
2
$\begingroup$

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.

$\endgroup$
3
  • $\begingroup$ 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. $\endgroup$ Commented Mar 1, 2012 at 17:23
  • $\begingroup$ But the point is valid for other subdisciplines, though I can't think of any conflicts atm. $\endgroup$ Commented Mar 1, 2012 at 17:25
  • $\begingroup$ WRT the novice thing: atleast, that's the only place I've seen it used. $\endgroup$ Commented Mar 1, 2012 at 17:34
-1
$\begingroup$

Overview of \newcommand

Below, I present three different example of how to create a new command in your postings on the subject of physics.

I hope that you will copy, paste, and edit parts of my examples to suit your purposes.


Example 1: The Example with Arrows

$\bbox[20px, border: 4px solid]{A \to B \to C}$

The following two columns of code produce the same final rendering.

By "rendering" I mean that the final result looks the same to humans eyes.

Example with Arrows
Code 1 of 2
Example with Arrows
Code 2 of 2
$\newcommand{\SES}[3]{ #1 \to #2 \to #3 }$
$\SES{A}{B}{C}$
$A \to B \to C $

The \newcommand stuff should go somewhere near the top of your posting, and you should only write it once.

Note that in our example...

  • #1 gets replaced by the letter A

  • #2 gets replaced by the letter B

  • #3 gets replaced by the letter C

The name of the new command is SES

The brackets with a number three [3] show how many inputs the new command accepts.


Example 2: The Blackboard Bold Letter "ℝ"

$\bbox[20px, border: 4px solid]{\newcommand{\R}{\mathbb{R}}\R}$

The following two columns of code produce the same final rendering.

By "rendering" I mean that the final result looks the same to humans eyes.

Example with Blackboard Bold Letter "ℝ"
Code 1 of 2
Example with Blackboard Bold Letter "ℝ"
Code 2 of 2
$\newcommand{\R}{\mathbb{R}}$
$\R$
$\mathbb{R}$

Example 3: Example with Parentheses

$\bbox[20px, border: 4px solid]{\left(\dfrac{1}{x}\right)}$

Note that parentheses () usually stay small no matter what is inside of them.

Equivalently, parentheses () do not automatically get bigger, or smaller, based on what is inside of the parentheses.

The various ways to make parentheses bigger.

LOOKS BAD
TO HUMAN EYES
LOOKS GOOD
TO HUMAN EYES
$$\text{The reciprocal of } x \text{ is } (\dfrac{1}{x}).$$ $$\text{The reciprocal of } x \text{ is } \left(\dfrac{1}{x}\right)$$

The following two columns of code produce the same final rendering.

By "rendering" I mean that the final result looks the same to humans eyes.

Example with Parentheses
Code 1 of 2
Example with Parentheses
Code 2 of 2
$\newcommand{\b}[1]{\left(#1\right)}$
$\b{\dfrac{1}{x}}$
$\left(\dfrac{1}{x}\right)$
$\endgroup$

You must log in to answer this question.

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