# MathJax basic tutorial and quick reference


Second, add \limits_{x \to 1} inside. The code now looks like $\lim \limits_{x \to 1}$, and renders as $\lim \limits_{x \to 1}$. The \to inside makes the right arrow, rendered as $\to$. The _ makes the $x \to 1$ go underneath the $\lim$. Finally, the pair of curly braces { } makes sure that $x \to 1$ is treated as a whole object, and not two separate things.

Lastly, add the function you want to apply the limit to. To make the limit mentioned above, $\lim \limits_{x \to 1} \frac{x^2-1}{x-1}$, simply use $\lim\limits_{x \to 1} \frac{x^2-1}{x-1}$.

And that is how you make a limit using MathJax.

• Why not just \lim_{x\to 1} $$\lim_{x\to 1}?$$ As I understand it \limits is only needed for operations that don't already understand limits, for example if you want to use + and get $$\mathop{+}\limits_{i=1}^k\text{ instead of }+_{i=1}^k$$ When used inline, your suggestion will produce $\lim\limits_{x\to 1}$ instead of the more compact form $\lim_{x\to 1}$ that mathjax normally chooses. Are you sure this is good advice?
– MJD
Feb 26, 2014 at 14:10
• @MJD $\lim_{x\to 1} renders to$\lim_{x\to 1}$, and$\lim\limits_{x\to 1 renders as $lim\limits_{x\to 1}$. Note how the $x\to 1$ is separated from the first limit, and not directly underneath. We do not write limits like that in real life, so we use \limits. Feb 26, 2014 at 16:19
• I meant that the second limit renders to $\lim \limits_{x \to 1}$ Feb 26, 2014 at 16:28
• Limits are usually written that way in typeset materials like papers and books when the limit is inline, rather than a displayed formula, and that's why MathJax typesets it that way.
– MJD
Feb 26, 2014 at 16:41
• The issue with this answer is that it is trying to "force" display mode on inline code. Doing so makes the text look less pretty. For example, see how the spacing between the lines change when I force display mode using \lim\limits_{x\mapsto 1}\dfrac1x: $\lim\limits_{x\mapsto 1}\dfrac1x$. On the other hand, when I let $\TeX$ do what it wants to do, using \lim_{x\mapsto 1}\frac1x, the spacing between the lines stays the same, which is much neater: $\lim_{x\mapsto 1}\frac1x$. This is much easier on the eyes. If you want to make your math mode more prominent then take a new line using $$-$$ Jul 17, 2014 at 12:30
• The moral is: $\TeX$ was written by a jolly clever chap. Let it do what it wants, because it does it for a reason! Jul 17, 2014 at 12:35
• Part 11 of the "question" shows how to write limits in the way they were meant to be written in LaTeX and MathJax. Nov 14, 2015 at 23:17

# Arbitrary operators

If an operator is not available as a built-in command, use \operatorname{…}. So for things like $$\operatorname{arsinh}(x)$$ write \operatorname{arsinh}(x) since \arsinh(x) will give an error and arsinh(x) has wrong font and spacing: $arsinh(x)$.

This was already mentioned in a comment by Charles Staats. You might consider this an addition to the FAQ section on \lim, \sin and so on.

For operators which need limits above and below the operator, use \operatorname*{…}, as in $$\operatorname*{Res}_{z=1}\left(\frac1{z^2-z}\right)=1$$

• We can also use $\verb*{\rm ...}*$. For example, $\verb*{\rm arsinh}*$ yields ${\rm arsinh}$. Aug 12, 2014 at 0:27
• @Felix: \rm will change the font but not the spacing. \operatorname{arsinh}x renders as “$\operatorname{arsinh}x$” while {\rm arsinh}x renders as “${\rm arsinh}x$”. Notice the added space between operator and operand in the first example, which is missing in the second. On the whole, I'd say that operatorname is a lot more in the spirit of semantic markup, declaring what you want to write instead of how you want to write it, so I'd strongly suggest using this.
– MvG
Aug 13, 2014 at 11:27
• Thanks. I didn't know there was a difference between them. I always avoided ${\tt operatorname}$ because it was too long. Aug 13, 2014 at 14:41
• Thanks for this. I thought carefully about whether to put \operatorname in the main post, and decided to leave it out. The reason is simple: If a beginner omits \operatorname, the resulting formula will still be perfectly clear, and a more experienced user will have no trouble inserting the \operatorname where it is needed. So including it in the main post would not be a good use of space.
– MJD
Aug 16, 2014 at 6:28
• ... I always use "\text{operator }". Hmmm, $\text{arsinh }x$ vs $\operatorname{arsinh}x$. Feb 10, 2015 at 16:48
• If you use the same operator many times, I think you can do \DeclareMathOperator{\arsinh}{arsinh} at the post's top. Never tried it though… Aug 15, 2015 at 17:28
• What is the code for the last one? May 27, 2021 at 8:07
• @Laxmi you can right-click on MathJax formulas and select "Show Math As / TeX Commands" to see the code for any formula. You can also click on the date of the edits to see edit history, and in that history use "Side-by-side Markdown" rendering to see the source of the whole post.
– MvG
May 27, 2021 at 15:07

# Highlighting equation

To highlight an equation, \bbox can be used. E.g,

$$\bbox[yellow] { e^x=\lim_{n\to\infty} \left( 1+\frac{x}{n} \right)^n \qquad (1) }$$

produces

$$\bbox[yellow] { e^x=\lim_{n\to\infty} \left( 1+\frac{x}{n} \right)^n \qquad (1) }$$

By default, the bounding box is "tight", so it doesn't extend beyond the characters used in the formula. You can add a little space around the equation by adding a measurement after the color. E.g.,

$$\bbox[yellow,5px] { e^x=\lim_{n\to\infty} \left( 1+\frac{x}{n} \right)^n \qquad (1) }$$

produces

$$\bbox[yellow,5px] { e^x=\lim_{n\to\infty} \left( 1+\frac{x}{n} \right)^n \qquad (1) }$$

$$\bbox[5px,border:2px solid red] { e^x=\lim_{n\to\infty} \left( 1+\frac{x}{n} \right)^n \qquad (2) }$$

produces

$$\bbox[5px,border:2px solid red] { e^x=\lim_{n\to\infty} \left( 1+\frac{x}{n} \right)^n \qquad (2) }$$

You can do both border and background, as well:

$$\bbox[yellow,5px,border:2px solid red] { e^x=\lim_{n\to\infty} \left( 1+\frac{x}{n} \right)^n \qquad (1) }$$

produces

$$\bbox[yellow,5px,border:2px solid red] { e^x=\lim_{n\to\infty} \left( 1+\frac{x}{n} \right)^n \qquad (1) }$$

• When using constructs like this, please heed the points raised in this discussion on usage of colour. May 20, 2016 at 15:56
• This would be a very helpful feature. May 19, 2017 at 13:36

# Absolute values and norms

The absolute value of some expression can be denoted as \lvert x\rvert or, more generally, as \left\lvert … \right\rvert. It renders as $\lvert x\rvert$.

The norm of a vector (or similar) can be denoted as \lVert v\rVert or, more generally, as \left\lVert … \right\rVert. It renders as $\lVert v\rVert$. (You may also write \left\|…\right\| instead.)

In both cases, the rendering is better than what you'd get from |x| or ||v||, which render with bars that don't descend low enough and sub-optimal spacing. At least on some browsers, so here is a screenshot how it looks for me, using Firefox 31 on OS X:

And here is the same formula rendered by your browser:

$$|x|, ||v|| \quad\longrightarrow\quad \lvert x\rvert, \lVert v\rVert$$

It was typeset as

$$|x|, ||v|| \quad\longrightarrow\quad \lvert x\rvert, \lVert v\rVert$$
• You can use \|x\| instead of \lVert x \rVert; $\|x\|$ and $\lVert x \rVert$. (I don't think that there is a difference between them. I've tried [asking on SE](tex.stackexchange.com/questions/77767/whats-the-correct-way-to-write-norm).) Jun 24, 2014 at 8:48
• On my browser |x| and \lvert x\rvert ($|x|$ and $\lvert x\rvert$) look identical, contrary to your claim. Perhaps you need to show an example more complicated than just 'x'?
– MJD
Jun 24, 2014 at 12:39
• @MJD: What's your browser? I included a screenshot to support my claim.
– MvG
Aug 13, 2014 at 11:24
• Usually various versions of Firefox on either Linux or Windows. I happen to have Windows 8 booted now, so here's a screenshot from there: a.pomf.se/jrujkq.PNG The bar height looks good on both pairs of symbols; the spacing is a little off for the || version. On Linux they looked the same.
– MJD
Aug 13, 2014 at 17:02
• Here's a screenshot with FF 31.0 under Linux: a.pomf.se/fhwmjo.png
– MJD
Aug 16, 2014 at 6:23
• The difference in output that you are seeing has to do with whether you have the STIX fonts installed locally on your computer or not. The | in STIX doesn't descend below the baseline, while in the MathJax TeX fonts it does. May 20, 2016 at 14:16

## Giving reasons on each line of a sequence of equations

To produce this: \begin{align} v + w & = 0 &&\text{Given} \tag 1\\ -w & = -w + 0 && \text{additive identity} \tag 2\\ -w + 0 & = -w + (v + w) && \text{equations $(1)$ and $(2)$} \end{align}

write this:

\begin{align}
v + w & = 0  &&\text{Given} \tag 1\\
-w & = -w + 0 && \text{additive identity} \tag 2\\
-w + 0 & = -w + (v + w) && \text{equations $(1)$ and $(2)$}
\end{align}
• Using multiple \tag commands in my equations causes them to break. It only takes one tag per equation and it labels the entire thing instead of allowing tagging on a per-line basis. Any ideas? Jun 1, 2019 at 20:19
• @code_dredd The particular formatting in this answer still seems to work. Perhaps you could post your formulas in a new meta question to get help with them. Jun 2, 2019 at 5:20
• Why would you use \tag, instead of just using ()? Feb 1, 2021 at 21:13
• @SomeGuy First, this is what \tag is meant for. It puts the (1) exactly where it should be, at the right margin. Second, if you have an equation like 0 = ax^2+bx+c and you just use (1) on it instead of \tag1, you end up with $0 = ax^2+bx+c(1)$. To fix this, instead of \tag you have to insert other commands to make enough blank space. Not a net gain, in my opinion. Feb 2, 2021 at 0:59

# Pack of cards

If you are asking (or answering) a combinatorics question involving packs of cards you can make it look more elegant by using \spadesuit, \heartsuit, \diamondsuit, \clubsuit in math mode: $$\spadesuit\quad\heartsuit\quad\diamondsuit\quad\clubsuit$$ Or if you're really fussy:
\color{red}{\heartsuit} and \color{red}{\diamondsuit}
$$\color{red}{\heartsuit}\quad\color{red}{\diamondsuit}$$

You can also enter the standard Unicode characters (U+2660 BLACK SPADE SUIT etc.) literally, or copy them from here:

$$♠\quad♡\quad♢\quad♣\\ ♤\quad♥\quad♦\quad♧$$

• This is very nice! Is there other auto-shapes or stickers? May 19, 2017 at 13:37
• Is it also possible to draw the spade and club in outlines and fill the heart and diamond with a colour? May 19, 2017 at 13:39
• @AlwaysConfused None that come to mind. Google search turned up this which might help. Otherwise search for a TeX/LaTeX/MathJax symbol table. May 22, 2017 at 23:48
• @AlwaysConfused Unicode has those characters, so you can enter them however you normally enter Unicode characters, or you can now use copy-paste to copy them from this answer.
– MJD
May 29, 2018 at 16:11
• @MJD Not sure that your edit is a good idea, firstly because I think we would prefer questions and answers on MSE to be in MathJax as far as possible, secondly because this page is specifically a MathJax tutorial. However I'm not really bothered - if you still think it's a good idea, let me know and I'll approve the edit. May 30, 2018 at 4:31
• Is there a way to force the heart and diamond suit symbols to be filled, like the club and spade? Jun 2, 2019 at 18:39
• @code_dredd See my previous comment in reply to "Always Confused", also the comment by MJD. Jun 2, 2019 at 22:08
• @David I guess nothing has changed since then... Thanks. Jun 2, 2019 at 23:36
• To the above commenters - it is possible, for instance $$\color{yellow}♥\!\!\!\color{blue}♡$$ achieved via the code $$\color{yellow}♥\!\!\!\color{blue}♡$$ but you will need to fiddle with the number of \!s depending on where you put it, because I don't think there is a command in mathJax to place characters on top of each other. Another example,$$\Huge \color{green}\Huge \color{green}♥\!\!\!\!\!\!\!\!\!\!\,\color{red}♡$$ gives $$\Huge \color{green}♥\!\!\!\!\!\!\!\!\!\!\,\color{red}♡$$ Mar 28, 2020 at 6:02

## Left and Right Implication Arrows

Another way to display the arrows for right and left implication instead of using

$\Rightarrow$, $\Leftarrow$ and $\Leftrightarrow$

which produces $\Rightarrow$, $\Leftarrow$ and $\Leftrightarrow$ respectively, you can use

$\implies$ for $\implies$, $\impliedby$ for $\impliedby$ and $\iff$ for $\iff$

The latter of which produces longer arrows which may be more desirable to some.

# Degree symbol

Standard Mathjax does not yet support a dedicated degree symbol, so here are some of the ways to try and emulate one :

$$\begin{array} \\ \text{45^\text{o}} & \text{renders as} & 45^\text{o} \\ \text{45^o} & \text{renders as} & 45^o \\ \text{45^\circ} & \text{renders as} & 45^\circ \\ \text{45^{\large\circ}} & \text{renders as} & 45^{\large\circ}\\ \text{45\unicode{xB0}} & \text{renders as} & 45\unicode{xB0} & \text{Actual Unicode character}\\ \text{90°} & \text{renders as} & 90° & \text{Using keyboard entry of symbol} % % Use the following line as a template for additional entries % % \text{} & \text{renders as} & \\ \end{array}$$

The degree symbol for angles is not ^\circ. Although many people use this notation, the result looks quite different from the canonical degree symbol shipped with the font, as seen above.

If your keyboard doesn't have a ° key, feel free to copy from this post here, or follow these suggestions.

Note that comments below indicate that on some configurations at least, ° renders inferior to ^\circ. And I recently had a post of mine edited just for the sake of turning ° into ^\circ, indicating that someone felt rather strongly about this. So the suggestion above does seem somewhat controversial at the moment. I maintain that from a semantic point of view, ° is superior to ^\circ, and if the rendering suffers from this, then it's a bug in MathJax. After all, LaTeX offers a proper degree symbol in the tex companion fonts, indicating that someone there, too, decided that ^\circ is not perfect. But if things are broken now, I can't fault people from pragmatically sticking with the rendering they prefer. Personally I prefer semantics, also for the sake of screen readers.

Accessibility

Aside from appearance, one consideration in choosing which notation to use is how it will get parsed by screen readers. For example, ChromeVox reads both 45^\circ and 45° as "forty-five degrees", while the other two are pronounced as "forty-five oh", which may be a reason to avoid them.

Usepackage

Commonly in Latex you can \usepackage{gensymb} to get the \degree symbol, however on Stack Exchange this is not an option. Note that even if you can do this it will typically affect the entire page, which may have side effects for other users. So don't rely on this approach.

• If mathjax loads siunitx or gensymb, there is then \degree in latex which is the degree symbol. Feb 17, 2015 at 22:29
• @dustin: I couldn't find siunitx or gensymb mentioned anywhere in the MatJax source repository. Are they available as some kind of third-party extension? If so, where? Since MathJax is not LaTeX, packages can't be loaded unless they have been migrated. By the way, all occurrences of “degree” in the MathJax sources refer to something else, as far as I can tell, so there really doesn't seem to be a \degree macro. There should be one, imho.
– MvG
Feb 17, 2015 at 23:39
• I am not a mathjax expert. I just know latex. I just gave that suggestion in case they were available. Siunitx would be a great package to have. If you aren't familiar, you will see the advantage by scanning the documentation on ctan. Feb 17, 2015 at 23:43
• On my display, ° looks bad and ^\circ looks good: a.pomf.se/xnlfyg.png
– MJD
Mar 24, 2015 at 21:10
• Degree sign can generally be typed by holding down Alt and typing 0176 on the numeric keypad. ° (I don't know how international the actual number is). The leading zero is required. Apr 19, 2017 at 14:04
• @Joffan: 167 is the decimal representation of the Codepoint for ° in Latin 1, Unicode and CP-1252. Without the leading zero, CP-437 gets applied instead, at least in typical English-speaking countries, so you'd use Alt+248 there. The Wikipedia article I linked to already describes those two ways of entering the symbol, and en.wikipedia.org/wiki/Alt_code has some more details.
– MvG
Apr 20, 2017 at 22:24
• How to use Radian (c) , gradian (g) and Steradian (sr) ? And also, Angstrom (though a lenght unit)? May 21, 2017 at 16:06
• Actually we can write degrees by 90^o (O for Orange, using lowercase o, like 'o'), and it'd render it close to degrees symbol $$90^o + 30 ^o + 45^o$$
– user427802
May 31, 2018 at 14:41
• @AbhasKumarSinha It looks quite slanty to me. Jun 13, 2018 at 3:57
• @StephenG: I'm not happy with your latest edit. I feel that it is not helpful to users if we suggest even more ways to poorly format that symbol (like ^o imho), or to mention a LaTeX approach just to say it won't work. You deleted the example for 45°, but kept the sentence talking about it, including the colon. I'm reluctant to revert your edit on a CW page without a conversation, but as it stands I see the edit as a change for the worse. Can we find a combined solution?
– MvG
Oct 8, 2018 at 19:09
• I just wrote a feature request for a \degree symbol, since I believe it would be technically easy and conceptually beneficial to have such a symbol defined for the whole site.
– MvG
Oct 8, 2018 at 19:25
• @MvG I have added an entry to the "renders as" table for keyboard entry (which frankly looks awful IMO) but regarding your "unhappiness" note only one line was deleted from the version preceding my first edit and I regard your belief that this justifies your claim my edit was "unhelpful" is nonsense. I fail to see how undoing my edit helps anyone but you. Oct 10, 2018 at 4:16
• While we're at it, I included my comment on accessibility from the feature request post, since it may be more useful here. It would be nice if other people tested other screen readers to get a sample size of higher than one. Oct 10, 2018 at 5:25
• I recently discovered \mathring and hence there is a further variant a\mathring{}$a\mathring{}$ which is neither circ$a^\circ$ nor the actual unicode symbol $a°$ Nov 22, 2021 at 2:34

## Long division

$$\require{enclose} \begin{array}{r} 13 \\[-3pt] 4 \enclose{longdiv}{52} \\[-3pt] \underline{4}\phantom{2} \\[-3pt] 12 \\[-3pt] \underline{12} \end{array}$$

$$\require{enclose} \begin{array}{r} 13 \\[-3pt] 4 \enclose{longdiv}{52} \\[-3pt] \underline{4}\phantom{2} \\[-3pt] 12 \\[-3pt] \underline{12} \end{array}$$

One important trick shown here is the use of \phantom{2} to make a blank space that is the same size and shape as the digit 2 just above it.

This is adapted from https://stackoverflow.com/a/22871404/3466415 (which uses slightly different but not less valid formatting).

• Synthetic division. Example to find that $$x^3−6x^2+11x−6=(x−{\color{red}1})(x^2−5x+6)+{\color{blue}0}$$ \begin{array}{c|rrrr}& x^3 & x^2 & x^1 & x^0\\ & 1 & -6 & 11 & -6\\ {\color{red}1} & \downarrow & 1 & -5 & 6\\ \hline & 1 & -5 & 6 & |\phantom{-} {\color{blue}0} \end{array} \begin{array}{c|rrrr}& x^3 & x^2 & x^1 & x^0\\ & 1 & -6 & 11 & -6\\ {\color{red}1} & \downarrow & 1 & -5 & 6\\ \hline & 1 & -5 & 6 & |\phantom{-} {\color{blue}0} \end{array} Aug 21, 2016 at 14:32
• @Maria Mazur For the same example $\dfrac{x^3-6x^2+11x-6}{x-1}=x^2-5x+6$: $$\begin{array}{rrrr|ll} x^3 & -6x^2 & +11x & -6 & x - 1 \\ -x^3 & +x^2 & & & x^2-5x+6 \\ \hline & -5x^2 & +11x & -6\\ & \phantom{-}5x^2 & -5x & & & & \\ \hline & & +6x & -6 \\ & & -6x & +6 \\ \hline & & 0 & 0 \end{array}$$ I've used this code \begin{array}{rrrr|ll} x^3 & -6x^2 & +11x & -6 & x - 1 \\ -x^3 & +x^2 & & & x^2-5x+6 \\ \hline & -5x^2 & +11x & -6\\ & \phantom{-}5x^2 & -5x & & & & \\ \hline & & +6x & -6 \\ & & -6x & +6 \\ \hline & & 0 & 0 \end{array} May 16, 2019 at 20:06

# Displaystyle and Textstyle

Many things like fractions, sums, limits, and integrals display differently when written inline versus in a displayed formula. You can switch styles back and forth with \displaystyle and \textstyle in order to achieve the desired appearance.

Here's an example switching back and forth in a displayed equation:

$$\sum_{n=1}^\infty \frac{1}{n^2} \to \textstyle \sum_{n=1}^\infty \frac{1}{n^2} \to \displaystyle \sum_{n=1}^\infty \frac{1}{n^2}$$

$$\sum_{n=1}^\infty \frac{1}{n^2} \to \textstyle \sum_{n=1}^\infty \frac{1}{n^2} \to \displaystyle \sum_{n=1}^\infty \frac{1}{n^2}$$

It is possible to switch style inline as well:

Compare $\displaystyle \lim_{t \to 0} \int_t^1 f(t)\, dt$
versus $\lim_{t \to 0} \int_t^1 f(t)\, dt$.

Compare $\displaystyle \lim_{t \to 0} \int_t^1 f(t)\, dt$ versus $\lim_{t \to 0} \int_t^1 f(t)\, dt$.

• Oh!! I was always confused on why some people had \displaystyle. Nov 7, 2016 at 0:42
• @SimplyBeautifulArt I was always wondering on why the math expressions of some people looked nicer than mine..
– user486983
Sep 21, 2018 at 21:37
• There is also $\scriptstyle{AbC}$ $\scriptstyle{AbC}$ and $\scriptscriptstyle{AbC}$ $\scriptscriptstyle{AbC}$. Mar 5, 2020 at 8:52

# Vertical Spacing

Some formulas such as $\overline a+\overline b=\overline {a\cdot b}$, $\sqrt{a}-\sqrt{b}$, do not look quite right when it comes to vertical spacing. Fortunately, there is more than one way to fix this. One can for instance employ the \mathstrut command as follows:

$\sqrt{\mathstrut a} - \sqrt{\mathstrut b}$

Which yields: $\sqrt{\mathstrut a} - \sqrt{\mathstrut b}$. Or using \vphantom (vertical phantom) command, which measures the height of its argument and places a math strut of that height into the formula.

$\sqrt{\vphantom{b} a} - \sqrt{b}$

Which renders as: $\sqrt{\vphantom{b} a} - \sqrt{b}$.

Another issue is with the spacing within lines in situations like this,

Based on the previous technique, we can simplify $\dfrac{1}{\sqrt{\vphantom{b} a} - \sqrt{b}}$, and we thus get the result of the previous limit.

These two lines are too far apart, but this is unnecessary since the second line is very short. We can solve this by using the \smash command, to get:

Based on the previous technique, we can simplify $\smash{\dfrac{1}{\sqrt{\vphantom{b} a} - \sqrt{b}}}$, and we thus get the result of the previous limit.

• Alternatively, one can also sneak in a rule of zero width \rule{0pt}{2ex}, as explained here. Apr 29, 2020 at 15:06
• On Android, at least, the results of \smash look awful. The formula overlaps the text. Mar 5 at 1:10

# Equation numbering

## Simple equation

To give an equation a number, use the \tag{}. To refer to it later, use \label{} to label this equation. When you want to refer to it, use \eqref{}. For example,

$$e=mc^2 \tag{1}\label{eq1}$$

Equation $$\eqref{eq1}$$ is one the greatest equations in mankind history. Equation $$\eqref{eq1}$$ is produced using the following code,

$$e=mc^2 \tag{1}\label{eq1}$$

To refer to it, use \eqref{eq1}.

## Multi-line equation

Multi-line equation is actually just one equation rather than several equations. So the correct environment is aligned instead of align.

\begin{aligned} a &= b + c \\ &= d + e + f + g \\ &= h + i \end{aligned}\tag{2}\label{eq2}

Equation $$\eqref{eq2}$$ is a multi-line equation. The code to produce equation $$\eqref{eq2}$$ is

\begin{aligned} a &= b + c \\ &= d + e + f + g \\ &= h + i \end{aligned}\tag{2}\label{eq2}

## Multiple aligned equations

For multiple aligned equations, we use the align environment.

\begin{align} a &= b + c \tag{3}\label{eq3} \\ x &= yz \tag{4}\label{eq4}\\ l &= m - n \tag{5}\label{eq5} \end{align}

Equation $$\eqref{eq3}$$, $$\eqref{eq4}$$ and $$\eqref{eq5}$$ are multiple equations aligned together. The code to produce these equations is,

\begin{align} a &= b + c \tag{3}\label{eq3} \\ x &= yz \tag{4}\label{eq4}\\ l &= m - n \tag{5}\label{eq5} \end{align}
• I don’t believe there is any difference between align and aligned, but whatever feels comfortable I suppose. Feb 2, 2018 at 6:12
• There is actaully a difference, read here for a detailed discussions. Feb 2, 2018 at 6:28
• thank you very much for clearing up that understanding :) Feb 2, 2018 at 6:30
• You are welcome. When in doubt, always google it first :). Feb 2, 2018 at 6:32
• If there's an equation with multiple lines, is there a way to add tags on a per-line basis, i.e. \tag{1} for line 1, \tag{2} for line 2, etc? If I use the \tag{...} commands, I can only use one per equation and it labels the entire equation, not each line. Jun 1, 2019 at 20:17
• I am not aware of this kind of command. What is your use case? Jun 4, 2019 at 2:27
• the last equation numbering can also be used with align* instead of align Dec 2, 2019 at 23:59
• I get all tags on the first line: "a = b + c (3)(4)(5)". Dec 13, 2019 at 13:55
• Why do we need both and with aligned? I noticed that when I omitted that I did not get an equation number, but that does not happen when I use only without any other environments inside. May 12, 2021 at 20:41

