Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

LaTeX Primer

How to type mathematical equations using LaTeX

LaTeX is the standard language used by mathematicians, physicists, etc. for typesetting mathematical expressions. PF uses MathJax, a Javascript-based engine, to process LaTeX code in posts and render it in the way you would see it in a textbook.

Note: the PF apps for iOS (iPhone, iPad) and Android can only display raw LaTeX code as plain text. You must use PF via a web browser in order to see properly-rendered equations.

If you're on a computer, you can see the LaTeX code for any equation displayed in the forum by right-clicking (under Windows) or control-clicking (under Mac OS) on it. This brings up a contextual popup menu. Choose Show Math As, then TeX Commands.

To practice your LaTeX, enter it in the editing box for a new thread or post, and click the "Preview" button. Don't click the "Create Thread" or "Post Reply" button unless you want everybody else to see what you're doing!

This document aims to help people produce individual equations for postings on PF. It does not include features of LaTeX for layout of full documents, or precise formatting for print publication. If you have suggestions for additions to this document that fit with its general aims, please post them in our Feedback and Announcements forum.

Delimiting your LaTeX code

You must put special delimiters at the beginning and end of your LaTeX code, in order for MathJax to recognize it and process it. If you want the equation to appear inline with the surrounding text, put ## before and after it. If you want the equation to stand alone in a separate "paragraph", centered horizontally on the screen as in most textbooks, put $$ before and after it.

Here's an example, first with the delimiters disabled so you can see the raw code:

A quadratic equation is of the form ##ax^2 + bx + c = 0##. To solve for x, use the quadratic formula $$x = \frac {-b \pm \sqrt{b^2 -4ac}} {2a},$$ substituting the values of the coefficients a, b and c.

And now with the delimiters enabled so as to render the equations:

A quadratic equation is of the form ##ax^2 + bx + c = 0##. To solve for x, use the quadratic formula $$x = \frac {-b \pm \sqrt{b^2 -4ac}} {2a},$$ substituting the values of the coefficients a, b and c.

Hereafter, I will omit the delimiters in code examples, to save space.

Spaces

Most blank spaces and end-of-line characters in your raw code are ignored. Spaces will usually be placed in the rendered equation according to standard mathematical conventions. For example, a b + c / d produces ##a b + c / d##. I'll try to note exceptions when they arise.

If you need to insert a visible space, the simplest way is usually to use a ~ (tilde). You can also use \text{ }. There are examples of both in the Plain text section below.

I like to use spaces generously in my code, just like with a programming language, to separate sections of code visually and make it easier to read and debug.

Superscripts and subscripts

The symbols ^ and _ are the commands for superscripts (e.g. exponents) and subscripts, respectively. If a superscript or subscript has more than one character, enclose them in braces { }. This is a general rule for LaTeX commands, by the way: if they are to act on multiple characters, enclose those characters in braces.

You can have both subscripts and superscripts on the same symbol. Normally they appear one on top of the other. If you want them offset as in tensor notation, attach some of them to a pair of empty braces, as in the example for R below.

If a subscript or superscript is a label that should be displayed in upright Roman text, not italic, enclose it in \rm{ }.

##x^2, y_k, e^{ikx}## x^2, y_k, e^{ikx}
##x^2_k, R^{ab}{}_{cd}## x^2_k, R^{ab}{}_{cd}
##x_{old}, x_\rm{old}## x_{old}, x_\rm{old}

Roots and fractions

You can combine operations by using braces to nest them, one inside another. See the last example below.

$$\sqrt x, \sqrt[3] x, \sqrt {b^2 - 4ac}$$ \sqrt x, \sqrt[3] x, \sqrt {b^2 - 4ac}
$$\frac a b, \frac {ab} {c + d}$$ \frac a b, \frac {ab} {c + d}
$$\frac 1 {\sqrt {1 - \frac {v^2} {c^2}}}$$ \frac 1 {\sqrt {1 - \frac {v^2} {c^2}}}

Plain text

By default, most "math text" is in italics. To get plain Roman text, you can use \text{ } or \rm{ }, depending on whether you want spaces or math operations to be rendered or not.

##\text{some text \frac 1 2}## \text{some text \frac 1 2}
##\rm{some text \frac 1 2}## \rm{some text \frac 1 2}
##g = 9.81~\rm{m/s^2}## g = 9.81~\rm{m/s^2}

The last example above uses a ~ (tilde) to put a blank space between the number and the units. \text{ } doesn't work here because the exponent wouldn't display properly.

Parentheses, brackets, etc.

For single-line expressions you can use parentheses and brackets normally. However, braces are used by LaTeX itself for grouping things, as you've seen above, so if you want them to actually appear, you have to escape them by using backslashes. In this case the braces lose their normal grouping function, so you have to use a second, unescaped pair if you want them to be grouped.

