3562
$\begingroup$

(Deutsch: MathJax: LaTeX Basic Tutorial und Referenz)

  1. To see how any formula was written in any question or answer, including this one, right-click on the expression and choose "Show Math As > TeX Commands". (When you do this, the '$' will not display. Make sure you add these. See the next point. There are also other possibilities how to view the code for the formula or the whole post.)

  2. For inline formulas, enclose the formula in $...$. For displayed formulas, use $$...$$.
    These render differently. For example, type
    $\sum_{i=0}^n i^2 = \frac{(n^2+n)(2n+1)}{6}$
    to show $\sum_{i=0}^n i^2 = \frac{(n^2+n)(2n+1)}{6}$ (which is inline mode) or type
    $$\sum_{i=0}^n i^2 = \frac{(n^2+n)(2n+1)}{6}$$
    to show $$\sum_{i=0}^n i^2 = \frac{(n^2+n)(2n+1)}{6}$$ (which is display mode).

  3. For Greek letters, use \alpha, \beta, …, \omega: $\alpha, \beta, … \omega$. For uppercase, use \Gamma, \Delta, …, \Omega: $\Gamma, \Delta, …, \Omega$. Some Greek letters have variant forms: \epsilon \varepsilon $\epsilon$, $\varepsilon$, \phi \varphi $\phi$, $\varphi$, and others.

  4. For superscripts and subscripts, use ^ and _. For example, x_i^2: $x_i^2$, \log_2 x: $\log_2 x$.

  5. Groups. Superscripts, subscripts, and other operations apply only to the next “group”. A “group” is either a single symbol, or any formula surrounded by curly braces {}. If you do 10^10, you will get a surprise: $10^10$. But 10^{10} gives what you probably wanted: $10^{10}$. Use curly braces to delimit a formula to which a superscript or subscript applies: x^5^6 is an error; {x^y}^z is ${x^y}^z$, and x^{y^z} is $x^{y^z}$. Observe the difference between x_i^2 $x_i^2$ and x_{i^2} $x_{i^2}$.

  6. Parentheses Ordinary symbols ()[] make parentheses and brackets $(2+3)[4+4]$. Use \{ and \} for curly braces $\{\}$.

    These do not scale with the formula in between, so if you write (\frac{\sqrt x}{y^3}) the parentheses will be too small: $(\frac{\sqrt x}{y^3})$. Using \left(\right) will make the sizes adjust automatically to the formula they enclose: \left(\frac{\sqrt x}{y^3}\right) is $\left(\frac{\sqrt x}{y^3}\right)$.

    \left and\right apply to all the following sorts of parentheses: ( and ) $(x)$, [ and ] $[x]$, \{ and \} $\{ x \}$, | $|x|$, \vert $\vert x \vert$, \Vert $\Vert x \Vert$, \langle and \rangle $\langle x \rangle$, \lceil and \rceil $\lceil x \rceil$, and \lfloor and \rfloor $\lfloor x \rfloor$. \middle can be used to add additional dividers. There are also invisible parentheses, denoted by .: \left.\frac12\right\rbrace is $\left.\frac12\right\rbrace$.

    If manual size adjustments are required: \Biggl(\biggl(\Bigl(\bigl((x)\bigr)\Bigr)\biggr)\Biggr) gives $\Biggl(\biggl(\Bigl(\bigl((x)\bigr)\Bigr)\biggr)\Biggr)$.

  7. Sums and integrals \sum and \int; the subscript is the lower limit and the superscript is the upper limit, so for example \sum_1^n $\sum_1^n$. Don't forget {} if the limits are more than a single symbol. For example, \sum_{i=0}^\infty i^2 is $\sum_{i=0}^\infty i^2$. Similarly, \prod $\prod$, \int $\int$, \bigcup $\bigcup$, \bigcap $\bigcap$, \iint $\iint$, \iiint $\iiint$, \idotsint $\idotsint$.

  8. Fractions There are three ways to make these. \frac ab applies to the next two groups, and produces $\frac ab$; for more complicated numerators and denominators use {}: \frac{a+1}{b+1} is $\frac{a+1}{b+1}$. If the numerator and denominator are complicated, you may prefer \over, which splits up the group that it is in: {a+1\over b+1} is ${a+1\over b+1}$. Using \cfrac{a}{b} command is useful for continued fractions $\cfrac{a}{b}$, more details for which are given in this sub-article.

  9. Fonts

  • Use \mathbb or \Bbb for "blackboard bold": $\mathbb{CHNQRZ}$.
  • Use \mathbf for boldface: $\mathbf{ABCDEFGHIJKLMNOPQRSTUVWXYZ}$ $\mathbf{abcdefghijklmnopqrstuvwxyz}$.
    • For expression based characters, use \boldsymbol instead: $\boldsymbol{\alpha}$
  • Use \mathit for italics: $\mathit{ABCDEFGHIJKLMNOPQRSTUVWXYZ}$ $\mathit{abcdefghijklmnopqrstuvwxyz}$.
  • Use \pmb for boldfaced italics: $\pmb{ABCDEFGHIJKLMNOPQRSTUVWXYZ}$ $\pmb{abcdefghijklmnopqrstuvwxyz}$.
  • Use \mathtt for "typewriter" font: $\mathtt{ABCDEFGHIJKLMNOPQRSTUVWXYZ}$ $\mathtt{abcdefghijklmnopqrstuvwxyz}$.
  • Use \mathrm for roman font: $\mathrm{ABCDEFGHIJKLMNOPQRSTUVWXYZ}$ $\mathrm{abcdefghijklmnopqrstuvwxyz}$.
  • Use \mathsf for sans-serif font: $\mathsf{ABCDEFGHIJKLMNOPQRSTUVWXYZ}$ $\mathsf{abcdefghijklmnopqrstuvwxyz}$.
  • Use \mathcal for "calligraphic" letters: $\mathcal{ ABCDEFGHIJKLMNOPQRSTUVWXYZ}$ $\mathcal{abcdefghijklmnopqrstuvwxyz}$
  • Use \mathscr for script letters: $\mathscr{ABCDEFGHIJKLMNOPQRSTUVWXYZ}$ $\mathscr{abcdefghijklmnopqrstuvwxyz}$
  • Use \mathfrak for "Fraktur" (old German style) letters: $\mathfrak{ABCDEFGHIJKLMNOPQRSTUVWXYZ} \mathfrak{abcdefghijklmnopqrstuvwxyz}$.
  1. Radical signs / roots Use sqrt, which adjusts to the size of its argument: \sqrt{x^3} $\sqrt{x^3}$; \sqrt[3]{\frac xy} $\sqrt[3]{\frac xy}$. For complicated expressions, consider using {...}^{1/2} instead.

  2. Some special functions such as "lim", "sin", "max", "ln", and so on are normally set in roman font instead of italic font. Use \lim, \sin, etc. to make these: \sin x $\sin x$, not sin x $sin x$. Use subscripts to attach a notation to \lim: \lim_{x\to 0} $$\lim_{x\to 0}$$ Nonstandard function names can be set with \operatorname{foo}(x) $\operatorname{foo}(x)$.

  3. There are a very large number of special symbols and notations, too many to list here; see this shorter listing, or this exhaustive listing. Some of the most common include:

  • \lt \gt \le \leq \leqq \leqslant \ge \geq \geqq \geqslant \neq $\lt$, $\gt$, $\le$, $\leq$, $\leqq$, $\leqslant$, $\ge$, $\geq$, $\geqq$, $\geqslant$, $\neq$. You can use \not to put a slash through almost anything: \not\lt $\not\lt$ but it often looks bad.
  • \times \div \pm \mp $\times$, $\div$, $\pm$, $\mp$. \cdot is a centered dot: $x\cdot y$
  • \cup \cap \setminus \subset \subseteq \subsetneq \supset \in \notin \emptyset \varnothing $\cup$, $\cap$, $\setminus$, $\subset$, $\subseteq$, $\subsetneq$, $\supset$, $\in$, $\notin$, $\emptyset$, $\varnothing$
  • {n+1 \choose 2k} or \binom{n+1}{2k} ${n+1 \choose 2k}$
  • \to \rightarrow \leftarrow \Rightarrow \Leftarrow \mapsto $\to$, $\rightarrow$, $\leftarrow$, $\Rightarrow$, $\Leftarrow$, $\mapsto$
  • \land \lor \lnot \forall \exists \top \bot \vdash \vDash $\land$, $\lor$, $\lnot$, $\forall$, $\exists$, $\top$, $\bot$, $\vdash$, $\vDash$
  • \star \ast \oplus \circ \bullet $\star$, $\ast$, $\oplus$, $\circ$, $\bullet$
  • \approx \sim \simeq \cong \equiv \prec \lhd \therefore $\approx$, $\sim $, $\simeq$, $\cong$, $\equiv$, $\prec$, $\lhd$, $\therefore$
  • \infty \aleph_0 $\infty\, \aleph_0$ \nabla \partial $\nabla$, $\partial$ \Im \Re $\Im$, $\Re$
  • For modular equivalence, use \pmod like this: a\equiv b\pmod n $a\equiv b\pmod n$.
  • For the binary mod operator, use \bmod like this: a\bmod 17 $a\bmod 17$.
  • Avoid using \mod, as it produces extra space: compare the above with a\mod 17 $a\mod 17$.
  • \ldots is the dots in $a_1, a_2, \ldots ,a_n$ \cdots is the dots in $a_1+a_2+\cdots+a_n$
  • Script lowercase l is \ell $\ell$.

Detexify lets you draw a symbol on a web page and then lists the $\TeX$ symbols that seem to resemble it. These are not guaranteed to work in MathJax but are a good place to start. To check that a command is supported, note that MathJax.org maintains a list of currently supported $\LaTeX$ commands, and one can also check Dr. Carol JVF Burns's page of $\TeX$ Commands Available in MathJax.

  1. Spaces MathJax usually decides for itself how to space formulas, using a complex set of rules. Putting extra literal spaces into formulas will not change the amount of space MathJax puts in: a␣b and a␣␣␣␣b are both $a b$. To add more space, use \, for a thin space $a\,b$; \; for a wider space $a\;b$. \quad and \qquad are large spaces: $a\quad b$, $a\qquad b$.