# Units

While $\LaTeX$ has packages that format units, MathJax does not. For visual consistency, one should format units within the same string of MathJax code as the value to which it corresponds, separating the value and unit with \ (space-backslash-space) since the BIPM recommends a small space between the value and units. In addition, follow the below conventions for formatting values and units:

### Decimal Separator & Digit Separation

Following the conventions of the English-speaking world, a . $.$ should be used to separate the decimal part of a number from the integral part, not , $,$ as is common in some languages. This is because commas are already reserved for separating mathematical notation such as arguments of multivariate functions, elements of a set, and the coordinates of ordered tuples.

No punctuation should be used to separate multiples of three digits on either side of the decimal separator; instead, a small space rendered by \, should be used on both sides of the decimal marker when the string of digits consists of more than four or five digits. For example,

• 4321.1234 $4321.1234$
• 54\,321.123\,45 $54\,321.123\,45$
• 0.56789 $0.56789$
• 0.567\,89 $0.567\,89$

If you use a decimal separator, you should include a digit on both sides of the separator, even if the digit is simply $0$.

### Powers of $10$

Seeing as we are not calculators, it is preferable to fully write without abbreviation \times10^{n} $\times10^{n}$ when scientific or engineering notation is helpful or necessary. Do not precede or follow this markdown with positive nor negative spaces; \times takes care of that on its own.