For angle brackets, \langle \rangle looks better than < >.

$$\{ a + b \}$$ \{ a + b \}
$$\frac 1 { \{ a + b \} }$$ \frac 1 { \{ a + b \} }
$$\langle x \rangle$$ \langle x \rangle

For "tall" expressions e.g. fractions, sums or integrals, you can make parentheses etc. adjust their size automatically to fit their contents.

$$\left( \frac a b \right)$$ \left( \frac a b \right)
$$\left[ \frac a b \right]$$ \left[ \frac a b \right]
$$\left\{ \frac a b \right\}$$ \left\{ \frac a b \right\}
$$\left| \frac a b \right|$$ \left| \frac a b \right|
$$\left\| \frac a b \right\|$$ \left\| \frac a b \right\|
$$\left< \frac a b \right>$$ \left< \frac a b \right>
$$\left[ 1 - \left( \frac v c \right)^2 \right]^{-1/2}$$ \left[ 1 - \left( \frac v c \right)^2 \right]^{-1/2}

You can omit half of a pair of these "stretchy" delimiters by replacing it with \left. or \right. as appropriate. See the Integrals section below for an example.

Vectors (physics)

Different people and textbooks use different notations for vectors. I hope yours is included in the following list. Also included are some common vector math operators. Of course, you can use any of the vector notations with these operators. I showed only one for brevity.

Vector ##\mathbf A, \vec A, \tilde A## \mathbf A, \vec A, \tilde A
Unit vector ##\hat {\mathbf A}, \hat A## \hat {\mathbf A}, \hat A
Scalar (dot) product ##\vec A \cdot \vec B## \vec A \cdot \vec B
Vector (cross) product ##\vec A \times \vec B## \vec A \times \vec B
Magnitude ##| \vec A |## | \vec A |

Derivatives

$$y', y'', y^{(n)}, \dot x, \ddot x$$ y', y'', y^{(n)}, \dot x, \ddot x
$$\frac {df} {dx}, \frac {\partial f} {\partial x}$$ \frac {df} {dx}, \frac {\partial f} {\partial x}
$$\frac {\partial^2 \psi} {\partial x^2}$$ \frac {\partial^2 \psi} {\partial x^2}
$$\nabla f, \nabla^2 \psi, \Box^2 \phi$$ \nabla f, \nabla^2 \psi, \Box^2 \phi

Integrals

With integrals, use \, to insert a bit of space (not as much as a full space) before the ##dx## (or whatever the variable of integration is) at the end.

In the second example, note how the vertical bar representing the evaluation of limits is specified. It's half of a left/right pair of "stretchy" absolute-value bars. The left half is hidden by specifying it as \left. instead of \left|.

$$\int x^2e^x \, dx$$ \int x^2e^x \, dx
$$\int_a^b x^2 \, dx = \left. \frac 1 3 x^3 \right|_a^b$$ \int_a^b x^2 \, dx = \left. \frac 1 3 x^3 \right|_a^b
$$\oint \vec E \cdot d \vec A$$ \oint \vec E \cdot d \vec A
$$\int_{y=c}^d \int_{x=a}^b e^{x+y} \, dx \, dy$$ \int_{y=c}^d \int_{x=a}^b e^{x+y} \, dx \, dy
$$\iint, \iint_S, \iiint, \iiint_V$$ \iint, \iint_S, \iiint, \iiint_V

Sums, products and limits

$$\sum_{n=0}^\infty a_n x^n$$ \sum_{n=0}^\infty a_n x^n
$$\prod_{n=0}^\infty a_n$$ \prod_{n=0}^\infty a_n
$$\lim_{n \rightarrow +\infty} {\frac {\sin(x)} x}$$ \lim_{n \rightarrow +\infty} {\frac {\sin(x)} x}

Greek letters

Greek lowercase ##\alpha\ \beta\ \gamma\ \delta\ \epsilon\ \zeta## \alpha \beta \gamma \delta \epsilon \zeta
##\eta\ \theta\ \iota\ \kappa\ \lambda\ \mu## \eta \theta \iota \kappa \lambda \mu
##\nu\ \omicron\ \xi\ \pi\ \rho\ \sigma\ \tau## \nu \omicron \xi \pi \rho \sigma \tau
##\upsilon\ \phi\ \chi\ \psi\ \omega## \upsilon \phi \chi \psi \omega
Greek lowercase
(variant forms)
##\varepsilon\ \vartheta\ \varpi\ \varrho\ \varsigma\ \varphi## \varepsilon \vartheta \varpi \varrho \varsigma \varphi
Greek uppercase ##\Gamma\ \Delta\ \Theta\ \Lambda\ \Xi\ \Pi## \Gamma \Delta \Theta \Lambda \Xi \Pi
##\Sigma\ \Upsilon\ \Phi\ \Psi\ \Omega## \Sigma \Upsilon \Phi \Psi \Omega