To set plain text, use \text{…}: $\{x\in s\mid x\text{ is extra large}\}$. You can nest $…$ inside of \text{…}, for example to access spaces.

  1. Accents and diacritical marks Use \hat for a single symbol $\hat x$, \widehat for a larger formula $\widehat{xy}$. If you make it too wide, it will look silly. Similarly, there are \bar $\bar x$ and \overline $\overline{xyz}$, and \vec $\vec x$ and \overrightarrow $\overrightarrow{xy}$ and \overleftrightarrow $\overleftrightarrow{xy}$. For dots, as in $\frac d{dx}x\dot x = \dot x^2 + x\ddot x$, use \dot and \ddot.

  2. Special characters used for MathJax interpreting can be escaped using the \ character: \\\$ $\$$, \{ $\{$, \_ $\_$, etc. If you want \ itself, you should use \backslash (symbol) or \setminus (binary operation) for $\backslash$, because \\ is for a new line.

(Tutorial ends here.)


It is important that this note be reasonably short and not suffer from too much bloat. To include more topics, please create short addenda and post them as answers instead of inserting them into this post.

Contents

Alphabetical list of links to To MathJax Topics, by title:

$\endgroup$
82
  • 32
    $\begingroup$ Some capital Greek letters are the same as the Roman equivalents, so they are not separated in $\LaTeX$. For a capital beta, one must use something like \mathrm{B}: $\mathrm{B}$ $\endgroup$
    – robjohn Mod
    Aug 28 '12 at 2:06
  • 10
    $\begingroup$ Two related questions: How do I insert a table when asking a question? and How to show the integral symbol on this site? $\endgroup$ Aug 28 '12 at 13:26
  • 33
    $\begingroup$ A quick addition to point 11: If you want to use a $\sin$-like symbol that is not already defined, the command is \operatorname: e.g., \operatorname{Spec} A gives $\operatorname{Spec} A$. $\endgroup$ Aug 28 '12 at 16:45
  • 21
    $\begingroup$ It might be useful to mention hanging subscripts for things like _5C_3 $_5C_3$. You could also mention \frac vs \dfrac. $\endgroup$
    – axblount
    Aug 29 '12 at 18:09
  • 8
    $\begingroup$ My basic idea is that if a beginner can express a formula clearly, then someone else can come in and clean up the typesetting afterwards. I am considering getting rid of the section about \big, \left, and \right for this reason, and trimming the section on spacing. $\endgroup$
    – MJD
    Aug 30 '12 at 2:06
  • 9
    $\begingroup$ Most of the references to TeX or LaTeX in this and the answers ought to be to MathJaX (the exception that I can see being the output of Detexify). I know this is a bit pedantic, but would it be alright to correct this? $\endgroup$ Sep 11 '12 at 14:13
  • 5
    $\begingroup$ @AndrewStacey Thanks for pointing this out. Let's by all means be as correct as possible, particularly when there's no extra cost. $\endgroup$
    – MJD
    Sep 11 '12 at 14:15
  • 4
    $\begingroup$ @MJD Okay, I've had a go (also the answer about arrays). I wonder also whether or not it is worth a sentence at the end pointing out that whilst MathJaX does its best to emulate TeX, it isn't TeX and so while knowing how something is done in TeX gives you a starting point, it isn't a guarantee that the same thing works in MathJaX. (As a case in point, questions about MathJaX are generally off-topic over on TeX-SX.) $\endgroup$ Sep 11 '12 at 14:22
  • 6
    $\begingroup$ @AndrewStacey I wouldn't. They are close enough that it seems to me to be a needless refinement. I might even argue that MathJax is $\TeX$, although an alternative implementation. We're willing to accept that other programming languages (JavaScript, for example) that have slightly incompatible implementations are nevertheless the same language; why not in this case as well? $\endgroup$
    – MJD
    Sep 11 '12 at 14:35
  • 11
    $\begingroup$ @MJD Except that this is meant as a tutorial for those who aren't familiar with the distinction (and there really is a distinction: "slightly incompatible implementations" doesn't really fit the bill here). One thing tutorials often include is a "Where to find out more" section. This doesn't. Someone who doesn't know the distinction might be tempted to search for help on TeX or LaTeX instead and wonder why it doesn't work. $\endgroup$ Sep 11 '12 at 14:40
  • 6
    $\begingroup$ @AndrewStacey All the tips given here would work in any $\TeX$/$\LaTeX$ environment with the proper packages. MathJax is just the service used to render it. You wouldn't say "Miktex tutorial" or "texlive tutorial". $\endgroup$
    – axblount
    Sep 11 '12 at 15:01
  • 6
    $\begingroup$ @axblount But that's precisely the wrong way around to think about it! The likelihood is that someone will look at this tutorial to figure out how to write something on the Maths-SX site: i.e., to use MathJaX. If they can't find help here, where do they go? If they have the idea that MathJaX is "just a javascript implementation of TeX" then they might think to look for help with TeX, but that is quite possibly not going to be helpful. $\endgroup$ Sep 11 '12 at 15:08
  • 9
    $\begingroup$ @axblount For a start, you've changed the goalposts: "LaTeX math expressions". LaTeX is so much more than just a way of typesetting maths! Second, I don't really know but it wouldn't take me long to cook one up. I don't use MathJaX so I haven't explored it. But I know, for example, that it can't handle catcode changes. Which means that I can't make ( and ) automatically resizeable. I can in LaTeX. $\endgroup$ Sep 11 '12 at 16:04
  • 81
    $\begingroup$ I wish I saw this post when I first joined. This post should be a main link on the home page. There should be a button under each box: NEW TO LATEX, CLICK HERE FOR EXAMPLES. This is extremely useful, concise. $\endgroup$ May 31 '13 at 18:09
  • 15
    $\begingroup$ @MJD: I use \mathrm in many places; e.g. $\mathrm{d}x$ in integrals and derivatives and for operator names that don't need the full force of \operatorname. \mathrm was intended for roman symbols in math mode; \text was intended for text because of the way it spaces things. See this TEX thread. Since I don't believe we can use preambles in MathJax, we can't use \DeclareMathOperator, though we can use \newcommand, but that is orthogonal to the use of \mathrm vs \text for math symbols. $\endgroup$
    – robjohn Mod
    Jun 10 '13 at 16:23

37 Answers 37

406
$\begingroup$

Matrices

  1. Use $$\begin{matrix}…\end{matrix}$$ In between the \begin and \end, put the matrix elements. End each matrix row with \\, and separate matrix elements with &. For example,

    $$
        \begin{matrix}
        1 & x & x^2 \\
        1 & y & y^2 \\
        1 & z & z^2 \\
        \end{matrix}
    $$
    

    produces:

    $$ \begin{matrix} 1 & x & x^2 \\ 1 & y & y^2 \\ 1 & z & z^2 \\ \end{matrix} $$

    MathJax will adjust the sizes of the rows and columns so that everything fits.

  2. To add brackets, either use \left…\right as in section 6 of the tutorial, or replace matrix with pmatrix $\begin{pmatrix}1&2\\3&4\\ \end{pmatrix}$, bmatrix $\begin{bmatrix}1&2\\3&4\\ \end{bmatrix}$, Bmatrix $\begin{Bmatrix}1&2\\3&4\\ \end{Bmatrix}$, vmatrix $\begin{vmatrix}1&2\\3&4\\ \end{vmatrix}$, Vmatrix $\begin{Vmatrix}1&2\\3&4\\ \end{Vmatrix}$.

  3. Use \cdots $\cdots$ \ddots $\ddots$ vdots $\vdots$ when you want to omit some of the entries:

    $$\begin{pmatrix} 1 & a_1 & a_1^2 & \cdots & a_1^n \\ 1 & a_2 & a_2^2 & \cdots & a_2^n \\ \vdots & \vdots& \vdots & \ddots & \vdots \\ 1 & a_m & a_m^2 & \cdots & a_m^n \end{pmatrix}$$

  4. For horizontally "augmented" matrices, put parentheses or brackets around a suitably-formatted table; see arrays below for details. Here is an example:

    $$ \left[\begin{array}{cc|c} 1&2&3\\ 4&5&6 \end{array}\right] $$

    is produced by:

    $$ \left[
    \begin{array}{cc|c}
      1&2&3\\
      4&5&6
    \end{array}
    \right] $$
    

    The cc|c is the crucial part here; it says that there are three centered columns with a vertical bar between the second and third.

  5. For vertically "augmented" matrices, use \hline. For example

$$ \begin{pmatrix} a & b \\ c & d\\ \hline 1 & 0\\ 0 & 1 \end{pmatrix} $$ is produced by

$$
  \begin{pmatrix}
    a & b\\
    c & d\\
  \hline
    1 & 0\\
    0 & 1
  \end{pmatrix}
$$
  1. For small inline matrices use \bigl(\begin{smallmatrix} ... \end{smallmatrix}\bigr), e.g. $\bigl( \begin{smallmatrix} a & b \\ c & d \end{smallmatrix} \bigr)$ is produced by:

     $\bigl( \begin{smallmatrix} a & b \\ c & d \end{smallmatrix} \bigr)$
    
$\endgroup$
7
  • 22
    $\begingroup$ This says "End each matrix row with \\". But there is no reason to end the LAST row of the matrix that way. The double backslash means: now go on to the next row. But there isn't any next row after the last one. $\endgroup$ Aug 28 '14 at 5:15
  • 4
    $\begingroup$ I can't edit, but that could be phrased "Separate matrix rows with \\". $\endgroup$
    – trichoplax
    Nov 18 '16 at 9:43
  • $\begingroup$ as in section 6 of the tutorial... Which tutorial? Is there a link to this tutorial section? $\endgroup$
    – Tom Hale
    May 21 '17 at 3:35
  • $\begingroup$ @tom The tutorial is at the top of this page. It has numbered sections. $\endgroup$
    – MJD
    May 21 '17 at 17:04
  • 5
    $\begingroup$ @MichaelHardy but a \\ on every line is harmless, and it makes the editing of matrices easier because swapping with the last line can be done with one quick keystroke in many editors. $\endgroup$
    – Reb.Cabin
    Feb 8 '18 at 15:18
  • $\begingroup$ Is it possible to get smallpmatrix or something? $\endgroup$ Aug 13 '18 at 5:49
  • $\begingroup$ There's something strange about the second matrix (right after "produces:"); it seems to render properly as a matrix on this page, but on the revision permalink math.meta.stackexchange.com/revisions/5023/7 it shows up as raw LaTeX in a code block. $\endgroup$
    – j.c.
    Feb 16 at 21:31
298
$\begingroup$

Symbols

In general, you have to search in long tables about a specific symbol you're looking for, things like $\Psi$, $\delta$, $\zeta$, $\ge$, $\subseteq$ ... And it turns out that this operation can be frustrating and time consuming, which can cause the buddy to abandon writing the complete $\LaTeX$ sentence in his answer, or in some cases, the complete answer itself.

That's why the tool that I will present you in this post was conceived. Basically, it is a $\LaTeX$ handwritten symbol recognition. Example in image:

enter image description here

Here is the website: Detexify² No more frustration.

$\endgroup$
4
  • 4
    $\begingroup$ How to implement usepackage ? I'd like to have \iddots from package mathdots available. $\endgroup$ Jun 15 '16 at 11:05
  • 1
    $\begingroup$ @GottfriedHelms see this question - I think the answer is "you can't do that"... $\endgroup$
    – Floris
    Jun 27 '17 at 22:40
  • $\begingroup$ @Floris: thanks, that is indeed the informative answer! $\endgroup$ Jun 28 '17 at 0:35
  • 1
    $\begingroup$ It recognized my horrible drawing with a finger on my notebook's mousepad! Unfortunately the symbol it recognized (mapsfrom) isn't part of MathJax $\endgroup$ Oct 24 '19 at 5:55
288
$\begingroup$

Aligned equations

Often people want a series of equations where the equals signs are aligned. To get this, use \begin{align}…\end{align}. Each line should end with \\, and should contain an ampersand at the point to align at, typically immediately before the equals sign.

For example,

\begin{align} \sqrt{37} & = \sqrt{\frac{73^2-1}{12^2}} \\ & = \sqrt{\frac{73^2}{12^2}\cdot\frac{73^2-1}{73^2}} \\ & = \sqrt{\frac{73^2}{12^2}}\sqrt{\frac{73^2-1}{73^2}} \\ & = \frac{73}{12}\sqrt{1 - \frac{1}{73^2}} \\ & \approx \frac{73}{12}\left(1 - \frac{1}{2\cdot73^2}\right) \end{align}

is produced by

\begin{align}
\sqrt{37} & = \sqrt{\frac{73^2-1}{12^2}} \\
 & = \sqrt{\frac{73^2}{12^2}\cdot\frac{73^2-1}{73^2}} \\ 
 & = \sqrt{\frac{73^2}{12^2}}\sqrt{\frac{73^2-1}{73^2}} \\
 & = \frac{73}{12}\sqrt{1 - \frac{1}{73^2}} \\ 
 & \approx \frac{73}{12}\left(1 - \frac{1}{2\cdot73^2}\right)
\end{align}

The usual $$ marks that delimit the display may be omitted here.

$\endgroup$
20
  • 7
    $\begingroup$ The AMS's Short Math Guide recommends the align environment over eqnarray in LaTeX. In MathJax the spacing seems to be the same , but align requires one less ampersand per line. $\endgroup$
    – user856
    Aug 28 '12 at 4:41
  • 3
    $\begingroup$ Thanks. I was not sure whether to discuss that. A detailed argument against eqnarray is in this article. $\endgroup$
    – MJD
    Aug 28 '12 at 4:51
  • $\begingroup$ Would you mind if I changed your example to use align then? $\endgroup$
    – user856
    Aug 28 '12 at 5:34
  • $\begingroup$ @Rahul: Please go ahead and change anything that seems good to change. This is all CW. $\endgroup$
    – MJD
    Aug 28 '12 at 5:44
  • $\begingroup$ Also, if you think you have a better example, please use it; I used the first one I found. $\endgroup$
    – MJD
    Aug 28 '12 at 5:49
  • 7
    $\begingroup$ Correct me if I'm wrong, but I don't believe the \$\$ is necessary before and after the \begin{align}. I've certainly never used it. From experience, the \begin{align} puts you into math-display mode by itself. $\endgroup$
    – TravisJ
    Apr 21 '15 at 12:24
  • 7
    $\begingroup$ I sometimes find that one line of this environment is too close to another, making them uncomfortable to read. The interline spacing can be adjusted by using input such as \\[1ex] instead of \\ . (And of course the 1 can be changed to another value such as 1.5 or .7 in order to get enough space but not too much.) $\endgroup$
    – David K
    Jan 30 '16 at 16:29
  • $\begingroup$ Note - in Jekyll I had to add an additional `\` to break lines. $\endgroup$
    – baxx
    May 24 '16 at 17:45
  • 3
    $\begingroup$ @MJD i put the equation: \begin{align} f(x)&=\left(x^3\right)+\left(x^3+x^2+x^1\right)+\left(x^3+x^2\right)\\ f'(x)&=\left(\left(3x^2+2x+1\right)+\left(3x^2+2x\right)\\ f''(x)&=\left(6x+2\right)\\ \end{align} but the third & is a problem: according to Mathjax, it does not go there. then when i remove it, it says that "a missing close brace or unclosed brace" is present. what am i doing wrong? $\endgroup$ Apr 26 '17 at 21:25
  • 5
    $\begingroup$ @AlexanderDay How did you used that boxed quote? What are commands or formats for it? $\endgroup$ May 21 '17 at 15:53
  • $\begingroup$ @AlexanderDay I notice that right after your second &=, you have two instances of \left( in a row, and one of them is not closed. Removing one of them, I get: $\begin{align} f(x)&=\left(x^3\right)+\left(x^3+x^2+x^1\right)+\left(x^3+x^‌​2\right)\\ f'(x)&=\left(3x^2+2x+1\right)+\left(3x^2+2x\right)\\ f''(x)&=\left(6x+2\right)\\ \end{align}$ Is that what you expected? $\endgroup$ Oct 2 '17 at 13:31
  • 3
    $\begingroup$ @AlwaysConfused the box delimits a MathJax formula with a syntax error. $\endgroup$ Oct 2 '17 at 13:32
  • $\begingroup$ There is a difference between aligned and align environment. Equation above are just one equation with different representations, rather than multiple aligned equations. I think the correct environment is aligned. $\endgroup$
    – jdhao
    Jan 26 '18 at 1:40
  • 1
    $\begingroup$ The align* variation does two things differently: left-aligns the equations and omits equation numbers; the regular align environment centers the equations and puts an equation number on each line, at least in my version of Jupyter notebooks. $\endgroup$
    – Reb.Cabin
    Feb 6 '18 at 19:24
  • $\begingroup$ @Reb.Cabin I think the thing is that this is MathJaX. When I am writing stuff in LaTeX I always use align*, but I cannot see the difference between align and align* here, in MathJaX world. $\endgroup$
    – user1729
    Aug 14 '18 at 14:36
239
$\begingroup$

Definitions by cases (piecewise functions)

Use \begin{cases}…\end{cases}. End each case with a \\, and use & before parts that should be aligned.

For example, you get this:

$$f(n) = \begin{cases} n/2, & \text{if $n$ is even} \\ 3n+1, & \text{if $n$ is odd} \end{cases}$$

by writing this:

  f(n) =
\begin{cases}
n/2,  & \text{if $n$ is even} \\
3n+1, & \text{if $n$ is odd}
\end{cases}

The brace can be moved to the right: $$ \left. \begin{array}{l} \text{if $n$ is even:}&n/2\\ \text{if $n$ is odd:}&3n+1 \end{array} \right\} =f(n) $$ by writing this:

\left.
\begin{array}{l}
\text{if $n$ is even:}&n/2\\
\text{if $n$ is odd:}&3n+1
\end{array}
\right\}
=f(n)

To get a larger vertical space between cases we can use \\[2ex] instead of \\. For example, you get this:

$$f(n) = \begin{cases} \frac{n}{2}, & \text{if $n$ is even} \\[2ex] 3n+1, & \text{if $n$ is odd} \end{cases}$$

by writing this:

f(n) =
\begin{cases}
\frac{n}{2},  & \text{if $n$ is even} \\[2ex]
3n+1, & \text{if $n$ is odd}
\end{cases}

(An ‘ex’ is a length equal to the height of the letter x; 2ex here means the space should be two exes high.)

$\endgroup$
5
  • $\begingroup$ @MJD Do we have to use the additional instruction \displaystyle when the formulas displayed are more complex ? $\endgroup$
    – jibe
    Jul 1 '14 at 14:43
  • 3
    $\begingroup$ @jibs \displaystyle is enabled automatically in displays, for example between $$…$$. You should not ever have to use it. $\endgroup$
    – MJD
    Jul 1 '14 at 14:50
  • $\begingroup$ @jibe In general, the separate cases in this notation should be in text style unless they are very very complex (and then, the { notation is just wrong anyways). $\endgroup$
    – yo'
    Aug 25 '14 at 9:53
  • $\begingroup$ can this be written with ascii math instead of latex @MJD $\endgroup$
    – wrufesh
    May 24 '18 at 7:35
  • $\begingroup$ What an absurd function to use as an example. Nobody would ever consider such a function. $\endgroup$ Oct 30 '18 at 21:56
165
$\begingroup$

Arrays

It is often easier to read tables formatted in MathJax rather than plain text or a fixed width font. Arrays and tables are created with the array environment. Just after \begin{array} the format of each column should be listed, use c for a center aligned column, r for right aligned, l for left aligned and a | for a vertical line. Just as with matrices, cells are separated with & and rows are broken using \\. A horizontal line spanning the array can be placed before the current line with \hline.

For example, $$\begin{array}{c|lcr} n & \text{Left} & \text{Center} & \text{Right} \\ \hline 1 & 0.24 & 1 & 125 \\ 2 & -1 & 189 & -8 \\ 3 & -20 & 2000 & 1+10i \end{array} $$

$$
\begin{array}{c|lcr}
n & \text{Left} & \text{Center} & \text{Right} \\
\hline
1 & 0.24 & 1 & 125 \\
2 & -1 & 189 & -8 \\
3 & -20 & 2000 & 1+10i
\end{array}
$$

Arrays can be nested to make an array of tables.

For example, $$ % outer vertical array of arrays \begin{array}{c} % inner horizontal array of arrays \begin{array}{cc} % inner array of minimum values \begin{array}{c|cccc} \text{min} & 0 & 1 & 2 & 3\\ \hline 0 & 0 & 0 & 0 & 0\\ 1 & 0 & 1 & 1 & 1\\ 2 & 0 & 1 & 2 & 2\\ 3 & 0 & 1 & 2 & 3 \end{array} & % inner array of maximum values \begin{array}{c|cccc} \text{max}&0&1&2&3\\ \hline 0 & 0 & 1 & 2 & 3\\ 1 & 1 & 1 & 2 & 3\\ 2 & 2 & 2 & 2 & 3\\ 3 & 3 & 3 & 3 & 3 \end{array} \end{array} \\ % inner array of delta values \begin{array}{c|cccc} \Delta&0&1&2&3\\ \hline 0 & 0 & 1 & 2 & 3\\ 1 & 1 & 0 & 1 & 2\\ 2 & 2 & 1 & 0 & 1\\ 3 & 3 & 2 & 1 & 0 \end{array} \end{array} $$

As the source for the preceding array is long, please right-click on one of the tables and choose $\mathsf{Show\ Math\ As\ }\blacktriangleright\mathsf{\ TeX\ Commands}$.

$\endgroup$
12
  • 13
    $\begingroup$ You'll have to wrap the contents of each cell in \text if you don't want $all italics, weird-looking spacing, an' odd apostrophes$. $\endgroup$
    – user856
    Aug 29 '12 at 21:30
  • $\begingroup$ @RahulNarain: True. I used words just for illustration, but I guess the example was slightly misleading. If you'd like to modify it please go ahead. $\endgroup$
    – axblount
    Aug 29 '12 at 22:00
  • 2
    $\begingroup$ Thanks! I like your numeric example better, since the widths of the entries are different enough that the alignment differences are visually clear. $\endgroup$
    – MJD
    Aug 30 '12 at 1:37
  • $\begingroup$ @robjohn how do you use | while typing , i don't find it in my keyboard...... $\endgroup$
    – ABC
    Mar 28 '13 at 12:05
  • $\begingroup$ @exploringnet: on my keyboard, it is the shifted backslash. It may be in different places (or absent) depending on your keyboard. On my mobile device (iPhone), it is in the shifted numerics, to the right of the backslash. In mathmode, \vert gives $\vert$ and \mid gives $\mid$, but neither works in the column spec for an array. If you cannot type it on your keyboard, you can alwaays copy and paste it from another document. $\endgroup$
    – robjohn Mod
    Mar 28 '13 at 17:39
  • $\begingroup$ It should perhaps be mentioned, that in nested arrays there seems to be no option to synchronize column-widths and/or row-heights over the top-level. I didn't find a solution such that if two arrays are stacked vertically one could make their column-widths matching/fit. $\endgroup$ Aug 26 '13 at 9:16
  • 5
    $\begingroup$ This could also be convenient for some people, althought it destroys the joy of writing tables in $\LaTeX$ by hand! $\endgroup$ Jun 3 '14 at 14:18
  • $\begingroup$ @Rahul: why did regulars not press developers to enhance HTML formatting instead of doing inconvenient and resource-devouring detours through MathJax? When a table contains (mostly) formulæ, the use of a formula-formatting engine looks determined. But when one wants just a table, why should it run software with completely different purpose? I once tried to speak about it at meta.SE, but was gagged. $\endgroup$ Dec 3 '14 at 12:11
  • $\begingroup$ @IncnisMrsi What kind of pressure could we apply: bribery, threats, kidnapping? A feature request was made, supported by SE communities, and declined by SE (on technical grounds, as they say). At least we have the MathJax workaround, with all of its flaws: SO and others have nothing. $\endgroup$
    – user147263
    Dec 3 '14 at 15:55
  • 8
    $\begingroup$ Center Aligned Table Captions with Left Aligned Contents \begin{array}{ll} \hfill\mathrm{Bad}\hfill & \hfill\mathrm{Better}\hfill \\ \hline \\ e^{i\frac{\pi}2} \quad e^{\frac{i\pi}2}& e^{i\pi/2} \\ \int_{-\frac\pi2}^\frac\pi2 \sin x\,dx & \int_{-\pi/2}^{\pi/2}\sin x\,dx \\ \end{array} $\endgroup$ Dec 12 '16 at 16:41
  • $\begingroup$ P.S. Table copied from MJD's example below so as to make an example with cells much wider than the caption. $\endgroup$ Dec 12 '16 at 16:47
  • $\begingroup$ How would you make a tic-tac-toe grid, framed or not framed? And how would you construct an array on the right side? $\endgroup$
    – Mr Pie
    Nov 19 '17 at 0:23
157
$\begingroup$

Fussy spacing issues

These are issues that won't affect the correctness of formulas, but might make them look significantly better or worse. Beginners should feel free to ignore this advice; someone else will correct it for them, or more likely nobody will care.

Don't use \frac in exponents or limits of integrals; it looks bad and can be confusing, which is why it is rarely done in professional mathematical typesetting. Write the fraction horizontally, with a slash:

$$\begin{array}{cc} \mathrm{Bad} & \mathrm{Better} \\ \hline \\ e^{i\frac{\pi}2} \quad e^{\frac{i\pi}2}& e^{i\pi/2} \\ \int_{-\frac\pi2}^\frac\pi2 \sin x\,dx & \int_{-\pi/2}^{\pi/2}\sin x\,dx \\ \end{array}$$

The | symbol has the wrong spacing when it is used as a divider, for example in set comprehensions. Use \mid instead:

$$\begin{array}{cc} \mathrm{Bad} & \mathrm{Better} \\ \hline \\ \{x|x^2\in\Bbb Z\} & \{x\mid x^2\in\Bbb Z\} \\ \end{array}$$

When using stretchable delimiters (i.e. with \left and \right), it may be preferable to use \,\middle|\,. This produces a stretchable vertical bar with a little bit of space around it. Another alternative is to use a colon instead.

$$\begin{array}{cc} \mathrm{Bad} & \mathrm{Better} \\ \hline \\ \left\{\dfrac{m}{n} \mid m,n\in\Bbb Z\right\} & \left\{\dfrac{m}{n} \,\middle|\, m,n\in\Bbb Z\right\} \\ \end{array}$$

For double and triple integrals, don't use \int\int or \int\int\int. Instead use the special forms \iint and \iiint: $$\begin{array}{cc} \mathrm{Bad} & \mathrm{Better} \\ \hline \\ \int\int_S f(x)\,dy\,dx & \iint_S f(x)\,dy\,dx \\ \int\int\int_V f(x)\,dz\,dy\,dx & \iiint_V f(x)\,dz\,dy\,dx \end{array}$$

Use \, to insert a thin space before differentials; without this $\TeX$ will mash them together:

$$\begin{array}{cc} \mathrm{Bad} & \mathrm{Better} \\ \hline \\ \iiint_V f(x)dz dy dx & \iiint_V f(x)\,dz\,dy\,dx \end{array}$$

$\endgroup$
11
  • 3
    $\begingroup$ I think the first adjusted fraction looks better than the original, but I don't like the second. In any case, this minor spacing imbalance is too peripheral to belong in a basic MathJax tutorial IMO. Too likely to scare people away rather than make them feel helped. $\endgroup$ Aug 31 '12 at 21:05
  • 2
    $\begingroup$ @Henning Do you mean that the fraction example is too unimportant even to appear in an addendum on fussy spacing, or that the fussy spacing article is too unimportant to appear as an addendum to the tutorial? $\endgroup$
    – MJD
    Aug 31 '12 at 23:57
  • 2
    $\begingroup$ I was talking specifically about the fraction example. Mostly I'm concerned that somebody will come away thinking, Eeek! Do I have to worry about THAT to use the site? But it's also arguable that the disclaimer at the top of the answer ought to take care of that. $\endgroup$ Sep 1 '12 at 21:13
  • 2
    $\begingroup$ @MJD I like the less space, but what if we want to list the bounds for multiple integrals? Like if we have say 3 integrals and we have 3 separate bounds for each how would we list each one? Or do we have to do \int_bound1^bound2\int_bound3^bound4\int_bound5^bound6?? $\endgroup$ Nov 19 '12 at 19:45
  • 2
    $\begingroup$ @Kyle I think that's exactly what you do in that case. $\endgroup$
    – MJD
    Nov 19 '12 at 20:09
  • 30
    $\begingroup$ Worth nothing you can use \middle with | to get it to work with \left and \right, like \left\{x\middle | \frac{x^2}{2} \in \mathbb{z}\right\}: $\left\{x\middle | \frac{x^2}{2} \in \mathbb{z}\right\}$ $\endgroup$
    – asmeurer
    Jun 9 '13 at 22:49
  • 1
    $\begingroup$ Thanks very much! I wanted to do that, but didn't know how. $\endgroup$
    – MJD
    Jun 10 '13 at 15:47
  • $\begingroup$ @asmeurer Don't forget the spacing around the bar. $\endgroup$
    – user76284
    Apr 26 '18 at 19:30
  • 1
    $\begingroup$ It seems \middle \mid doesn't work. What is the correct way to get the right spacing with automatic vertical resizing? $\endgroup$
    – asmeurer
    Apr 26 '18 at 20:05
  • $\begingroup$ In the case of base $e$ powers I would recommend using $\exp(i\pi/2)$ which is, in my opinion, even better than what's suggested in this post. $\endgroup$ Jun 9 '18 at 23:08
  • $\begingroup$ @asmeurer I always use \left\{\, ... \,\middle|\, ... \,\right\} like in $\left\{\,x\in\Bbb R\,\middle|\, \frac{x^2}{2}\in\Bbb Z\,\right\}$. $\endgroup$
    – Christoph
    Dec 17 '18 at 21:14
131
$\begingroup$

Crossing things out

Use \require{cancel} in the first formula in your post that requires cancelling; you need it only once per page. Then use:

$$\require{cancel}\begin{array}{rl} \verb|y+\cancel{x}| & y+\cancel{x}\\ \verb|\cancel{y+x}| & \cancel{y+x}\\ \verb|y+\bcancel{x}| & y+\bcancel{x}\\ \verb|y+\xcancel{x}| & y+\xcancel{x}\\ \verb|y+\cancelto{0}{x}| & y+\cancelto{0}{x}\\ \verb+\frac{1\cancel9}{\cancel95} = \frac15+& \frac{1\cancel9}{\cancel95} = \frac15 \\ \end{array} $$

Use \require{enclose} for the following:

$$\require{enclose}\begin{array}{rl} \verb|\enclose{horizontalstrike}{x+y}| & \enclose{horizontalstrike}{x+y}\\ \verb|\enclose{verticalstrike}{\frac xy}| & \enclose{verticalstrike}{\frac xy}\\ \verb|\enclose{updiagonalstrike}{x+y}| & \enclose{updiagonalstrike}{x+y}\\ \verb|\enclose{downdiagonalstrike}{x+y}| & \enclose{downdiagonalstrike}{x+y}\\ \verb|\enclose{horizontalstrike,updiagonalstrike}{x+y}| & \enclose{horizontalstrike,updiagonalstrike}{x+y}\\ \end{array} $$

\enclose can also produce enclosing boxes, circles, and other notations; see MathML menclose documentation for a complete list.

$\endgroup$
6
  • 24
    $\begingroup$ Can I use \enclose{counterstrike}? :P $\endgroup$ Jul 27 '15 at 19:19
  • 58
    $\begingroup$ That sneaky $19/95 = 1/5$. Nice one! $\endgroup$
    – Darth Geek
    Dec 8 '15 at 23:57
  • 28
    $\begingroup$ I see you can further resolve existing resolutions, $\cancelto{\cancelto{\cancelto{x^{2+x}}{\cancelto{x^2}{x}+4}}4}0$ $\endgroup$
    – alan2here
    May 1 '16 at 2:40
  • 2
    $\begingroup$ Is enclose a $\LaTeX$ package, or only a MathML option? $\endgroup$
    – Tim Thayer
    Nov 4 '16 at 18:51
  • 2
    $\begingroup$ Here is a related post on meta: Striking out equations. $\endgroup$ Mar 20 '19 at 3:15
  • $\begingroup$ Nice command to make an arrow: $\cancelto{}{}$ \cancelto{}{} $\endgroup$
    – math
    Jul 21 '20 at 11:32
117
$\begingroup$

System of equations

  • Use \begin{array}…\end{array} and \left\{…\right.. For example, you get this:

$$ \left\{ \begin{array}{c} a_1x+b_1y+c_1z=d_1 \\ a_2x+b_2y+c_2z=d_2 \\ a_3x+b_3y+c_3z=d_3 \end{array} \right. $$

by writing this:

$$
\left\{ 
\begin{array}{c}
a_1x+b_1y+c_1z=d_1 \\ 
a_2x+b_2y+c_2z=d_2 \\ 
a_3x+b_3y+c_3z=d_3
\end{array}
\right. 
$$
  • Alternatively we can use \begin{cases}…\end{cases}. The same system

$$ \begin{cases} a_1x+b_1y+c_1z=d_1 \\ a_2x+b_2y+c_2z=d_2 \\ a_3x+b_3y+c_3z=d_3 \end{cases} $$

is produced by the following code

$$\begin{cases}
a_1x+b_1y+c_1z=d_1 \\ 
a_2x+b_2y+c_2z=d_2 \\ 
a_3x+b_3y+c_3z=d_3
\end{cases}
$$
  • To align the = signs use \begin{aligned}...\end{aligned} and \left\{…\right. (see asmeurer's comment) $$\left\{\begin{aligned} a_1x+b_1y+c_1z&=d_1+e_1 \\ a_2x+b_2y&=d_2 \\ a_3x+b_3y+c_3z&=d_3 \end{aligned} \right. $$

whose code is

$$
\left\{
\begin{aligned} 
a_1x+b_1y+c_1z &=d_1+e_1 \\ 
a_2x+b_2y&=d_2 \\ 
a_3x+b_3y+c_3z &=d_3 
\end{aligned} 
\right. 
$$
  • To align the = signs and the terms as in $$\left\{\begin{array}{ll}a_1x+b_1y+c_1z &=d_1+e_1 \\ a_2x+b_2y &=d_2 \\ a_3x+b_3y+c_3z &=d_3 \end{array} \right.$$

use array with l (for "align left"; there are also c and r) parameters

$$
\left\{
\begin{array}{ll}
a_1x+b_1y+c_1z &=d_1+e_1 \\ 
a_2x+b_2y &=d_2 \\ 
a_3x+b_3y+c_3z &=d_3 
\end{array} 
\right.
$$
  • Vertical space between equations. As explained in Definition by cases to get a larger vertical space between equations we can use \\[2ex] instead of \\. The system

$$\begin{cases} a_1x+b_1y+c_1z=\frac{p_1}{q_1} \\[2ex] a_2x+b_2y+c_2z=\frac{p_2}{q_2} \\[2ex] a_3x+b_3y+c_3z=\frac{p_3}{q_3} \end{cases} $$

is generated by the following code

$$\begin{cases} a_1x+b_1y+c_1z=d_1 \\[2ex] a_2x+b_2y+c_2z=d_2 \\[2ex] a_3x+b_3y+c_3z=d_3 \end{cases} $$

in comparison with

$$\begin{cases} a_1x+b_1y+c_1z=\frac{p_1}{q_1} \\ a_2x+b_2y+c_2z=\frac{p_2}{q_2} \\ a_3x+b_3y+c_3z=\frac{p_3}{q_3} \end{cases} $$

whose code is

$$\begin{cases} a_1x+b_1y+c_1z=\frac{p_1}{q_1} \\ a_2x+b_2y+c_2z=\frac{p_2}{q_2} \\ a_3x+b_3y+c_3z=\frac{p_3}{q_3} \end{cases} $$

  • In response to elect's comment. The following code

    $$ \left\{ \begin{array}{l} 0 = c_x-a_{x0}-d_{x0}\dfrac{(c_x-a_{x0})\cdot d_{x0}}{\|d_{x0}\|^2} + c_x-a_{x1}-d_{x1}\dfrac{(c_x-a_{x1})\cdot d_{x1}}{\|d_{x1}\|^2} \\[2ex] 0 = c_y-a_{y0}-d_{y0}\dfrac{(c_y-a_{y0})\cdot d_{y0}}{\|d_{y0}\|^2} + c_y-a_{y1}-d_{y1}\dfrac{(c_y-a_{y1})\cdot d_{y1}}{\|d_{y1}\|^2} \end{array} \right. $$

produces

$$ \left\{ \begin{array}{l} 0 = c_x-a_{x0}-d_{x0}\dfrac{(c_x-a_{x0})\cdot d_{x0}}{\|d_{x0}\|^2} + c_x-a_{x1}-d_{x1}\dfrac{(c_x-a_{x1})\cdot d_{x1}}{\|d_{x1}\|^2} \\[2ex] 0 = c_y-a_{y0}-d_{y0}\dfrac{(c_y-a_{y0})\cdot d_{y0}}{\|d_{y0}\|^2} + c_y-a_{y1}-d_{y1}\dfrac{(c_y-a_{y1})\cdot d_{y1}}{\|d_{y1}\|^2} \end{array} \right. $$

$\endgroup$
3
  • 5
    $\begingroup$ Is it possible to rotate text? To have a vertical word written in front of the large curly bracket that spans over all the equations? $\endgroup$
    – Steeven
    Jul 3 '17 at 14:21
  • 4
    $\begingroup$ @Steeven Go here $\longrightarrow$ math.meta.stackexchange.com/questions/27798/… $\endgroup$
    – Mr Pie
    Feb 1 '18 at 4:32
  • 1
    $\begingroup$ Thank you, @user477343. This would be a useful feature on this list. $\endgroup$
    – Steeven
    Feb 1 '18 at 14:38
116
$\begingroup$

Colors

Named colors are browser-dependent; if a browser doesn't know a particular color name, it may render the text as black. The following colors are standard in HTML4 and CSS2 and should be interpreted the same by most browsers: $$\begin{array}{|rc|} \hline \verb+\color{black}{text}+ & \color{black}{text} \\ \verb+\color{gray}{text}+ & \color{gray}{text} \\ \verb+\color{silver}{text}+ & \color{silver}{text} \\ \verb+\color{white}{text}+ & \color{white}{text} \\ \hline \verb+\color{maroon}{text}+ & \color{maroon}{text} \\ \verb+\color{red}{text}+ & \color{red}{text} \\ \verb+\color{yellow}{text}+ & \color{yellow}{text} \\ \verb+\color{lime}{text}+ & \color{lime}{text} \\ \verb+\color{olive}{text}+ & \color{olive}{text} \\ \verb+\color{green}{text}+ & \color{green}{text} \\ \verb+\color{teal}{text}+ & \color{teal}{text} \\ \verb+\color{aqua}{text}+ & \color{aqua}{text} \\ \verb+\color{blue}{text}+ & \color{blue}{text} \\ \verb+\color{navy}{text}+ & \color{navy}{text} \\ \verb+\color{purple}{text}+ & \color{purple}{text} \\ \verb+\color{fuchsia}{text}+ & \color{magenta}{text} \\ \hline \end{array}$$

HTML5 and CSS 3 define an additional 124 color names that will be supported on many browsers.

Math Stack Exchange's default style uses a light-colored page background, so avoid using light colors for text. Stick to darker colors like maroon, green, blue, and purple, and remember also that 7–10% of men are color-blind and have difficulty distinguishing red and green.

The color may also have the form #rgb where $r, g, b$ are in the range or 09, af and represent the intensity of red, green, and blue on a scale of $0–15$, with a=10, b=11, … f=15. For example:

$$\begin{array}{|rrrrrrrr|}\hline \verb+#000+ & \color{#000}{text} & & & \verb+#00F+ & \color{#00F}{text} & & \\ & & \verb+#0F0+ & \color{#0F0}{text} & & & \verb+#0FF+ & \color{#0FF}{text}\\ \verb+#F00+ & \color{#F00}{text} & & & \verb+#F0F+ & \color{#F0F}{text} & & \\ & & \verb+#FF0+ & \color{#FF0}{text} & & & \verb+#FFF+ & \color{#FFF}{text}\\ \hline \end{array} $$

$$\begin{array}{|rrrrrrrr|} \hline \verb+#000+ & \color{#000}{text} & \verb+#005+ & \color{#005}{text} & \verb+#00A+ & \color{#00A}{text} & \verb+#00F+ & \color{#00F}{text} \\ \verb+#500+ & \color{#500}{text} & \verb+#505+ & \color{#505}{text} & \verb+#50A+ & \color{#50A}{text} & \verb+#50F+ & \color{#50F}{text} \\ \verb+#A00+ & \color{#A00}{text} & \verb+#A05+ & \color{#A05}{text} & \verb+#A0A+ & \color{#A0A}{text} & \verb+#A0F+ & \color{#A0F}{text} \\ \verb+#F00+ & \color{#F00}{text} & \verb+#F05+ & \color{#F05}{text} & \verb+#F0A+ & \color{#F0A}{text} & \verb+#F0F+ & \color{#F0F}{text} \\ \hline \verb+#080+ & \color{#080}{text} & \verb+#085+ & \color{#085}{text} & \verb+#08A+ & \color{#08A}{text} & \verb+#08F+ & \color{#08F}{text} \\ \verb+#580+ & \color{#580}{text} & \verb+#585+ & \color{#585}{text} & \verb+#58A+ & \color{#58A}{text} & \verb+#58F+ & \color{#58F}{text} \\ \verb+#A80+ & \color{#A80}{text} & \verb+#A85+ & \color{#A85}{text} & \verb+#A8A+ & \color{#A8A}{text} & \verb+#A8F+ & \color{#A8F}{text} \\ \verb+#F80+ & \color{#F80}{text} & \verb+#F85+ & \color{#F85}{text} & \verb+#F8A+ & \color{#F8A}{text} & \verb+#F8F+ & \color{#F8F}{text} \\ \hline \verb+#0F0+ & \color{#0F0}{text} & \verb+#0F5+ & \color{#0F5}{text} & \verb+#0FA+ & \color{#0FA}{text} & \verb+#0FF+ & \color{#0FF}{text} \\ \verb+#5F0+ & \color{#5F0}{text} & \verb+#5F5+ & \color{#5F5}{text} & \verb+#5FA+ & \color{#5FA}{text} & \verb+#5FF+ & \color{#5FF}{text} \\ \verb+#AF0+ & \color{#AF0}{text} & \verb+#AF5+ & \color{#AF5}{text} & \verb+#AFA+ & \color{#AFA}{text} & \verb+#AFF+ & \color{#AFF}{text} \\ \verb+#FF0+ & \color{#FF0}{text} & \verb+#FF5+ & \color{#FF5}{text} & \verb+#FFA+ & \color{#FFA}{text} & \verb+#FFF+ & \color{#FFF}{text} \\ \hline \end{array}$$

You can have a look here for quick reference on colors in HTML.

$\endgroup$
2
  • 12
    $\begingroup$ We should add that colors can be used on items other than text, such as variables and operators. The '\color' command applies to the next item: surround anything longer with braces. $\endgroup$ Feb 21 '15 at 20:30
  • 1
    $\begingroup$ One can in fact use any CSS-compatible colour specification here, including rgb, rgba, hsl, and hsla colours. (I'd edit the answer, but have no time now. Maybe later, if nobody else beats me to it.) $\endgroup$ Oct 11 '18 at 14:31
115
$\begingroup$

Additional decorations

$\def\demo#1#2{#1{#2}\ #1{#2#2}\ #1{#2#2#2}}$

\overline: $\demo\overline A$

\underline: $\demo\underline B$

\widetilde: $\demo\widetilde C$

\widehat: $\demo\widehat D$

\fbox: $\demo\fbox {$E$}$

\underleftarrow: $\demo\underleftarrow{F}\qquad$ variant: \xleftarrow{}: $\xleftarrow{abc}$

\underrightarrow: $\demo\underrightarrow{G}\qquad$ variant: \xrightarrow{}: $\xrightarrow{abc}$

\underleftrightarrow: $\demo\underleftrightarrow{H}$

\overrightarrow $\demo\overrightarrow{AB}$

\overbrace: $\overbrace{(n - 2) + \overbrace{(n - 1) + n + (n + 1)} + (n + 2)}$

\underbrace: $(n \underbrace{- 2) + (n \underbrace{- 1) + n + (n +} 1) + (n +} 2)$

\overbrace and \underbrace accept a superscript or a subscript, respectively, to annotate the brace. For example, \underbrace{a\cdot a\cdots a}_{b\text{ times}} is $$\underbrace{a\cdot a\cdots a}_{b\text{ times}}$$

Note: \varliminf: $\varliminf$ and \varlimsup:$\varlimsup$ have special symbol of their own.

Single character accents

\check: $\check{I}$

\acute: $\acute{J}$

\grave: $\grave{K}$

\vec: $\vec u\ \vec{AB}$ (c.f. \overrightarrow above)

\bar: $\bar z$

\hat: $\hat x$

\tilde: $\tilde x$

\dot \ddot \dddot: $\dot x,\ddot x,\dddot x$

\mathring: $\mathring A$

General stacking

If you cannot find your symbol remember that you can stack various symbols using

\overset{above}{level}: $\overset{@}{ABC}\ \overset{x^2}{\longmapsto}\ \overset{\bullet\circ\circ\bullet}{T}$

\underset{below}{level}: $\underset{@}{ABC}\ \underset{x^2}{\longmapsto}\ \underset{\bullet\circ\circ\bullet}{T}$

You can use these together too. You can type $X \overset{a}{\underset{b}{\to}} Y$ with X\overset{a}{\underset{b}{\to}}Y.

Arc over points

\overset{ \huge\frown}{PQ}: $\overset{ \huge\frown}{PQ}$ denotes the arc over points $P$ and $Q$ (As per comment of @Calvin Khor to @Paul Sinclair's question)

$\endgroup$
7
  • $\begingroup$ Instead of using $\fbox$, you could also use $\boxed{...}$ $\endgroup$
    – Mr Pie
    Oct 19 '17 at 21:23
  • 2
    $\begingroup$ added arrows with text variants, some new single char accents and general stacking section. $\endgroup$
    – zwim
    Oct 27 '17 at 1:42
  • 3
    $\begingroup$ stackrel also seems to work well, as in \stackrel{\text{def}}{=} $\longrightarrow\stackrel{\text{def}}{=}$ $\endgroup$
    – Reb.Cabin
    Feb 6 '18 at 16:48
  • $\begingroup$ Is there a way to do arcs over points, such as to indicate the arc of a curve between two points $P$ and $Q$? \widearc {PQ} doesn't seem to work. $\endgroup$ Jul 29 '19 at 21:41
  • $\begingroup$ $\frown{PQ}$ kind of. $\endgroup$
    – user645636
    Sep 13 '19 at 21:36
  • 1
    $\begingroup$ The best I've been able to come up with is \overset{\frown}{PQ} : $\overset{\frown}{PQ}$. But since \frown doesn't adjust in size, it doesn't look right. Does anyone know how get a properly sized arc? $\endgroup$ Sep 20 '19 at 23:47
  • 2
    $\begingroup$ @PaulSinclair I offer the following \overset{\frown}{AB}\overset{ \large\frown}{CD}\overset{\Large\frown}{EF}\overset{ \huge\frown}{GH}\overset{\Huge\frown}{ABC} $\overset{\frown}{AB}\overset{ \large\frown}{CD}\overset{\Large\frown}{EF}\overset{ \huge\frown}{GH}\overset{\Huge\frown}{ABC}$ $\endgroup$ Sep 22 '19 at 7:11
111
$\begingroup$

Commutative diagrams

AMScd diagrams must start with a "require":

$\require{AMScd}$
\begin{CD}
A @>a>> B\\
@V b V V= @VV c V\\
C @>>d> D
\end{CD}

to get this diagram: $\require{AMScd}$ \begin{CD} A @>a>> B\\ @V b V V= @VV c V\\ C @>>d> D \end{CD}

@>>> is used for arrow right

@<<< is used for arrow left

@VVV is used for arrow down

@AAA is used for arrow up

@= is used for horizontal double line

@| is used for vertical double line

@. is used for no arrow

Another example:

    \begin{CD}
    A @>>> B @>{\text{very long label}}>> C \\
    @. @AAA @| \\
    D @= E @<<< F
\end{CD}

\begin{CD} A @>>> B @>{\text{very long label}}>> C \\ @. @AAA @| \\ D @= E @<<< F \end{CD}

Long labels increase the length of the arrow and in this version also automatically increase corresponding arrows.

$\require{AMScd}$
\begin{CD}
  RCOHR'SO_3Na @>{\text{Hydrolysis,$\Delta, Dil.HCl$}}>> (RCOR')+NaCl+SO_2+ H_2O 
\end{CD}

$\require{AMScd}$ \begin{CD} \text{RCOHR'SO$_3$Na} @>{\text{Hydrolysis, $\Delta,$ Dil. HCl}}>> \text{(RCOR')+NaCl+SO$_2$+ H$_2$O} \end{CD}

$\endgroup$
11
  • $\begingroup$ $\require{AMScd}$\begin{CD} RCOHR'SO_3Na @>{\text{Hydrolysis,\Delta, Dil.HCl}>> (RCOR')+NaCl+SO_2+ H_2O \end{CD} Why does this code not give the correct output? $\endgroup$
    – Quark
    Feb 4 '16 at 10:04
  • $\begingroup$ @Quark: The main error was a missing bracket after HCl. $\endgroup$
    – Lehs
    Feb 4 '16 at 11:38
  • $\begingroup$ @Lehs Thanks. That was a silly mistake :| What if I wanted to write something below the arrow? Also, could you suggest some online website to learn MathJax? $\endgroup$
    – Quark
    Feb 4 '16 at 11:58
  • $\begingroup$ @Quark: then you move a > sign: @>>{\text{very long label}}> I learn MathJax from the examples i.e. in this tread. $\endgroup$
    – Lehs
    Feb 4 '16 at 15:06
  • $\begingroup$ @Lehs Why did you rollback my edit...? You removed the formatting from the list, broke again (in Chrome) the example diagram, and reinserted your duplicate example. Why? $\endgroup$ Feb 4 '16 at 15:25
  • $\begingroup$ @NajibIdrissi: because your edit appeared as a mess in IE. The diagram wasn't even written out. Maybe there is something wrong in your or in my web-program. Now it looks good in IE. $\endgroup$
    – Lehs
    Feb 4 '16 at 15:44
  • $\begingroup$ @NajibIdrissi: Now it also looks good in Chrome for Windows and for Android, plus Safari for Androids. I don't know what the problem is with the current version. $\endgroup$
    – Lehs
    Feb 4 '16 at 15:56
  • 3
    $\begingroup$ I realize this thread is quite old, but what about diagonal arrows? $\endgroup$ Mar 23 '17 at 5:01
  • 2
    $\begingroup$ @AlfredYerger: there are no such possibilities in AMScd. $\endgroup$
    – Lehs
    Sep 28 '17 at 3:57
  • 2
    $\begingroup$ @AlfredYerger Maybe presheaf can help there? See also answer and suggestions about this here: How to draw a commutative diagram? $\endgroup$ Nov 6 '17 at 11:44
  • $\begingroup$ how to draw a double arrow pointing both directions to the left as well as to the right between two points in commutative diagram? $\endgroup$
    – Uncool
    Mar 17 at 13:43
96
$\begingroup$

Continued fractions

To make a continued fraction, use \cfrac, which works just like \frac but typesets the results differently:

$$ x = a_0 + \cfrac{1^2}{a_1 + \cfrac{2^2}{a_2 + \cfrac{3^2}{a_3 + \cfrac{4^4}{a_4 + \cdots}}}}$$

Don't use regular \frac or \over, or it will look awful:

$$ x = a_0 + \frac{1^2}{a_1 + \frac{2^2}{a_2 + \frac{3^2}{a_3 + \frac{4^4}{a_4 + \cdots}}}}$$

You can of course use \frac for the compact notation:

$$ x = a_0 + \frac{1^2}{a_1+} \frac{2^2}{a_2+} \frac{3^2}{a_3 +} \frac{4^4}{a_4 +} \cdots$$

Continued fractions are too big to put inline. Display them with $$$$ or use a notation like $[a_0; a_1, a_2, a_3, \ldots]$.

$\endgroup$
8
  • 3
    $\begingroup$ The RHS of the following continued fraction $$ \cfrac{a_{1}}{b_{1}+\cfrac{a_{2}}{b_{2}+\cfrac{a_{3}}{b_{3}+\ddots }}}= {\genfrac{}{}{}{}{a_1}{b_1}} {\genfrac{}{}{0pt}{}{}{+}} {\genfrac{}{}{}{}{a_2}{b_2}} {\genfrac{}{}{0pt}{}{}{+}} {\genfrac{}{}{}{}{a_3}{b_3}} {\genfrac{}{}{0pt}{}{}{+\dots}} $$ can be typeset with the \genfrac command '{\genfrac{}{}{}{}{a_1}{b_1}} {\genfrac{}{}{0pt}{}{}{+}} {\genfrac{}{}{}{}{a_2}{b_2}} {\genfrac{}{}{0pt}{}{}{+}} {\genfrac{}{}{}{}{a_3}{b_3}} {\genfrac{}{}{0pt}{}{}{+\dots}}' $\endgroup$ Sep 17 '12 at 20:39
  • $\begingroup$ I wonder if something like $\frac12{\vphantom{1}\atop+}\frac34$ would be good enough? It is much simpler. (\frac12{\vphantom{1}\atop+}\frac34) $\endgroup$
    – MJD
    Sep 17 '12 at 22:30
  • 1
    $\begingroup$ Yes, it is. I didn't mention it because in User’s Guide for the amsmath Package it is written the following: "Note. For technical reasons, using the primitive fraction commands \over, \atop, \above in a LATEX document is not recommended (see, e.g., amsmath.faq)." $\endgroup$ Sep 17 '12 at 22:44
  • 4
    $\begingroup$ Happily, we are not writing $\LaTeX$ documents here. $\endgroup$
    – MJD
    Sep 17 '12 at 22:44
  • 11
    $\begingroup$ Or write \underset{j=1}{\overset{\infty}{\LARGE\mathrm K}}\frac{a_j}{b_j}=\cfrac{a_1}{b_1+\cfrac{a_2}{b_2+\cfrac{a_3}{b_3+\ddots}}} to get $$\underset{j=1}{\overset{\infty}{\LARGE\mathrm K}}\frac{a_j}{b_j}=\cfrac{a_1}{b_1+\cfrac{a_2}{b_2+\cfrac{a_3}{b_3+\ddots}}}.$$ $\endgroup$ Jan 24 '13 at 9:15
  • 8
    $\begingroup$ @AméricoTavares Or use \mathop instead of \overset and \underset: \mathop{\LARGE\mathrm K}_{i=1}^\infty \frac{a_i}{b_i} $$\mathop{\LARGE\mathrm K}_{i=1}^\infty \frac{a_i}{b_i}$$ $\endgroup$
    – AlexR
    Feb 21 '15 at 20:48
  • $\begingroup$ @AlexR It's easier, thanks! $\endgroup$ May 17 '15 at 13:24
  • $\begingroup$ @AméricoTavares, Why don't you edit the answer and put this extremely helpful command into there, I think that would be more helpful. $\endgroup$
    – user249332
    Jan 24 '16 at 15:44
91
$\begingroup$

Using \newcommand

I would like to remark that it is possible to define LaTeX commands as you do in your TeX files. I felt so happy when I first discovered it! It's enough to insert something like

$ \newcommand{\SES}[3]{ 0 \to #1 \to #2 \to #3 \to 0 } $

$ \newcommand{\SES}[3]{ 0 \to #1 \to #2 \to #3 \to 0 }$ at the top of your post (remember the dollars!). Then you can just use your commands as you are used to do: in my example typing $$ \SES{A}{B}{C} $$ will produce the following:

$$ \SES{A}{B}{C} $$

It's also possible to use plain \def:

\def\ses#1#2#3{0 \to #1 \to #2 \to #3 \to 0}

and then $\ses{A}{B}{C}$ will produce the same output.

$\endgroup$
3
  • 10
    $\begingroup$ Be aware that this affects the entire post, possibly even the frontpage, so it should be used with great care. $\endgroup$
    – AlexR
    Feb 21 '15 at 20:55
  • $\begingroup$ $\SES{1}{2}{3}$ $\endgroup$
    – ericw31415
    May 7 '18 at 22:03
  • 4
    $\begingroup$ @AlexR It's been fixed since. $\endgroup$
    – iBug
    Apr 10 '19 at 3:40
91
$\begingroup$

Tags & References

For longer calculations (or referring to other post's results) it is convenient to use the tagging/labelling/referencing system. To tag an equation use \tag{yourtag}, and if you want to refer to that tag later on, add \label{somelabel} right after the \tag. It is not necessary that yourtag and somelabel are the same, but it usually is more convenient to do so:

$$ a := x^2-y^3 \tag{*}\label{*} $$

$$ a := x^2-y^3 \tag{*}\label{*} $$

In order to refer to an equation, just use \eqref{somelabel}

$$ a+y^3 \stackrel{\eqref{*}}= x^2 $$

$$ a+y^3 \stackrel{\eqref{*}}= x^2 $$

or \ref{somelabel}

Equations are usually referred to as $\eqref{*}$, but you can also use $\ref{*}$.

Equations are usually referred to as $\eqref{*}$, but you can also use $\ref{*}$.

As you can see, references are even turned into hyperlinks, which you can use externally as well, e.g. like this. Note that you can also reference labels in other posts as long as they appear on the same site, which is especially useful when referring to a question with multiple equations, or when commenting on a post.


Due to a bug blocks containing a \label will break in preview, as a workaround you can put $\def\label#1{}$ in your post while editing and remove that on submission - unfortunately this means you won't spot misspelled references before submitting... Just don't forget to remove that \def again

$\endgroup$
8
  • 10
    $\begingroup$ Also works in comments: \eqref{*} yields a clickable $\eqref{*}$ $\endgroup$ Oct 31 '13 at 10:22
  • $\begingroup$ To enable automatically tagging your queations with incremental numbers, add <script type="text/x-mathjax-config"> MathJax.Hub.Config({TeX: { equationNumbers: {autoNumber: "all"} }}); </script> to your header. $\endgroup$ Jan 20 '16 at 20:56
  • $\begingroup$ @GeraldSenarclensdeGrancy That would however yield a global numbering on all answers to one question, not per-answer. And it would break the current expectation of by default not having tags despite using unstarred \begin{align} etc.... Though personally I'd agree with this $\endgroup$ Jan 21 '16 at 7:19
  • 5
    $\begingroup$ I'm just curious, is there a way to have the tags on the left side of the equation? Something like$$(1)\qquad\qquad\sum\limits_{j}k\tag*{}$$But the $(1)$ tag is all the way to the left. $\endgroup$
    – Crescendo
    Aug 26 '17 at 16:46
  • $\begingroup$ How do we write a tag without brackets (because I want to tag a little square as a box of accomplishment)? $\endgroup$
    – Mr Pie
    Dec 12 '17 at 1:20
  • $\begingroup$ @user477343 No idea, you could ask at tex.stackexchange.com or see if the manual of amsmath has something... $\endgroup$ Dec 12 '17 at 9:30
  • $\begingroup$ @Crescendo You could ask on tex.stackexchange.com for a solution. A workaround could be \begin{array}{lc} or similar. $\endgroup$ Dec 12 '17 at 9:31
  • 1
    $\begingroup$ Hey, I figured how to tag without brackets. You simply put what is inside the braces: $\{$\tag*{$\ldots$}$\}$ which I learnt from here $\longrightarrow$ math.meta.stackexchange.com/questions/27731/… $\endgroup$
    – Mr Pie
    Jan 28 '18 at 0:42
85
$\begingroup$

\implies ($\implies$) is a marginally preferable alternative to \Rightarrow ($\Rightarrow$) for implication.

There's also \iff $\iff$ and \impliedby $\impliedby$.

\to ($\to$) is preferable to \rightarrow or \longrightarrow for things like $f\colon A \to B$. The reverse is \gets ($\gets$).

$\endgroup$
8
  • 6
    $\begingroup$ Why is it preferable? $\endgroup$
    – MJD
    Jul 9 '13 at 20:00
  • 17
    $\begingroup$ implies looks nicer as the arrow is longer and \to is quicker to right (and it's also what you say in your head while typing it). at least that's what I think. $\endgroup$ Jul 29 '13 at 13:21
  • 2
    $\begingroup$ Remember the difference between \to and \mapsto as in $T:\mathbb R\to \mathbb R,\; x\mapsto x+1$ produced by T:\mathbb R\to \mathbb R,\; x\mapsto x+1 $\endgroup$
    – yo'
    Aug 25 '14 at 9:57
  • 8
    $\begingroup$ I prefer using \to when it appears as part of a larger propositional formula, rather than at the top level, i.e. $p\land((q\lor r)\to s)$, because the spacing is similar to that of other binary operators. \implies is better for sentence- or clause-level implications, or in displays, i.e. $$x+2=4-x\implies x=1.$$ $\endgroup$ Feb 2 '15 at 14:22
  • $\begingroup$ I have always used \Longleftarrow for \impliedby. It generates the same thing anyway, for which the former generates $\Longleftarrow$ and the latter generates $\impliedby$ with \Leftarrow $\Leftarrow$ as an alternate for reverse implication. $\endgroup$
    – Mr Pie
    Jan 16 '18 at 6:47
  • $\begingroup$ @yo' instead of \mathbb you could also use \Bbb as a matter of fact :) $\endgroup$
    – Mr Pie
    Jan 16 '18 at 6:53
  • $\begingroup$ Is there a way to add some text above impllies? Such as "by (1)" to refer to another equation that is used for substitution and similar cases. $\endgroup$
    – Alexandros
    Dec 27 '19 at 22:02
  • 1
    $\begingroup$ @Alexandros yes \overset{3.1415}{\underset{26535}{\implies}} produces $$\overset{3.1415}{\underset{26535}{\implies}}$$ $\endgroup$
    – user645636
    Feb 8 '20 at 12:12
76
$\begingroup$

Big braces

Use \left and \right to make braces - (round), [square] and {curly} - scale up to be the size of their arguments. Thus

$$
f\left(
   \left[ 
     \frac{
       1+\left\{x,y\right\}
     }{
       \left(
          \frac{x}{y}+\frac{y}{x}
       \right)
       \left(u+1\right)
     }+a
   \right]^{3/2}
\right)
$$

renders as $$ f\left(\left[ \frac{1+\left\{x,y\right\}}{\left(\frac{x}{y}+\frac{y}{x}\right)\left(u+1\right)}+a\right]^{3/2}\right). $$

Note that curly braces need to be escaped as \{ \}.

If you start a big brace with \left and then need to match that to a \right brace that's on a different line, use the forms \right. and \left. to make "shadow" braces. Thus,

$$
\begin{aligned}
a=&\left(1+2+3+  \cdots \right. \\
& \cdots+ \left. \infty-2+\infty-1+\infty\right)
\end{aligned}
$$

renders as $$ \begin{aligned} a=&\left(1+2+3+ \cdots \right. \\ & \cdots+ \left. \infty-2+\infty-1+\infty\right). \end{aligned} $$

There is also a \middle construct which is useful when one has a mid-expression brace which must also scale up:

$$
\left\langle  
  q
\middle\|
  \frac{\frac{x}{y}}{\frac{u}{v}}
\middle| 
   p 
\right\rangle
$$

renders as $$ \left\langle q\middle\|\frac{\frac{x}{y}}{\frac{u}{v}} \middle| p \right\rangle. $$

Note that constructs like \left\langle, \left| and \left\| are also possible.

$\endgroup$
1
  • 4
    $\begingroup$ Note: \Big( ... \Big) produces $\Big(\dots\Big)$ but this bracket size is fixed in all situations unlike \left( ... \right) which varies in size with its contents. \Big can be useful in various situations. $\endgroup$
    – Nick
    Dec 19 '14 at 6:34
72
$\begingroup$

Limits

To make a limit (like $\lim \limits_{x \to 1} \frac{x^2-1}{x-1}$), use this syntax:

First, start off with $\lim. This renders as $\lim$. The backslash is there to prevent things like $lim$, where the letters are slanted.

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.

$\endgroup$
7
  • 26
    $\begingroup$ 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? $\endgroup$
    – MJD
    Feb 26 '14 at 14:10
  • 4
    $\begingroup$ @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. $\endgroup$ Feb 26 '14 at 16:19
  • 2
    $\begingroup$ I meant that the second limit renders to $\lim \limits_{x \to 1}$ $\endgroup$ Feb 26 '14 at 16:28
  • 9
    $\begingroup$ 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. $\endgroup$
    – MJD
    Feb 26 '14 at 16:41
  • 13
    $\begingroup$ 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 $$-$$ $\endgroup$
    – user1729
    Jul 17 '14 at 12:30
  • 7
    $\begingroup$ The moral is: $\TeX$ was written by a jolly clever chap. Let it do what it wants, because it does it for a reason! $\endgroup$
    – user1729
    Jul 17 '14 at 12:35
  • 2
    $\begingroup$ Part 11 of the "question" shows how to write limits in the way they were meant to be written in LaTeX and MathJax. $\endgroup$
    – David K
    Nov 14 '15 at 23:17
62
$\begingroup$

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 $$

$\endgroup$
8
  • 1
    $\begingroup$ We can also use $\verb*{\rm ...}*$. For example, $\verb*{\rm arsinh}*$ yields ${\rm arsinh}$. $\endgroup$ Aug 12 '14 at 0:27
  • 18
    $\begingroup$ @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. $\endgroup$
    – MvG
    Aug 13 '14 at 11:27
  • 5
    $\begingroup$ Thanks. I didn't know there was a difference between them. I always avoided ${\tt operatorname}$ because it was too long. $\endgroup$ Aug 13 '14 at 14:41
  • 2
    $\begingroup$ 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. $\endgroup$
    – MJD
    Aug 16 '14 at 6:28
  • 3
    $\begingroup$ ... I always use "\text{operator }". Hmmm, $\text{arsinh }x$ vs $\operatorname{arsinh}x$. $\endgroup$ Feb 10 '15 at 16:48
  • 4
    $\begingroup$ 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… $\endgroup$
    – MickG
    Aug 15 '15 at 17:28
  • $\begingroup$ What is the code for the last one? $\endgroup$ May 27 at 8:07
  • $\begingroup$ @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. $\endgroup$
    – MvG
    May 27 at 15:07
60
$\begingroup$

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) } $$

To add a border, use

$$ \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) } $$

$\endgroup$
2
  • 2
    $\begingroup$ When using constructs like this, please heed the points raised in this discussion on usage of colour. $\endgroup$
    – Lord_Farin
    May 20 '16 at 15:56
  • 1
    $\begingroup$ This would be a very helpful feature. $\endgroup$ May 19 '17 at 13:36
53
$\begingroup$

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:

Screenshot

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$$
$\endgroup$
6
  • 7
    $\begingroup$ 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).) $\endgroup$ Jun 24 '14 at 8:48
  • $\begingroup$ 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'? $\endgroup$
    – MJD
    Jun 24 '14 at 12:39
  • $\begingroup$ @MJD: What's your browser? I included a screenshot to support my claim. $\endgroup$
    – MvG
    Aug 13 '14 at 11:24
  • $\begingroup$ 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. $\endgroup$
    – MJD
    Aug 13 '14 at 17:02
  • $\begingroup$ Here's a screenshot with FF 31.0 under Linux: a.pomf.se/fhwmjo.png $\endgroup$
    – MJD
    Aug 16 '14 at 6:23
  • 3
    $\begingroup$ 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. $\endgroup$ May 20 '16 at 14:16
51
$\begingroup$

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}
$\endgroup$
4
  • 1
    $\begingroup$ 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? $\endgroup$
    – code_dredd
    Jun 1 '19 at 20:19
  • $\begingroup$ @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. $\endgroup$
    – David K
    Jun 2 '19 at 5:20
  • $\begingroup$ Why would you use \tag, instead of just using ()? $\endgroup$
    – Some Guy
    Feb 1 at 21:13
  • $\begingroup$ @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. $\endgroup$
    – David K
    Feb 2 at 0:59
47
$\begingroup$

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♧ $$

$\endgroup$
9
  • 1
    $\begingroup$ This is very nice! Is there other auto-shapes or stickers? $\endgroup$ May 19 '17 at 13:37
  • 1
    $\begingroup$ Is it also possible to draw the spade and club in outlines and fill the heart and diamond with a colour? $\endgroup$ May 19 '17 at 13:39
  • 2
    $\begingroup$ @AlwaysConfused None that come to mind. Google search turned up this which might help. Otherwise search for a TeX/LaTeX/MathJax symbol table. $\endgroup$
    – David
    May 22 '17 at 23:48
  • 1
    $\begingroup$ @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. $\endgroup$
    – MJD
    May 29 '18 at 16:11
  • 1
    $\begingroup$ @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. $\endgroup$
    – David
    May 30 '18 at 4:31
  • $\begingroup$ Is there a way to force the heart and diamond suit symbols to be filled, like the club and spade? $\endgroup$
    – code_dredd
    Jun 2 '19 at 18:39
  • $\begingroup$ @code_dredd See my previous comment in reply to "Always Confused", also the comment by MJD. $\endgroup$
    – David
    Jun 2 '19 at 22:08
  • $\begingroup$ @David I guess nothing has changed since then... Thanks. $\endgroup$
    – code_dredd
    Jun 2 '19 at 23:36
  • $\begingroup$ 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}♡ $$ $\endgroup$ Mar 28 '20 at 6:02
42
$\begingroup$

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.

$\endgroup$
37
$\begingroup$

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.

$\endgroup$
13
  • 2
    $\begingroup$ If mathjax loads siunitx or gensymb, there is then \degree in latex which is the degree symbol. $\endgroup$
    – dustin
    Feb 17 '15 at 22:29
  • 1
    $\begingroup$ @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. $\endgroup$
    – MvG
    Feb 17 '15 at 23:39
  • 2
    $\begingroup$ 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. $\endgroup$
    – dustin
    Feb 17 '15 at 23:43
  • 15
    $\begingroup$ On my display, ° looks bad and ^\circ looks good: a.pomf.se/xnlfyg.png $\endgroup$
    – MJD
    Mar 24 '15 at 21:10
  • 2
    $\begingroup$ 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. $\endgroup$
    – Joffan
    Apr 19 '17 at 14:04
  • 1
    $\begingroup$ @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. $\endgroup$
    – MvG
    Apr 20 '17 at 22:24
  • $\begingroup$ How to use Radian (c) , gradian (g) and Steradian (sr) ? And also, Angstrom (though a lenght unit)? $\endgroup$ May 21 '17 at 16:06
  • $\begingroup$ 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$$ $\endgroup$
    – user427802
    May 31 '18 at 14:41
  • 1
    $\begingroup$ @AbhasKumarSinha It looks quite slanty to me. $\endgroup$
    – Tom Hale
    Jun 13 '18 at 3:57
  • 3
    $\begingroup$ @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? $\endgroup$
    – MvG
    Oct 8 '18 at 19:09
  • $\begingroup$ 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. $\endgroup$
    – MvG
    Oct 8 '18 at 19:25
  • $\begingroup$ @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. $\endgroup$
    – StephenG
    Oct 10 '18 at 4:16
  • $\begingroup$ 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. $\endgroup$ Oct 10 '18 at 5:25
34
$\begingroup$

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

$\endgroup$
4
  • 10
    $\begingroup$ 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} $\endgroup$ Aug 21 '16 at 14:32
  • 1
    $\begingroup$ I will need this. It is so useful. $\endgroup$ May 21 '17 at 16:09
  • 1
    $\begingroup$ What about long division? $\endgroup$
    – Aqua
    Aug 14 '18 at 8:54
  • 1
    $\begingroup$ @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} $\endgroup$ May 16 '19 at 20:06
28
$\begingroup$

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

$\endgroup$
3
  • 4
    $\begingroup$ Oh!! I was always confused on why some people had \displaystyle. $\endgroup$ Nov 7 '16 at 0:42
  • 1
    $\begingroup$ @SimplyBeautifulArt I was always wondering on why the math expressions of some people looked nicer than mine.. $\endgroup$
    – user486983
    Sep 21 '18 at 21:37
  • $\begingroup$ There is also $\scriptstyle{AbC}$ $\scriptstyle{AbC}$ and $\scriptscriptstyle{AbC}$ $\scriptscriptstyle{AbC}$. $\endgroup$ Mar 5 '20 at 8:52
27
$\begingroup$

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.

$\endgroup$
1
  • $\begingroup$ Alternatively, one can also sneak in a rule of zero width \rule{0pt}{2ex}, as explained here. $\endgroup$
    – on4aa
    Apr 29 '20 at 15:06
24
$\begingroup$

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{equation}\begin{aligned} a &= b + c \\ &= d + e + f + g \\ &= h + i \end{aligned}\end{equation}\tag{2}\label{eq2}$$

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

$$\begin{equation}\begin{aligned}
a &= b + c \\
  &= d + e + f + g \\
  &= h + i
\end{aligned}\end{equation}\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}$$
$\endgroup$
9
  • $\begingroup$ I don’t believe there is any difference between align and aligned, but whatever feels comfortable I suppose. $\endgroup$
    – Mr Pie
    Feb 2 '18 at 6:12
  • $\begingroup$ There is actaully a difference, read here for a detailed discussions. $\endgroup$
    – jdhao
    Feb 2 '18 at 6:28
  • $\begingroup$ thank you very much for clearing up that understanding :) $\endgroup$
    – Mr Pie
    Feb 2 '18 at 6:30
  • $\begingroup$ You are welcome. When in doubt, always google it first :). $\endgroup$
    – jdhao
    Feb 2 '18 at 6:32
  • $\begingroup$ 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. $\endgroup$
    – code_dredd
    Jun 1 '19 at 20:17
  • $\begingroup$ I am not aware of this kind of command. What is your use case? $\endgroup$
    – jdhao
    Jun 4 '19 at 2:27
  • $\begingroup$ the last equation numbering can also be used with align* instead of align $\endgroup$
    – Masacroso
    Dec 2 '19 at 23:59
  • $\begingroup$ I get all tags on the first line: "a = b + c (3)(4)(5)". $\endgroup$
    – Jiri Kriz
    Dec 13 '19 at 13:55
  • $\begingroup$ Why do we need both $$ and \begin{equation} with aligned? I noticed that when I omitted \begin{equation} that I did not get an equation number, but that does not happen when I use only $$ without any other environments inside. $\endgroup$ May 12 at 20:41
16
$\begingroup$

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} $$

$\endgroup$
1
  • 3
    $\begingroup$ It must have taken more than a thousand words to write that picture though :D $\endgroup$
    – Mr Pie
    Jul 20 '18 at 9:25
15
$\begingroup$

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}

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

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

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

$\endgroup$

You must log in to answer this question.

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