Nevertheless, if necessary, use an upright variant of the letter ‘E’ or ‘e’ to indicate order of magnitude, such as

• \mathrm{E}\,6 $\mathrm{E}\,6$
• \scriptsize{\mathrm{E}}\,\normalsize{6} $\scriptsize{\mathrm{E}}\,\normalsize{6}$
• \mathrm{e}\,6 $\mathrm{e}\,6$

A small space on either side is perfectly fine and recommended.

### Single Units

The symbol of any unit—especially SI units—should follow the form \mathrm{u}. (I have this command saved under the keyboard shortcut usin on my devices.) For example,

• \mathrm{m} $\mathrm{m}$
• \mathrm{kg} $\mathrm{kg}$
• \mathrm{ft.} $\mathrm{ft.}$

Do not use a period with symbolic units; do use a period with abbreviated units.

### Units with a Dot Multiplier

Multiplied units conjoined by a dot should follow the form \mathrm{u}\!\cdot\!\mathrm{v} $\mathrm{u}\!\cdot\!\mathrm{v}$. (I have this sequence of commands saved under the keyboard shortcut umul on my devices.) Because of how \cdot is designed (i.e., to separate numbers), the small negative space \! on either side maintains uniform spacing throughout the whole compound unit. For example,

• \mathrm{N}\!\cdot\!\mathrm{m} $\mathrm{N}\!\cdot\!\mathrm{m}$
• \mathrm{s}\!\cdot\!\mathrm{A} $\mathrm{s}\!\cdot\!\mathrm{A}$