Other special symbols

Planck's constant ##\hbar## \hbar
Infinity ##\infty## \infty
Plus or minus ##\pm\ \mp## \pm \mp
Parallel, perpendicular ##\parallel\ \perp## \parallel \perp
For all ##\forall## \forall
There exists ##\exists## \exists
Ellipsis ##\dots\ \cdots\ \vdots\ \ddots## \dots \cdots \vdots \ddots
Arrows ##\rightarrow\ \leftarrow\ \leftrightarrow## \rightarrow \leftarrow \leftrightarrow
##\Rightarrow\ \Leftarrow\ \Leftrightarrow## \Rightarrow \Leftarrow \Leftrightarrow
##\mapsto\ \to## \mapsto \to
##\uparrow\ \downarrow## \uparrow \downarrow
Relations ##\gt\ \geq\ \lt\ \leq\ \neq## \gt \geq \lt \leq \neq
##\gg\ \ll\ \equiv## \gg \ll \equiv
##\approx\ \cong\ \sim## \approx \cong \sim
Absolute value, norm ##|x|, \|x\|## |x|, \|x\|
Complex conjugate, adjoint ##z^*, \bar z, z^\dagger## z^*, \bar z, z^\dagger
Dirac bracket notation ##\langle \phi | \psi \rangle## \langle \phi | \psi \rangle
Direct sum ##V \oplus W## V \oplus W
Tensor product ##S \otimes T## S \otimes T
Function composition ##f \circ g## f \circ g
Element of, not in ##x \in A, x \not\in A## x \in A, x \not\in A
Subset ##A \subset B, B \subseteq C## A \subset B, B \subseteq C
Set union, intersection ##A \cup B, A \cap B## A \cup B, A \cap B
Multiple set union, intersection $$\bigcup_i A_i, \bigcap_i A_i$$ \bigcup_i A_i, \bigcap_i A_i
Set subtraction ##A \setminus B## A \setminus B
Binomial coefficient ##\binom n k, {_nC_r}## \binom n k, {_nC_r}

Special functions

Trigonometric ##\sin\ \cos\ \tan\ \sec\ \csc\ \cot## \sin \cos \tan \sec \csc \cot
Inverse trig ##\arcsin\ \arccos\ \arctan## \arcsin \arccos \arctan
##\sin^{-1}\ \cos^{-1}\ \tan^{-1}\\\sec^{-1}\ \csc^{-1}\ \cot^{-1}## \sin^{-1} \cos^{-1} \tan^{-1}
\sec^{-1} \csc^{-1} \cot^{-1}
Logarithmic ##\ln\ \log\ \log_2\ \log_{10}## \ln \log \log_2 \log_{10}
Hyperbolic trig ##\sinh\ \cosh\ \tanh\ \coth## \sinh \cosh \tanh \coth
Inverse hyperbolic trig ##\sinh^{-1}\ \cosh^{-1}\ \tanh^{-1}\ \coth^{-1}## \sinh^{-1} \cosh^{-1} \tanh^{-1} \coth^{-1}
Other functions ##\operatorname {arcsec}## \operatorname {arcsec}

Functions that are not supported directly, e.g. arcsec arccsc arccot sech csch arcsinh arccosh arctanh arcsech arccsch arccoth, can be used via \operatorname as shown in the last example above.

Multiple lines

To split an equation or expression into multiple lines, use \\.

To align them in a specific way (e.g. at = signs), enclose them between \begin{align} and \end{align}, and use & to specify the points at which they are to be aligned. By default, this adds equation numbers at the right side of the page. To suppress them, put \nonumber at the end of each line.

$$y = (a + b)^2 \\ = a^2 + 2ab + b^2$$ y = (a + b)^2 \\ = a^2 + 2ab + b^2
$$\begin{align} y & = (a + b)^2 \\ & = a^2 + 2ab + b^2 \end{align}$$ \begin{align}
y & = (a + b)^2 \\
& = a^2 + 2ab + b^2
\end{align}
$$\begin{align} y & = (a + b)^2 \nonumber \\ & = a^2 + 2ab + b^2 \nonumber \end{align}$$ \begin{align}
y & = (a + b)^2 \nonumber \\
& = a^2 + 2ab + b^2 \nonumber
\end{align}

This sort of construction with \begin{} and \end{} is called an environment. There are other environments for different kinds of formatting.

Matrices and Tables