Do not use \times $\times$ as a separator.

### Units with a Solidus Separator

Divided units conjoined by a solidus should follow the form \left.\mathrm{u}\middle/\mathrm{v}\right. $\left.\mathrm{u}\middle/\mathrm{v}\right.$. (I have this sequence of commands saved under the keyboard shortcut udiv on my devices.) The extra markdown is to ensure that solidus stretches the entire height of the unit, especially when exponents are involved. For example,

• \left.\mathrm{J}\middle/\mathrm{s}\right. $\left.\mathrm{J}\middle/\mathrm{s}\right.$
• \left.\mathrm{m}\middle/\mathrm{s}^2\right. $\left.\mathrm{m}\middle/\mathrm{s}^2\right.$

You may include small negative spaces \! on either side of the solidus if you please.

### Exponents

Exponents can be rendered with the standard MathJax markdown. The carat and number should immediately follow the closing brace of the mathrm{} argument. For example,

• \mathrm{m}^2 $\mathrm{m}^2$
• \left.\mathrm{m}\middle/\mathrm{s}^2\right. $\left.\mathrm{m}\middle/\mathrm{s}^2\right.$

### Parentheses

Parentheses can also be rendered with standard MathJax markdown using \left( and \right) outside the argument of \mathrm. For example,

• \left.\mathrm{kg}\!\cdot\!\mathrm{m}^2\middle/\left(\mathrm{C}\!\cdot\!\mathrm{s}\right)\right. $\left.\mathrm{kg}\!\cdot\!\mathrm{m}^2\middle/\left(\mathrm{C}\!\cdot\!\mathrm{s}\right)\right.$

### Exponents in Place of Separators

If you prefer to use no separators and only powers, separator each single \mathrm{} with a small space \, and use exponents as necessary. For example,

• \mathrm{m}\,\mathrm{s}^{-2} $\mathrm{m}\,\mathrm{s}^{-2}$
• \mathrm{s}^{-1}\,\mathrm{mol} $\mathrm{s}^{-1}\,\mathrm{mol}$

### Examples in Context

\mu_0=4\pi\times10^{-7} \ \left.\mathrm{\mathrm{T}\!\cdot\!\mathrm{m}}\middle/\mathrm{A}\right.

$$\mu_0=4\pi\times10^{-7} \ \left.\mathrm{\mathrm{T}\!\cdot\!\mathrm{m}}\middle/\mathrm{A}\right.$$

$$180^\circ=\pi \ \mathrm{rad}$$

N_A = 6.022\times10^{23} \ \mathrm{mol}^{-1}

$$N_A = 6.022\times10^{23} \ \mathrm{mol}^{-1}$$

# Linear programming

## Formulation

A theoretical LPP can be typeset as

$$\begin{array}{ll} \text{maximize} & c^T x \\ \text{subject to}& d^T x = \alpha \\ &0 \le x \le 1. \end{array}$$

$$\begin{array}{ll} \text{maximize} & c^T x \\ \text{subject to}& d^T x = \alpha \\ &0 \le x \le 1. \end{array}$$