$$\begin{pmatrix} 1 & 2 & 3 & 4 \\ a & b & c & d \\ x & y & z & w \end{pmatrix}$$ \begin{pmatrix}
1 & 2 & 3 & 4 \\
a & b & c & d \\
x & y & z & w
\end{pmatrix}
$$\begin{vmatrix} 1 & 2 & 3 & 4 \\ a & b & c & d \\ x & y & z & w \end{vmatrix}$$ \begin{vmatrix}
1 & 2 & 3 & 4 \\
a & b & c & d \\
x & y & z & w
\end{vmatrix}
$$\begin{bmatrix} 1 & 2 & 3 & 4 \\ a & b & c & d \\ x & y & z & w \end{bmatrix}$$ \begin{bmatrix}
1 & 2 & 3 & 4 \\
a & b & c & d \\
x & y & z & w
\end{bmatrix}
$$\begin{matrix} 1 & 2 & 3 & 4 \\ a & b & c & d \\ x & y & z & w \end{matrix}$$ \begin{matrix}
1 & 2 & 3 & 4 \\
a & b & c & d \\
x & y & z & w
\end{matrix}
$$\begin{array}{|c|c|c|c|} \hline 1 & 2 & 3 & 4 \\ \hline a & b & c & d \\ \hline x & y & z & w \\ \hline \end{array}$$ \begin{array}{|c|c|c|c|}
\hline 1 & 2 & 3 & 4 \\
\hline a & b & c & d \\
\hline x & y & z & w \\
\hline
\end{array}

Cases

The cases environment lets you specify functions piecewise, with a left brace grouping the cases together.

$$u(x) = \begin{cases} e^x & \text{if } x \geq 0 \\ 1 & \text{if } x < 0 \end{cases}$$ u(x) =
\begin{cases}
e^x & \text{if } x \geq 0 \\
1 & \text{if } x < 0
\end{cases}
$$V(x) = \begin{cases} 0, & x \lt 0 \\ V_0, & 0 \leq x \lt L \\ 0, & x \geq L \end{cases}$$ V(x) =
\begin{cases}
0, & x \lt 0 \\
V_0, & 0 \leq x \lt L \\
0, & x \geq L
\end{cases}

Fonts

Blackboard bold ##\mathbb {ABCDE}## \mathbb {ABCDE}
Calligraphic ##\mathcal {ABCDE}## \mathcal {ABCDE}
Fraktur ##\mathfrak {ABCDE}## \mathfrak {ABCDE}
Sans serif ##\mathsf {ABCDE}## \mathsf {ABCDE}
Roman (upright) ##\mathrm {ABCDE}## \mathrm {ABCDE}
Bold ##\mathbf {ABCDE}## \mathbf {ABCDE}
Typewriter ##\mathtt {ABCDE}## \mathtt {ABCDE}

Bugs, tips and other notes

If your LaTeX code includes something that looks like a BBcode tag for formatting text in a forum post, e.g. [i] for italics, this will likely mess up the rendering of your equation. This happens because the forum software (Xenforo) on PF's server interprets BBcode tags and converts them to HTML tags before sending the page to your browser, where our LaTeX engine (MathJax) runs using Javascript. The simplest way to fix this is to include a space inside the brackets, e.g. [ i].

An alternative way of delimiting your LaTeX code is to enclose it between the following BBcode tags: [itex]...[/itex] for inline equations (instead of ##...##), and [tex]...[/tex] for display-style equations (instead of $$...$$). However, the following delimiters that are often used elsewhere do not work here on PF: $...$, \(...\) and \[...\].

If you have LaTeX code that was prepared in some other software, and uses the $...$ delimiters that don't work here, you can convert them to ##...## in an editor that uses regular expressions. Replace (^|[^\$])\$([^\$]|$) by $1##$2. (Thanks to mfb for this tip!)

If for some reason you want to post raw LaTeX code that includes the delimiters, you can prevent the delimiters from being interpreted as delimiters. Select a delimiter (only), click the Text Color icon (the one that looks like a half moon) in the palette at the top of the editing window, and choose black. Repeat for the other delimiter.

Further information

If you have questions about using LaTeX, you can ask them in our Math Software and LaTeX forum. If you have questions or suggestions about this document, please post them in our Feedback and Announcements forum.

For MathJax documentation, see the MathJax web site. In particular, you can find a complete list of LaTeX commands supported by MathJax here

Acknowledgements

LaTeX was originally implemented on PF thanks to the efforts of former admin chroot. Greg Bernhardt installed the current MathJax processor. The original version of this FAQ, for the vBulletin forum software that PF originally used, was written or contributed to by Fredrik, micromass, Redbelly98, vela, Mark44, LCKurtz and jtbell. It was reformatted for the current Xenforo forum software by jtbell.