To input a numerical LPP, use alignat instead of align to get better alignment between signs, variables and coefficients.

\begin{alignat}{5} \max \quad & z = & x_1 & + & 12 x_2 & & & && \\ \mbox{s.t.} \quad & & 13 x_1 & + & x_2 & + & 12x_3 & \geq 5 && \tag{constraint 1} \\ & & x_1 & & & + & x_3 & \leq 16 && \tag{constraint 2} \\ & & 15 x_1 & + & 201 x_2 & & & = 14 && \tag{constraint 3} \\ & & \rlap{x_i \ge 0, i = 1, 2, 3} \end{alignat}

\begin{alignat}{5} \max \quad & z = & x_1 & + & 12 x_2 & & & && \\ \mbox{s.t.} \quad & & 13 x_1 & + & x_2 & + & 12x_3 & \geq 5 && \tag{constraint 1} \\ & & x_1 & & & + & x_3 & \leq 16 && \tag{constraint 2} \\ & & 15 x_1 & + & 201 x_2 & & & = 14 && \tag{constraint 3} \\ & & \rlap{x_i \ge 0, i = 1, 2, 3} \end{alignat}

We treat $$\max$$, $$z$$, each variable, $$\pm$$ sign and RHS as one separate column, while leaving an extra empty column on the right. Then we count the number of separators &, add one into this number then divide it by two. (e.g. (9 + 1) ÷ 2 = 5)

\rlap is used so that the last row spans over one column.

Optional: \tag is used to label the constraints.

## Change MATLAB/Octave matrices to $$\rm\LaTeX$$ code

To get fractions, execute format rat at the beginning.

Writing manually the $$\rm\LaTeX$$ code for a matrix with many rows and columns in Octave is tedious. The Octave function

strcat("\\begin{bmatrix}\n",strrep(strrep(mat2str(A)," "," & "), ...
";"," \\\\\n")(2:end-1),"\n\\end{bmatrix}\n")

converts

A = [1 2 2; 2 3 4; 4 4 2]
A =

1   2   2
2   3   4
4   4   2

to

$$\begin{bmatrix} 1 & 2 & 2 \\ 2 & 3 & 4 \\ 4 & 4 & 2 \end{bmatrix}$$

so that pasting the generated code gives

$$\begin{bmatrix} 1 & 2 & 2 \\ 2 & 3 & 4 \\ 4 & 4 & 2 \end{bmatrix}.$$

## Simplex tableaux

Since the coefficient of the objective value variable $$z$$ never changes, my habit is to omit the $$z$$-column to save ink.

### Normal simplex tableau

$$\begin{array}{rrrrrr|r} & x_1 & x_2 & s_1 & s_2 & s_3 & \\ \hline s_1 & 0 & 1 & 1 & 0 & 0 & 8 \\ s_2 & 1 & -1 & 0 & 1 & 0 & 4 \\ s_3 & 1 & 1 & 0 & 0 & 1 & 12 \\ \hline & -1 & -1 & 0 & 0 & 0 & 0 \end{array}$$

$$\begin{array}{rrrrrr|r} & x_1 & x_2 & s_1 & s_2 & s_3 & \\ \hline s_1 & 0 & 1 & 1 & 0 & 0 & 8 \\ s_2 & 1 & -1 & 0 & 1 & 0 & 4 \\ s_3 & 1 & 1 & 0 & 0 & 1 & 12 \\ \hline & -1 & -1 & 0 & 0 & 0 & 0 \end{array}$$

It can be stacked up to give an illustration of the entering of variables at different stages.

$$\begin{array}{rrrrrrr|rr} & x_1 & x_2 & s_1 & s_2 & s_3 & w & & \text{ratio} \\ \hline s_1 & 0 & 1 & 1 & 0 & 0 & 0 & 8 & - \\ w & 1^* & -1 & 0 & -1 & 0 & 1 & 4 & 4 \\ s_3 & 1 & 1 & 0 & 0 & 1 & 0 & 12 & 12 \\ \hdashline & 1 & -1 & 0 & -1 & 0 & 0 & 4 & \\ \hline s_1 & 0 & 1 & 1 & 0 & 0 & 0 & 8 & \\ x_1 & 1 & -1 & 0 & -1 & 0 & 1 & 4 & \\ s_3 & 0 & 2 & 0 & 2 & 1 & -1 & 8 & \\ \hdashline & 0 & 0 & 0 & 0 & 0 & -1 & 0 & \end{array}$$

$$\begin{array}{rrrrrrr|rr} & x_1 & x_2 & s_1 & s_2 & s_3 & w & & \text{ratio} \\ \hline s_1 & 0 & 1 & 1 & 0 & 0 & 0 & 8 & - \\ w & 1^* & -1 & 0 & -1 & 0 & 1 & 4 & 4 \\ s_3 & 1 & 1 & 0 & 0 & 1 & 0 & 12 & 12 \\ \hdashline & 1 & -1 & 0 & -1 & 0 & 0 & 4 & \\ \hline s_1 & 0 & 1 & 1 & 0 & 0 & 0 & 8 & \\ x_1 & 1 & -1 & 0 & -1 & 0 & 1 & 4 & \\ s_3 & 0 & 2 & 0 & 2 & 1 & -1 & 8 & \\ \hdashline & 0 & 0 & 0 & 0 & 0 & -1 & 0 & \end{array}$$

### Dual simplex tableau

$$\begin{array}{rrrrrrrr|r} & x_1 & x_2 & x_3 & x_4 & x_5 & x_6 & x_7 & \\ \hline x_4 & 0 & -3 & 7 & 1 & 0 & 0 & 2 & 2M -4 \\ x_5 & 0 & -9 & 0 & 0 & 1 & 0 & -1 & -M -3 \\ x_6 & 0 & 6 & -1 & 0 & 0 & 1 & -4^* & -4M +8 \\ x_1 & 1 & 0 & 1 & 0 & 0 & 0 & 1 & M \\ \hline & 0 & 1 & 1 & 0 & 0 & 0 & 2 & 2M \\ \text{ratio} & & & 1 & & & & 1/2 & \end{array}$$

$$\begin{array}{rrrrrrrr|r} & x_1 & x_2 & x_3 & x_4 & x_5 & x_6 & x_7 & \\ \hline x_4 & 0 & -3 & 7 & 1 & 0 & 0 & 2 & 2M -4 \\ x_5 & 0 & -9 & 0 & 0 & 1 & 0 & -1 & -M -3 \\ x_6 & 0 & 6 & -1 & 0 & 0 & 1 & -4^* & -4M +8 \\ x_1 & 1 & 0 & 1 & 0 & 0 & 0 & 1 & M \\ \hline & 0 & 1 & 1 & 0 & 0 & 0 & 2 & 2M \\ \text{ratio} & & & 1 & & & & 1/2 & \end{array}$$

It can be stacked up to give a theoretical illustration of what happens in the upcoming steps.

$$\begin{array}{rrrrrrr|r} & x_1 & x_2 & x_3 & s_1 & s_2 & s_3 & \\ \hline s_1 & -2 & 0 & -2 & 1 & 0 & 0 & -60 \\ s_2 & -2 & -4^* & -5 & 0 & 1 & 0 & -70 \\ s_3 & 0 & -3 & -1 & 0 & 0 & 1 & -27 \\ \hdashline & 8 & 10 & 25 & 0 & 0 & 0 & 0 \\ \text{ratio} & -4 & -5/2 & -5 & & & & \\ \hline s_1 & -2^* & 0 & -2 & 1 & 0 & 0 & -60 \\ x_2 & 1/2 & 1 & 5/4 & 0 & -1/4 & 0 & 35/2 \\ s_3 & 3/2 & 0 & 11/4 & 0 & -3/4 & 1 & 51/2 \\ \hdashline & 3 & 0 & 25/2 & 0 & 5/2 & 0 & -175 \\ \text{ratio} & -3/2 & & 25/4 & & & & \\ \hline x_1 & 1 & 0 & 1 & -1/2 & 0 & 0 & 30 \\ x_2 & 0 & 1 & 3/4 & 1/4 & -1/4 & 0 & 5/2 \\ s_3 & 0 & 0 & 5/4 & 3/4 & -3/4^* & 1 & -39/2 \\ \hdashline & 0 & 0 & 19/2 & 3/2 & 5/2 & 0 & -265 \\ \text{ratio} & & & & & \dots & & \\ \hline x_1 & 1 & 0 & 1 & -1/2 & 0 & 0 & 30 \\ x_2 & 0 & 1 & 1/3 & 0 & 0 & -1/3 & 9 \\ s_2 & 0 & 0 & -5/3 & -1 & 1 & -4/3 & 26 \\ \hdashline & 0 & 0 & 41/3 & 4 & 0 & 10/3 & -330 \end{array}$$

## Duality

A picture is worth a thousand words.

$$\require{extpfeil} % produce extensible horizontal arrows \begin{array}{ccc} % arrange LPPs % first row % first LPP \begin{array}{ll} \max & z = c^T x \\ \text{s.t.} & A x \le b \\ & x \ge 0 \end{array} & \xtofrom{\text{duality}} & % second LPP \begin{array}{ll} \min & v = b^T y \\ \text{s.t.} & A^T y \ge c \\ & y \ge 0 \end{array} \\ ({\cal PC}) & & ({\cal DC}) \\ \text{add } {\Large \downharpoonleft} \text{slack var} & & \text{minus } {\Large \downharpoonright} \text{surplus var}\\ % Change to your favorite arrow style % % second row % third LPP \begin{array}{ll} \max & z = c^T x \\ \text{s.t.} & A x + s = b \\ & x,s \ge 0 \end{array} & \xtofrom[\text{some steps skipped}]{\text{duality}} & % fourth LPP \begin{array}{ll} \min & v = b^T y \\ \text{s.t.} & A^T y - t = c \\ & y,t \ge 0 \end{array} \\ ({\cal PS}) & & ({\cal DS}) % \end{array}$$

• It must have taken more than a thousand words to write that picture though :D Jul 20, 2018 at 9:25