Latex: About
Latex, or
(pronounced lah-TEK or lay-TEK), is a typesetting markup language that is useful to produce properly formatted mathematical and scientific expressions.
Using LaTeX in the Community, the AoPSWiki, or the Classroom
If you starting and looking to learn more about what
can do, see this non-technical introduction on
. If you are just learning
for use on the AoPS site, then go to the LaTeX on AoPS page. If you would like to produce full documents of your own LaTeX, please follow the directions below.
Getting Started with Writing Documents in LaTeX
-
-
- The first step is to install the software to use LaTeX on your computer.
- For Windows, follow these instructions (in order):
- For Google Docs, you can get AoPS syntax (math only) through these steps:
- Click +free at the top of the Google Docs add-on store page for Auto-Latex Equations.
- Follow the popup instructions. For a full tutorial, visit the official website of the add-on.
- The first step is to install the software to use LaTeX on your computer.
All of these packages are free. They are not developed or distributed by Art of Problem Solving.
-
(You will also need a PDF viewer such as the free Adobe Reader; however this program is already installed on most computers as well as in Google Chrome.)
- Work through the “Basics” pages:
- The First Document page walks you through creating your first LaTeX document.
- The Math page shows you how to typeset mathematics in LaTeX.
- The Style section shows you how to control the basic appearance of your document.
- The Pictures section shows you how to add images to your LaTeX documents.
- Use our LaTeX quick-reference guides to learn more about:
- Use the TeXer to experiment and practice writing mathematical formulas.
- Use the Help page and the LaTeX forum for more help.
See also
LaTeX:Downloads
| LaTeX |
| About – Getting Started – Diagrams – Symbols – Downloads – Basics – Math – Examples – Pictures – Layout – Commands – Packages – Help |
The following are some downloads that are important to start writing in TeX.
If you are using a Windows machine, you can download and install the following two programs (MiKTeX and TeXnicCenter, in that order) to get started with LaTeX. These programs are free, and are not produced or distributed by AoPS Incorporated.
Note: These are not the only ways to install and use LaTeX on your computer. The TeX Users Group has a list of other LaTeX programs, including programs for Mac and Linux computers.
Contents
For Windows
MiKTeX
MiKTeX is the engine that does the typesetting work.
Note: The MiKTeX download file is large – 190 MB.
To download and install MiKTeX, do the following:
- Click here to open the MiKTeX download site. For Mac users, you will need MacTex which can be found here.
- On the MiKTeX download page, there will be several panels for the different downloads offered. Open the panel with the title Basic MiKTeX Installer, 32-bit by clicking on it, or if your computer is 64-bit, choose the 64-bit option. Download the basic MiKTeX system by clicking the download button there.
- After the MiKTeX system download has finished, find the downloaded file, and double-click on it to launch the installer. Follow the directions in the installer. Some things to watch out for while installing:
- When it asks you for the directory in which to install the files, we recommend leaving the default C:\Program Files\MiKTeX 2.9, but if you choose to change it, make note of where you change it to: you will need this information when installing TeXnicCenter. Note: Some versions of Windows will use C:\Program Files (x86)\MiKTeX 2.9 as the installation folder. Pay attention to what folder MiKTeX installs to!
- It will ask you your “preferred paper size”. North American users will probably want “Letter”; users elsewhere in the world will want “A4”.
- When it asks “Download packages on the fly”, if you choose “Ask me first”, beware that this may cause bugs later. In the future, if you compile a LaTeX file and it reports producing 0 pages or that a “GUI framework cannot be initialized”, know that this is because a package has not been installed. To fix this, either install the missing package yourself or change this setting to “Yes” instead of “Ask me first”.
At this point, MiKTeX should be installed on your computer.
TeXnicCenter
TeXnicCenter is a visual interface and editor for producing LaTeX documents. It is not a What-You-See-Is-What-You-Get (WYSIWYG) editor, meaning your code doesn’t immediately become nice math images as you type. However, it does include an easy-to-use interface for finding symbol commands, and its text editor is custom-designed to help you avoid syntax errors. To install TeXnicCenter:
- Click here to open the TeXnicCenter download site. (Right click on the link and open it in a new window.)
- Click on “Downloads” on the top of the page.
- Download the version that fits the Windows architecture that you have (32- or 64-bit). If you are unsure, download the 32-bit version.
- Once the download is finished, run the installer that you just downloaded. We recommend accepting the default options, except that you may wish to add a desktop shortcut icon when you are asked.
- If you elected to have a shortcut on the desktop, just click the icon on the desktop. Otherwise, click Start on the main Windows window, search for ‘TeXnicCenter’, and run the program.
- When the program starts, a Tips window will open. Click Close. The program will then walk you through the configuration wizard:
- When it asks you for the “full path of the directory where the executables” are located, type C:\Program Files\MiKTeX 2.9\miktex\bin (Note: if you changed the default location when installing MiKTeX, then you’ll need to replace C:\Program Files\MiKTeX 2.9 with the directory to which you installed MiKTeX. Your computer may also have used C:\Program Files (x86)\MiKTeX 2.9 as the installation folder, in which case you’ll instead have to type C:\Program Files (x86)\MiKTeX 2.9\miktex\bin as the directory for the executables.) If you are using the 64-bit version, the path might be C:\Program Files\MiKTeX 2.9\miktex\bin\x64.
- If it asks you to pick a PostScript viewer, you may just leave everything blank and just click “Next”. Similarly if it asks you to pick a DVI viewer, just leave everything blank and click “Next”. (It may or may not ask you these things, depending on how your computer is configured.)
For Mac
Mac TeX
Mac TeX is an all-in-one package which contains the LaTeX distribution, editors, and other useful software.
- To download the latest version, visit the MacTeX website. (Note: file is about 2GB and will take a while to download.)
- Once you’ve downloaded the distribution, install it by double-clicking the .dmg file and follow the instructions.
- Once installed, everything you need can be found in your Applications folder and in a folder called TeX. TeXShop and TeXworks are both open source LaTeX editors, released under the GPL.
- For more information click here.
TeX for GNU/Linux & Unix
TeX Live
TeX Live is an easy way to get up and running with the TeX document production system. It provides a comprehensive TeX system with binaries for GNU/Linux and Unix Operating Systems. It includes all the major TeX-related programs, macro packages, and fonts that are completely free software, including support for many languages around the world.
- Most GNU/Linux distributions like Arch, Debian, Fedora, Ubuntu, etc have TeX Live in the repository, so you can directly just get it through you package manager or software center. Otherwise, download TeX Live from here.
- Open a terminal, change to directory in which you have downloaded the .tar.gz file(for example, cd ~/Downloads if you have downloaded the file in Downloads folder). Then untar by tar -xvzf install-tl-unx.tar.gz. Next cd install-tl-unx. Then, install-tl -gui wizard. Now follow the instructions.
- For more, checkout here.
You can use just any text editor of your use! Some common are gedit, emacs(with ACUTeX is the most powerful Tex editor), vim,etc.
TeXstudio
TeXstudio is an integrated writing environment for creating LaTeX documents. Our goal is to make writing LaTeX as easy and comfortable as possible. Therefore TeXstudio has numerous features like syntax-highlighting, integrated viewer, reference checking and various assistants.
- Click here. Download the proper version for your distribution. Otherwise download the source tarball and follow the associated wiki for installation.
You’re now ready to use LaTeX! Progress onto your first LaTeX document
See Also
aTeX:LaTeX on AoPS
| LaTeX |
| About – Getting Started – Diagrams – Symbols – Downloads – Basics – Math – Examples – Pictures – Layout – Commands – Packages – Help |
This article explains how to use LaTeX in the AoPSWiki, the AoPS Community, and the AoPS Classroom. See Packages to know which packages are prebuilt into the AoPS site.
Contents
Getting Started with LaTeX
The Very Basics
LaTeX uses a special “math mode” to display mathematics. There are two types of this “math mode”:
In-line Math Mode
In in-line math mode, we use $ signs to enclose the math we want to display, and it displays in-line with our text. For example, typing $\sqrt{x} = 5$ gives us ![]()
Display Math Mode
In display math mode, we enclose our code in double dollar signs, and it displays the math centered and on its own line. For example, $$\sqrt{x} = 5$$ gives us ![]()
In-line vs. Display
Besides displaying in-line vs. displaying centered and on a new line, the two modes render differently in other ways. Note that $\sum_{k=1}^n k^2$ gives us
whereas $$\sum_{k=1}^n k^2$$ gives us ![]()
Basic Expressions
Multiplication
Sometimes, when we’re multiplying, we don’t need a multiplication symbol. For instance, we can write
instead of
without ambiguity. However, when you’re multiplying numbers, for instance, a multiplication symbol comes in handy. The standard symbol is given by $\cdot$. For example, $12\cdot\frac{1}{2}$ gives us ![]()
Fractions
We can make fractions via $\frac{...}{...}$. For instance, $\frac{x+y}{2}$ will give us ![]()
Roots
Square roots in
are pretty simple; we just type $\sqrt{...}$. For instance, $\sqrt{2}$ gives us
Cube roots, fourth roots, and so on are only slightly more difficult; we type $\sqrt[n]{...}$. For instance, $\sqrt[4]{x-y}$ gives ![]()
Superscripts & Subscripts
To get superscripts (or exponents), we use the caret symbol ^. Typing $x^2+y^2$ gives
Subscripts are obtained via an underscore (holding shift and the minus sign on most keyboards). For instance, $a_k$ yields ![]()
Groups
Most operations in
(such as superscripts and subscripts) can only see the “group” of characters immediately following it. We use curly braces {...} to indicate groups longer than one character. For instance, if we wrote $x^2015$, we’d expect to get
but we instead get
This is because each character in the string 2015 is in its own group until we tell
that 2015 should be one whole group. To convey this information to
, we write $x^{2015}$ and we get ![]()
Beyond the Basic Expressions
Grouping Basic Expressions
Our ordinary parentheses (...) and brackets [...] work to group expressions in
. For instance, $(x+y)[z+w]$ gives us
We can also group expressions using curly braces, but we can’t just type {...}. Rather, we must type \{...\}. This is because
uses plain curly braces for other things, such as fractions and superscripts and subscripts.
When we put (vertically) large expressions inside of parentheses (or brackets, or curly braces, etc.), the parentheses don’t resize to fit the expression and instead remain relatively small. For instance, $$f(x) = \pi(\frac{\sqrt{x}}{x-1})$$ comes out as
To automatically adjust the size of parentheses to fit the expression inside of them, we type \left(...\right). If we do this for our
equation above, we get
We can use \left and \right for all sorts of things… parentheses (as we saw), brackets $\left[...\right]$, braces $\left\{...\right\}, absolute values $\left|...\right|$, and much more (norms, floor and ceiling functions, inner products, etc.).
Lists
To make a list, such as a sequence, we use \dots. For example, $a_0,a_1,\dots,a_n$ will give us ![]()
Sums
There are two basic ways to write out sums. First, we can use + and \cdots. An example of this way would be $a_1+a_2+\cdots+a_n$ This will give us
Second, we could use summation notation, or \sum. Such an example is $\sum_{i=0}^n a_i$, giving
Note the use of superscripts and subscripts to obtain the summation index.
Products
Again, there are two basic ways to display products. First, we can use \cdot and \cdots. An example is $n! = n\cdot(n-1)\cdots 2\cdot 1$, which of course gives
The alternative is to use product notation with \prod. For instance, $n! = \prod_{k=1}^n k$, giving ![]()
Equalities and Inequalities
Inequalities
the commands >, <, \geq, \leq, and \neq give us
and
respectively.
Aligning Equations
To align multiple equations, we use the align* environment. For example, we might type a system of equations as follows:
\begin{align*}
ax + by &= 1 \\
cx + dy &= 2 \\
ex + fy &= 3.
\end{align*}
(You do not need dollar signs.) The & symbol tells
where to align to and the \\ symbols break to the next line. This code will output
An example of a string of equations is:
\begin{align*}
((2x+3)^3)' &= 3(2x+3)^2 \cdot (2x+3)' \\
&= 3(2x+3)^2 \cdot 2 \\
&= 6(2x+3)^2.
\end{align*}
Again, the & symbol tells
where to align to, and the \\ symbols break to the next line. This code outputs 
Numbering Equations
To number equations, we use the align environment. This is the same environment as the align* environment, but we leave the * off. The * suppresses numbering. To number one equation, the code
\begin{align}
ax + by = c
\end{align}
will produce
We don’t have to use & or \\ since there is nothing to align and no lines to break. To number several equations, such as a system, the code
\begin{align}
ax + by &= c \\
dx + ey &= f \\
gx + hy &= i
\end{align}
will produce
In general, align will auto-number your equations from first to last.
Comments in Equations
Again, we use the align* environment. The code
\begin{align*}
ax + by &= c & \text{because blah} \\
dx + ey &= f & \text{by such-and-such}
\end{align*}
will produce
(You can use align to get numbering and comments!)
Definition by Cases
To define, say, a function by cases, we use the cases environment. The code
$$ \delta(i,j) = \begin{cases} 0 & \text{if } i \neq j \\ 1 &\text{if } i = j \end{cases} $$
gives us
As usual, the & is for aligning and the \\ is for line-breaking.
LaTeX:Diagrams
| LaTeX |
| About – Getting Started – Diagrams – Symbols – Downloads – Basics – Math – Examples – Pictures – Layout – Commands – Packages – Help |
Diagrams and Figures
It’s best to rely on Asymptote for drawing diagrams and figures, as
does not have the ability to output good diagrams for geometry and such. However,
does work wonderfully well with Asymptote.
Integer coordinates inside a cube of sidelength 6
![[asy] usepackage("tikz"); label("\begin{tikzpicture}[domain=0:6,x=5mm,y=5mm,z=2.5mm] %\draw (0,0) grid (5,5); \foreach \i in {0,...,6}{ \foreach \z in {0,...,6}{ \foreach \m in {0,...,6}{ \node[red] at (\i ,(\m ,\z ) {\textbullet }; ); \node[red] at (\m ,(\i ,\z ) {\textbullet }; ); \node[blue] at (\z , (\i ,\m ) {\textbullet }; ); }; }; } \draw (0,0)--(0,6); \draw (0,0)--(6,0); \draw plot (6,(0,\x);); \draw plot (6,(6,\x);); \draw plot (0,(6,\x);); \draw (0,6)--(6,6); \draw (6,0)--(6,6); \end{tikzpicture}"); [/asy]](https://latex.artofproblemsolving.com/a/2/d/a2da003f181a809614d052dbc623346e20a0eca1.png)
LaTeX:Basics
First LaTeX Document
To create your first LaTeX document, start by opening a new file in TeXnicCenter. Start TeXnicCenter if you haven’t already done so. To open a new file in TeXnicCenter, click File in the upper left, then click New. A blank document should open. Type the following lines (or copy and paste them):
\documentclass{article}
\begin{document}
Hello, world!
\end{document}
Note that it is crimped brackets surrounding the words “article” and “document” in the above example, not parentheses. As you will see, crimped brackets(braces) are used for mathematical signs like \frac{x}{y} to make the fraction
. You will use this in countless ways when typing LaTeX. These braces indicate mandatory arguments that must be included with the command. Brackets ([ ]) are optional arguments, that do not have to be written with the command. For example, \sqrt[n]{x} produces
. The [n] does not have to be included if no root is needed, so you can just use \sqrt{x} to produce
. However, the {x} must always be included.
Save the document (press Ctrl-S or click File, then Save) as ‘Hello’ (don’t include the quote marks in the name) in a folder of your choice.
The file will appear in your folder as ‘Hello.tex.’ We will call this the source file. In order to create a finished LaTeX document, we need to compile the source file. First, make sure that the bar near the top of the TeXnicCenter window reads “LaTeX => PDF”; if it doesn’t, change it so that it does. Then to compile the file, do any one of the following:
- Select “Build” -> “Current File” -> “Build” from the top menu bar.
- Click on the “Build Current File” icon (it’s the down-pointing arrow over the stack of papers, just to the right of the “LaTeX => PDF” bar).
- Press Ctrl-F7.
You’ll see some system messages scroll in the “Output” panel at the bottom of the window, and then eventually you should (hopefully) see the line “0 Error(s), 0 Warning(s), 0 Bad Box(es), 1 Page(s)”. This means that your document (a PDF file) is ready. To view your document, do any one of the following:
- Select “Build -> “View Output” from the top menu bar.
- Click on the “View Output” icons (it’s the magnifying glass over the sheet of paper, just to the right of the “Build Current File” icon from above).
- Press F5.
This will open up Adobe Reader and show your finished document. You can print the document from Adobe Reader if you like.
For GNU/Linux
Open any Tex editor and save the text below into a file hello.tex,
\documentclass{article}
\begin{document}
Hello, world!
\end{document}
- Next open terminal and change directory to where the file is saved(for example cd ~/playground if it is in the playground folder).
- Next, pdflatex hello.tex and if you see no errors (“0 Error(s), 0 Warning(s), 0 Bad Box(es), 1 Page(s)”), you have successfully done it.
- A hello.pdf file was created in the directory, open it with a pdf-reader such as evince or xpdf.
Style
You’ll develop your own style of using LaTeX. The sections below outline some tips that you’ll find helpful.
Space
In LaTeX, you can use as much space as you like–LaTeX won’t see it when it compiles. If you put in three spaces, it will only see one. If you put in 6 empty lines, it’s the same as one. Therefore, this:
\documentclass{article}
\begin{document}
This is a sample document.
It illustrates how \LaTeX\ treats space.
\end{document}
results in the same as this:
\documentclass{article}
\begin{document}
This is a sample document.
It illustrates how \LaTeX\ treats space.
\end{document}
Generally, write so that it’s easy for you to read the source code. Don’t pour in tons of space, but leave enough that you can easily find parts of the source file later.
TeXnicCenter (and most editors you might use for LaTeX) treats line numbering differently than your typical text editor. If you just type and type text without ever hitting <Enter> so that your text scrolls on to the next line, TeXnicCenter sees all that text on the next line as just a continuation of the previous line. This is no big deal if you never make a mistake, but when an error occurs when you compile, you usually have to find the mistake in your source file by line number. If you have a 4 or 5 line ‘line’, finding the error can be a real headache. (You can see what line you are on in TeXnicCenter by looking at the bottom towards the right.)
Just as LaTeX doesn’t see tons of extra space, it won’t see space that isn’t there. One primary example of this is that LaTeX will only see linebreaks if there is an empty line or if you use the \newline command. Hence, this:
\documentclass{article}
\begin{document}
This is a sample document. It illustrates how \LaTeX\ treats space.
\end{document}
will produce the same result as:
\documentclass{article}
\begin{document}
This is a sample document.
It illustrates how \LaTeX\ treats space.
\end{document}
Comments
You can leave yourself (or later users of your source files) notes by using %. Anything in a line after % is ignored when the file is compiled. Try this:
\documentclass{article}
\begin{document}
%You won't see this in the final document.
You do see this.
\end{document}
You’ll see that comments are grayed out in TeXnicCenter. Comments are very useful to guide other readers of your source file, and to guide you in case you’ll come back to a file later. You’ll find comments throughout our sample files.
Including Packages
If you have commands that you have created or a specific layout you like to use, you don’t have to always copy and paste all that LaTeX code into the top of each LaTeX source file. Instead, you can create your own style files to include in your document. Read about Packages to learn how to create and include files of your own. Beginners should hold off on this until they’ve become more comfortable with LaTeX.
See Also
LaTeX:Math
Math Mode
LaTeX uses a special math mode to display mathematics. To place something written in TeX in math mode, use $ signs to enclose the math you want to display. For example, open a new source file in TeXnicCenter and type or copy/paste the following:
\documentclass{article}
\begin{document}
The solution to $\sqrt{x}=5$ is $x=25$.
\end{document}
Save the document (press Ctrl-S or click File, then Save) as ‘mymath’ (don’t include the quote marks in the name) in a folder of your choice. The file will appear in your folder as ‘mymath.tex.’
Compile the document just as you compiled your first document. When you view the output file, you should see
If you remove the $ symbols from your source file then try to compile, you should get ‘Missing $ inserted’ error messages in the Output window of TeXnicCenter (try it and see – you may have to scroll up in the Output window to see the errors).
Nearly all mathematical items, such as variables, expressions, equations, etc., should be written in math mode. In fact, most math will generate errors if you don’t remember to put it in math mode.
Display Math
As we saw above, when using $…$ to typeset math, the resulting math expression appears in-line. Sometimes, we may wish to display a mathematical expression on its own line. To do so, we use \[math stuff here\] or $$math stuff here$$ (the former is usually preferred now) to put the expression in display math mode:
\documentclass{article}
\begin{document}
The solution to \[\sqrt{x} = 5\] is \[x=25.\]
\end{document}
After you compile this and view it, you should see:
\[Math stuff here\] no longer works on the AoPS forums.
Notice that the equations are on their own lines and are centered. As a matter of style, usually we put this display math on their own lines in the source file, like this:
\documentclass{article}
\begin{document}
The solution to
\[
\sqrt{x} = 5
\]
is
\[
x=25.
\]
\end{document}
We can also use
\begin{equation} … \end{equation}
to display mathematics. This also creates a label, which we can refer to throughout the document using \label and \ref (or \eqref, using the amsmath package). See the following example:
\documentclass{article}
\usepackage{amsmath}
\begin{document}
The quadratic formula is shown in Equation~\eqref{eqn:quadratic}.
\begin{equation} \label{eqn:quadratic}
x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a}
\end{equation}
Using Equation~\eqref{eqn:quadratic}, we obtain...
\end{document}
Notice the (1) to the right of the equation. Once again, rather than typing (1) in your source file to refer to this equation, use LaTeX referencing commands. It is also considered good style to add ~ before commands such as \ref, \eqref, \cite; the ~ symbol represents an unbreakable space (so that you do not have a reference on the following line).
Display Style (\displaystyle)
Sometimes we have complicated expressions that we don’t want to put on their own lines, but that doesn’t render well with $…$ mode. For example:
\documentclass{article}
\begin{document}
Evaluate the sum $\sum_{i=0}^n i^3$.
\end{document}
gives us
That summation symbol is a little ugly. We can make it prettier by using \displaystyle:
\documentclass{article}
\begin{document}
Evaluate the sum $\displaystyle\sum\limits_{i=0}^n i^3$.
\end{document}
This gives us:
Notice that the summation symbol looks much nicer now – adding the \displaystyle at the beginning of your math (inside the $…$) will often make complicated math render more nicely. Note that it is not necessary to use \displaystyle when using display mode (\[ and \] or \begin{equation} and \end{equation}).
Aligning Equations (align)
A pair of very useful tools for displaying equations well are the “align” and “align*” environments. They allow you to neatly align a string of equations:
\documentclass{article}
\usepackage{amsmath}
\begin{document}
\begin{align*}
2x^2 + 3(x-1)(x-2) & = 2x^2 + 3(x^2-3x+2)\\&= 2x^2 + 3x^2 - 9x + 6\\&= 5x^2 - 9x + 6
\end{align*}
\end{document}
Compiling this should give: 
There are a few things to notice here. First, the align command requires that you use the package amsmath (and there’s no reason to not use this package). Second, the * after align prevents line numbers from popping up after each line – try removing both of the *s from the source file and compile to see equation numbers. Next, notice that each line is of the form
Math stuff & more math stuff \\
The & symbols separate the columns. There must be two columns (i.e. one & symbol). The \\ tells LaTeX that you are finished with this line and are on to the next. Notice that there’s no \\ on the last line; the \end{align*} tells LaTeX that you’re finished. As you see above, you can leave some columns blank. As a style issue, notice that we start a new line in our source file after each \\. We could run all the lines together, but that makes editing very difficult.
Typically, we use relational symbols like =, >, or < immediately following the &; align ensures that these symbols are arranged into a vertical column as you see above. That’s why we like align.
Finally, notice that there are no <dollar/> symbols, $$ … $$, or \[ … \], yet everything is rendered in math mode. This happens because align automatically puts everything in math mode – you don’t need <dollar/>s or \[ … \] tags.
Also note that in an align environment, you can use the \nonumber command if you want only some lines to be numbered. For example,
\documentclass{article}
\usepackage{amsmath}
\begin{document}
\begin{align}
2x^2 + 3(x-1)(x-2) & = 2x^2 + 3(x^2-3x+2)\\ \nonumber &= 2x^2 + 3x^2 - 9x + 6\\ &= 5x^2 - 9x + 6
\end{align}
\end{document}
compiles to this: 
Additional Packages
The basic LaTeX program does not include all the math you’ll want to use. In order to access all the math functions and symbols we will introduce in the guide pages, you’ll have to include a number of packages. We include these packages by using the \usepackage command between the \documentclass line and the \begin{document} line, such as:
\documentclass{article}
\usepackage{amsmath}
\begin{document}
We can write more than just $x$ now.
Now we can write things like $\binom{5}{3}$.
\end{document}
The package used above is part of the basic MiKTeX installation, so you don’t have to download anything new to include them. Later, you may want to read more about how to include more packages and how you can create packages of your own.
Finally, one last point of style – notice in that last example that we put the x in math mode by writing $x$ instead of just x. Try compiling with and without the x in math mode and you’ll see why. Always put your math in math mode!
If you find you want to do some math typesetting that you can’t find on this page, or among our discussions of symbols or commands, try reading the user’s guide for the amsmath package, which contains some of the really fancy applications of the ams packages.
See Also
LaTeX:Examples
| LaTeX |
| About – Getting Started – Diagrams – Symbols – Downloads – Basics – Math – Examples – Pictures – Layout – Commands – Packages – Help |
Before you move on to advanced commands and a library of symbols and keywords to memorize, look at a few LaTeX documents prepared by Art of Problem Solving Community members. Students wishing to submit examples should not feel obliged to make documents as intricate as those below – we would like to include some more introductory examples with these. The papers below are listed roughly in order of the complexity of LaTeX tools used.
Contents
The Basics: Twenty-Seven Problems
The Basics: Twenty-Seven Problems by Keone Hon. Click here for the source file. This file contains a collection of problems and solutions. Our beginning and newly intermediate-level problem solvers will find some good practice here.
A Diophantine Equation and Its Solution
A Diophantine Equation and Its Solution by Michael Viscardi. Click here for the source file. This paper contains a solution to the Diophantine Equation
. (Note this was produced within a few days of the student learning about LaTeX from our site – look at the paper for evidence of how quickly you can learn to render math in LaTeX.)
The Area of a Circle
The Area of a Circle by Community member nr1337. Click here for the source file. This example shows some mathematical writing including calculus and the newtheorem environment.
Solution for USAMTS Problem 4/4/15
Solution for USAMTS Problem 4/4/15 by Zachary Abel. Click here for the source file. This file gives an example of writing up a solution to a USAMTS solution. The LaTeX code includes an example of referencing. (Note: You will have to compile code with referencing in it twice – the first time you compile you will get warnings about undefined references.)
On the Existence and Uniqueness of Invariant Measures on Locally Compact Groups
On the Existence and Uniqueness of Invariant Measures on Locally Compact Groups by Simon Rubinstein-Salzedo. Click here for the source file. This will give you an example of a document that would be terrifying to typeset with basic word processors. It will also give students an idea of what to look forward to in very advanced mathematics. (In other words, middle and high school students shouldn’t expect to understand much, if any of it, but it is a great example of how to clearly render advanced math with LaTeX.)
Calculus Review and Formulas
Calculus Review and Formulas by Keone Hon. Click here for the source file. This example contains review of trigonometric formulas and some items of calculus. The LaTeX code exhibits some referencing, and uses of the definition and formula environments. This also illustrates the package hyperref, which allows us to put links to jump to in the document (you’ll see when you open the PDF document how useful these are).
Maxwell’s Equations
Maxwell’s Equations by Matt Hansen. Click here for main source file. Click here for bibliography file. This paper illustrates the use of BibTeX to compile a bibliography. To compile it completely, put both the main source file and the bibliography files in the same directory. Compile the source as usual. Then, in TeXnicCenter, click Build, then Current File, then BibTeX. Then compile the source as usual (you may need to compile the source twice after doing the BibTeX). This paper gives an overview of Maxwell’s Equations.
See Also
LaTeX:Pictures
| LaTeX |
| About – Getting Started – Diagrams – Symbols – Downloads – Basics – Math – Examples – Pictures – Layout – Commands – Packages – Help |
This article will cover incorporating pictures and diagrams into LaTeX documents. If you’re dealing strictly with geometric diagrams, consider reading about Asymptote, a graphics language that works extremely well with LaTeX–as you can see from the length of this article, working with graphics and pictures without Asymptote in LaTeX is no easy feat.
Contents
How to Incorporate Pictures
You can use more filetypes than just .png’s–you can even use a 1 page .pdf file as your image! You could also produce geometric diagrams within LaTeX as shown in the Asymptote article. Otherwise, if you are using LaTeX to produce PDF documents, you can make images such as geometric diagrams in your documents. The machine that we’ll use to include images is the graphicx package.
To walk through this example, first download a sample image by clicking here to open the image, then save it to your own computer. (Alternatively, depending on your browser, you may be able to right-click on the link to the image and choose “Save link as…”) Save the image with the name ‘myimage.png’. Make sure you pay attention to what folder you are saving the image to.
Next, open a new LaTeX source file in TeXnicCenter and input the following:
\documentclass{article}
\usepackage[pdftex]{graphicx}
\begin{document}
This is my first image.
\includegraphics{myimage.png}
That's a cool picture up above.
\end{document}
Save your source file in the same folder as you saved myimage.png. (Very important – if you save it in a different folder, you will get an error!)
Compile your source code and view the result. Make sure you have TeXnicCenter set up to produce PDF documents (LaTeX => PDF in the appropriate drop-menu). If you don’t, you’ll get a bunch of errors because LaTeX will expect your pictures to be in the EPS (Encapsulated PostScript) format. If you followed all the steps correctly, you should get a file that looks a lot like what you’ll see when you click here.
Let’s take a closer look at how this code works. First, notice the new line
\usepackage[pdftex]{graphicx}
This line includes the package graphicx, which contains all the image-handling routines you need to include images. To include a specific image, we use the statement:
\includegraphics{myimage.png}
Picture Manipulation
In this section we’ll discuss how you can manipulate images in LaTeX.
Note that some of these commands work with text as well (see reflecting) – diagrams are not the only items that can be reflected, rotated, etc. In practice, the centering is the only one of the above you’re likely to use much with text.
We’ll start where we left off on the How To page. If you haven’t already reviewed that page, do so now. Make sure you save the myimage.png file to your computer for use in these examples. Our original source code is:
\documentclass{article}
\usepackage[pdftex]{graphicx}
\begin{document}
This is my first image.
\includegraphics{myimage.png}
That's a cool picture up above.
\end{document}
Usually, we manipulate the image by adding an optional command to the \includegraphics statement. This optional command goes in brackets […] between the \includegraphics command and the name of the image. For example,
\includegraphics[angle=45]{myimage.png}
will rotate the image 45 degrees.
Cropping
The viewport optional command of the \includegraphics command tells what portion of the image file to include. The first two coordinates are the (x,y) coordinates in pixels of the lower left corner of the portion of the image file you want to include. The second two coordinates are the upper right values of (x,y). Try changing the values in the original source file and see what happens.
\documentclass{article}
\usepackage[pdftex]{graphicx}
\begin{document}
This is my first image.
\includegraphics*[viewport=30 30 120 120]{myimage.png}
That's a cool picture up above.
\end{document}
For example, the code above should result in a document like this. You can see that we only have a 90×90 pixel part of the image.
You may have noticed the subtle change to \includegraphics*: we added a * to the end of the command. This * “clips” the image to the viewport that we specified. If we had omitted it:
\documentclass{article}
\usepackage[pdftex]{graphicx}
\begin{document}
This is my first image.
\includegraphics[viewport=30 30 120 120]{myimage.png}
That's a cool picture up above.
\end{document}
we end up shifting the image to a new position, but not cropping it, as shown here.
To set the coordinates for an image, it is sometimes easiest to use trial and error, but it’s also possible to read the coordinates by loading the image in GIMP (for a png) or ghostview (for a ps or pdf) and moving the cursor.
Spacing
One issue to watch out for is spacing. Suppose we take out the empty lines from our original source code:
\documentclass{article}
\usepackage[pdftex]{graphicx}
\begin{document}
This is my first image.
\includegraphics{myimage.png}
That's a cool picture up above.
\end{document}
If you compile this, you should get something that looks like this. By removing the blank lines, we are telling LaTeX to try to put the image in the same line as the text. You see the mess that results. Generally, put an extra line before and after your image inclusion in your source file and you should be fine. This will also help you find the image in the source file if you need to edit.
Scaling and Resizing
We can easily scale the size of the diagram by using the scale optional command.
\documentclass{article}
\usepackage[pdftex]{graphicx}
\begin{document}
This is my first image.
\includegraphics[scale=0.25]{myimage.png}
That's a cool picture up above.
\end{document}
This scaled our figure by a factor of 0.25 (in other words, it makes it 4 times smaller), as shown here.
We can also scale the picture by explicitly setting the width and the height by using the width and height optional commands. For example:
\documentclass{article}
\usepackage[pdftex]{graphicx}
\begin{document}
This is my first image.
\includegraphics[width=5in,height=1in]{myimage.png}
That's a cool picture up above.
\end{document}
resizes our image to be 5 inches by 1 inch, as shown here.
Rotating
We can rotate a diagram by using the angle optional command.
\documentclass{article}
\usepackage[pdftex]{graphicx}
\begin{document}
This is my first image.
\includegraphics[angle=45]{myimage.png}
That's a cool picture up above.
\end{document}
This rotates our picture 45 degrees (counterclockwise), as shown here.
Reflecting
We can horizontally reflect a diagram by using the \reflectbox command.
\documentclass{article}
\usepackage[pdftex]{graphicx}
\begin{document}
This is my first image.
\reflectbox{\includegraphics{myimage.png}}
That's a cool picture up above.
\end{document}
See the result here – note that the text in the center of the image now reads backwards! The \reflectbox command is pretty simple – put the item to be horizontally reflected in { }:
\reflectbox{item reflected}
\reflectbox has two companion commands \scalebox and \rotatebox. These can be used with text as well as graphics, to produce some funky effects:
\documentclass{article}
\usepackage[pdftex]{graphicx}
\begin{document}
This document shows some funky effects with text.
\scalebox{2}{\rotatebox{60}{\reflectbox{This is really weird text!}}}
\end{document}
The output document shows text that is doubled in size, rotated 60 degrees, and reflected.
Centering
We center text or images using \begin{center} and \end{center}.
\documentclass{article}
\usepackage[pdftex]{graphicx}
\begin{document}
This is my first image.
\begin{center}
\includegraphics{myimage.png}
\end{center}
That's a cool picture up above.
\end{document}
Just put \begin{center} when you want to start centering, and \end{center} when you want to stop centering. (If you want to center everything until the end of the document, you still need an \end{center} before the \end{document} at the end of the source file.
The code above should give you something like this.
See Also
LaTeX:Layout
| LaTeX |
| About – Getting Started – Diagrams – Symbols – Downloads – Basics – Math – Examples – Pictures – Layout – Commands – Packages – Help |
This article outlines some of the basics of layout in LaTeX.
Note: Rather than typing up all the examples, you can copy-paste the examples into your TeXnicCenter files. We highly recommend opening up your TeXnicCenter and trying out each of the examples as you go. It takes almost no time at all to just copy-paste, compile, and view the results.
Contents
Source File Format
The source file of a LaTeX broadly consists of two parts, the preamble and the document itself. The preamble consists of everything before the \begin{document} command. Things like margin settings, document style definitions, paragraph spacing settings, custom function definition and page numeration style are items that are set in the preamble. Often, much of the preamble is placed in a separate file and included using the \usepackage statement. This allows you to use the same code in many source files by just including a single line in each source file.
Our next three sections deal primarily with preamble items, while the rest cover tools you might use within your document.
The Preamble
Document Class
The first line of the file sets the document class with the \documentclass[options]{class} command. For typical use the document class should be article. (Other supported classes are book, report, letter, as well as beamer for presentation slides.) There are several options that can be set with \documentclass, but generally they can be left to their defaults. One option that might need modification is the font size, which is 10pt by default but can be increased to 11pt or 12pt. A reference on other options for this command can be found here.
Packages
Packages are included after the \documentclass statement using \usepackage[options]{packagename}. Two common packages are amsmath, which allows you to write math formulas in your document, and graphicx with the pdftex option, which allows you to include images in your document.
Page Setup
LaTeX automatically sets reasonable values for the page dimensions, orientation, etc. However, in some cases customization may be required. There are two ways to do this: the easy way, using several packages which do all the work for you, and the hard way, which involves doing all the work yourself.
The Easy Way
The easy way involves using certain packages to do the heavy lifting. For example, to set the margins using the geometry package, use the line \usepackage[margin=2.5cm]{geometry}. Check out the geometry package user manual for more detailed possibilities. As another example, if you want a layout where paragraphs are separated by some space, but not indented (like in most HTML pages), use the parskip package: \usepackage{parskip}.
The Hard Way
The hard way involves setting all the desired values manually. Here are some values that can be set:
| \pdfpageheight, \pdfpagewidth | Dimensions of the PDF file. |
| \topmargin | Length of margin at top of page above all printing. 1 inch is added to this value. |
| \evensidemargin | Left margin on even numbered pages. 1 inch is added to this value. |
| \oddsidemargin | Left margin on odd numbered pages. 1 inch is added to this value. |
| \headheight | Height of the page header. |
| \headsep | Distance from bottom of header to the body of text on a page. |
| \topskip | Distance from top of main text box to the baseline of the first line of text in the main text box. |
| \textheight, \textwidth | Height and width of main text box. |
| \footskip | Distance from bottom of body to the bottom of the footer. |
| \parskip | Distance between paragraphs. |
| \parindent | Amount of indentation at the first line of a paragraph. |
The \setlength{length}{value} command is used to set these lengths. Units include inches (in), centimeters (cm) and points (pt). Note that these lengths can be negative. Here is an example:
\setlength{\topmargin}{0in}
\setlength{\headheight}{0in}
\setlength{\headsep}{0in}
\setlength{\textheight}{7.7in}
\setlength{\textwidth}{6.5in}
\setlength{\oddsidemargin}{0in}
\setlength{\evensidemargin}{0in}
\setlength{\parindent}{0.25in}
\setlength{\parskip}{0.25in}
Note that the right margin of a page is automatically set based on the left margin (with \oddsidemargin and \evensidemargin) and the width of the text (with \textwidth).
There are many other parameters that you can set in the preamble, such as the title of the document, the header style, the footer style, page numbering, etc. You can consult books or Google for more information on these areas.
Document Formatting
The document begins after the preamble. Use the command \begin{document} to start the document and \end{document} at the end.
This section will cover techniques to format material in the document you create.
Paragraphs
Any time LaTeX sees a blank line, it treats the next line as the start of a new paragraph. For example, try the following text:
Fourscore and seven years ago our fathers brought forth on this continent a new nation, conceived in liberty and dedicated to the proposition that all men are created equal. Now we are engaged in a great civil war, testing whether that nation or any nation so conceived and so dedicated can long endure. We are met on a great battlefield of that war. We have come to dedicate a portion of it as a final resting place for those who died here that the nation might live. This we may, in all propriety do. But in a larger sense, we cannot dedicate, we cannot consecrate, we cannot hallow this ground. The brave men, living and dead who struggled here have hallowed it far above our poor power to add or detract. The world will little note nor long remember what we say here, but it can never forget what they did here. It is rather for us the living, we here be dedicated to the great task remaining before us--that from these honored dead we take increased devotion to that cause for which they here gave the last full measure of devotion--that we here highly resolve that these dead shall not have died in vain, that this nation shall have a new birth of freedom, and that government of the people, by the people, for the people shall not perish from the earth.
When you typeset this, you should find that each of the three sections above is indented and is its own paragraph. Moreover, you should see that if you don’t have a full empty line between two lines in your source file, there is not only no new paragraph, but there is no line break, either. To end one paragraph and start another it is not enough to simply hit return and start typing on the next line – you must hit return twice and create an empty line for LaTeX to know to start a new paragraph.
You can also create indents wherever you want in text by adding the \indent command, and you can suppress the automatic indent caused by a new paragraph by using \noindent. For example, try typesetting this:
Fourscore and seven years ago our fathers brought forth on this continent a new nation, conceived in liberty and dedicated to the proposition that all men are created equal. \indent \indent Now \indent we \indent are engaged in a great civil war, testing whether that nation or any nation so conceived and so dedicated can long endure. We are met on a great battlefield of that war. We have come to dedicate a portion of it as a final resting place for those who died here that the nation might live. This we may, in all propriety do. But in a larger sense, we cannot dedicate, we cannot consecrate, we cannot hallow this ground. The brave men, living and dead who struggled here have hallowed it far above our poor power to add or detract. The world will little note nor long remember what we say here, but it can never forget what they did here. \noindent It is rather for us the living, we here be dedicated to the great task remaining before us--that from these honored dead we take increased devotion to that cause for which they here gave the last full measure of devotion--that we here highly resolve that these dead shall not have died in vain, that this nation shall have a new birth of freedom, and that government of the people, by the people, for the people shall not perish from the earth.
Note the effects of \indent and \noindent. Finally, understanding paragraphing rules in LaTeX is very important when using display math. Notice the difference in the following:
Now, if we can prove
\[ \Big(2\sqrt{\frac{a}{b}}\Big)^m + \Big(2\sqrt{\frac{b}{a}}\Big)^m
\ge 2^{m+1} \]
then we will be done. Dividing both sides of this inequality by $2^m$ yields
\[ \Big(\frac{a}{b}\Big)^m + \Big(\frac{b}{a}\Big)^m \ge 2, \]
which we can verify easily by AM-GM on the reciprocals that make up the LHS,
thus we have our desired
\[ \Big(1 + \frac{a}{b}\Big)^m + \Big(1 + \frac{b}{a}\Big)^m \ge
\Big(2\sqrt{\frac{a}{b}}\Big)^m + \Big(2\sqrt{\frac{b}{a}}\Big)^m \ge 2^{m+1}. \]
and
Now, if we can prove
\[ \Big(2\sqrt{\frac{a}{b}}\Big)^m + \Big(2\sqrt{\frac{b}{a}}\Big)^m
\ge 2^{m+1} \]
then we will be done. Dividing both sides of this inequality by $2^m$ yields
\[ \Big(\frac{a}{b}\Big)^m + \Big(\frac{b}{a}\Big)^m \ge 2, \]
which we can verify easily by AM-GM on the reciprocals that make up the LHS,
thus we have our desired
\[ \Big(1 + \frac{a}{b}\Big)^m + \Big(1 + \frac{b}{a}\Big)^m \ge
\Big(2\sqrt{\frac{a}{b}}\Big)^m + \Big(2\sqrt{\frac{b}{a}}\Big)^m \ge 2^{m+1}. \]
In the latter, there are indents after each display math, which we clearly don’t want. These are caused by the blank lines (and could be suppressed with \noindent).
Sections
For longer documents that you want to split into parts, you can use LaTeX sectioning commands. Here’s an example illustrating them. If you use the book document class, you can also use \chapter, but for most documents short of books, these should be sufficient.
\section{In This First Section}
This is the first section.
\subsection{We Have This First Subsection}
This is the first subsection.
\subsubsection{And This Subsubsection}
A subsubsection.
\paragraph{And This Paragraph}
A notable paragraph.
\subparagraph{And This Subparagraph}
A notable subparagraph.
\subsubsection{And Then This Subsubsection}
\section{The This Second Section}
\subsection{We Have This Subsection}
Font Sizes and Styles
To change the font size, use any one of the following commands. To change it for just a portion of the page, enclose that potion in { } and have the relevant font size command occur right at the beginning of the text inside the curly braces. In order from smallest to largest, the font sizes you can use are:
| \tiny |
| \scriptsize |
| \footnotesize |
| \small |
| \normalsize |
| \large |
| \Large |
| \LARGE |
| \huge |
| \Huge |
Try this out; the effects should be pretty clear:
When I was born, I was {\small small}. Actually, {\scriptsize I was
very small}. When I got older, I thought some day {\Large I would be
large}, {\Huge maybe even gigantic}. But instead, I'm not even
normalsize. {\small I'm still small.}
Here is a simple example that will probably show you all you need to know about bold, italics, and underlining.
When something is \emph{really}, \textbf{really} important, you can
\underline{underline it}, \emph{italicize it}, \textbf{bold it}. If
you \underline{\textbf{\emph{must do all three}}}, then you can nest
them.
Here is another example that demonstrates font families:
You may want to write things \textsf{in a sans-serif font}, or
\texttt{in a typewriter font}, or \textsl{in a slanted font} (which
is \emph{slightly different} than italics). Sometimes it pays
\textsc{to write things in small capitals}. You can next go to
\textbf{bold and then \textsl{bold and slanted} and then back to just
bold} again.
Spacing
There are a few spacing items you’ll find useful in LaTeX. First, you can force a normal-size space (as between words) by using a single backslash followed by a space. This is particularly useful after periods: LaTeX interprets periods as ends of sentences, so it puts extra space after them, but if a period doesn’t in fact end a sentence, you don’t want that extra space. Try this to see an example.
When Mr. Rogers read this, he was confused because the first sentence was only two words long. Mrs.\ Rogers wasn't confused at all.
In math mode, it’s a little different. LaTeX ignores normal spaces in math mode, so all three of the following will come out the same:
Spacing in math mode: $x + y$ $x + y$ $x+y$
Notice that the three math expressions come out all exactly the same. In general, you can trust math mode to space things out right rather than forcing any special spacing. This means that you should write formulas in your source document to be easily readable (by you), and trust LaTeX to do the right spacing.
However, if you do need to tweak the spacing in math mode, there are some special commands:
| \, | a small space |
| \: | a medium space |
| \; | a large space |
| \quad | a really large space |
| \qquad | a huge space |
| \! | a negative space (moves things back to the left) |
Here are examples of these in action:
$x+y$ $x+\,y$ $x+\:y$ $x+\;y$ $x+\quad y$ $x+\qquad y$ $x+\!y$
Two spacing tools that can be bluntly used to move things are \hspace and \vspace.
I \hspace{2in} like \hspace{1in} space.
\vspace{5in}
In every direction.
Sometimes, \hspace and \vspace will not produce the space you want; this happens most frequently at the beginning or the end of a line or of a page. If you want to force space there, use \hspace* or \vspace* instead.
Two more that can be used even more bluntly are \hfill and \vfill, which cause LaTeX to create as much horizontal or vertical space as possible (within the boundaries of a page as defined in the preamble):
I\hfill like \hfill space. \vfill In every direction.
Finally, you can force line breaks and prevent them in LaTeX. To force a linebreak somewhere in a block of text, simply use \\. To prevent a linebreak, you can use ~. The ~ in the text below prevents LaTeX from breaking a line between Professor and Reiter:
This website is largely a result of Professor Harold Reiter getting me involved in math education again.\\ Professor~Reiter's dedication to educating eager math students is an inspiration. Students, parents, and educators who like this site should thank Professor~Reiter. If you don't already know Professor~Reiter, don't worry, you'll eventually meet him. Professor~Reiter meets everybody.
Try taking out the ~ symbols, and you’ll probably see Professor and Reiter get split up at least once. Notice also that the \\ forces LaTeX to start a new line, but not a new paragraph. \par can be used to introduce a new paragraph.
Justification (Centering, etc).
We can center text using
\begin{center} text \end{center}
and can justify it left or right with
\begin{flushleft} text \end{flushleft} \begin{flushright} text \end{flushright}
\begin{center}
Fourscore and seven years ago our fathers brought forth on this
continent a new nation, conceived in liberty and dedicated to the
proposition that all men are created equal.
\end{center}
\begin{flushleft}
Now we are engaged in a great civil war, testing whether that nation
or any nation so conceived and so dedicated can long endure. We are
met on a great battlefield of that war. We have come to dedicate a
portion of it as a final resting place for those who died here that
the nation might live. This we may, in all propriety do. But in a
larger sense, we cannot dedicate, we cannot consecrate, we cannot
hallow this ground. The brave men, living and dead who struggled here
have hallowed it far above our poor power to add or detract. The
world will little note nor long remember what we say here, but it can
never forget what they did here.
\end{flushleft}
\begin{flushright}
It is rather for us the living, we here be dedicated to the great
task remaining before us--that from these honored dead we take
increased devotion to that cause for which they here gave the last
full measure of devotion--that we here highly resolve that these dead
shall not have died in vain, that this nation shall have a new birth
of freedom, and that government of the people, by the people, for the
people shall not perish from the earth.
\end{flushright}
You can also use \raggedleft to replace \begin{flushright} (and omit the \end{flushright}) and use \raggedright to replace \begin{flushleft} (and omit the \end{flushleft}).
Tables
The primary way to build a table is to use the tabular environment. Here’s an example:
\begin{tabular}[t]{|l|ccccc|c|}
\multicolumn{7}{c}{USAMTS Scores Round 1}\\\hline
Name&\#1&\#2&\#3&\#4&\#5&Total\\\hline
John Doe&5&5&3&2&1&16\\
Jane Doe&5&5&5&4&5&24\\
Richard Feynman&5&5&5&5&5&25\\\hline
\end{tabular}
When you typeset that code, you should see a simple table. Read through the following general description of the tabular environment to understand how the code above produced the table.
General form of the tabular environment:
| \begin{tabular}[alignment]{columns} |
| rows |
| \end{tabular} |
The italics show where you have to put code to create your table.
alignment – put either b or t, or omit this completely. This determines how your table is vertically positioned with the text around it. This entry is not too important – experiment using different values (or omitting it) when you have a table in the midst of a document to get a better feel for it.
columns – this describes the number of columns and the alignment of each column. Put r for a right-justified column, c for a centered column, and l for a left-justified column. Put a | if you want a vertical line between columns. For example, the column declaration {||rr|cc|l} will produce a table that has 2 vertical lines on the left, then two columns that are right-justified, then a vertical line, then 2 columns that are centered, then another vertical line, then a left-justified column. There are more complicating things that you can do, and even more complicated things if you include the array packing in your document (check a good LaTeX book for more details), but for most tables, the options we’ve described here are sufficient.
rows – You can have as many rows as you like. For each row, you need an entry for each column. Each of these entries is separated by an &. Use \\ to indicate that your input for that row is finished. Hence, if your column declaration was {cccc}, a possible row entry could be
| 5&5&5&5\\ |
If you wish for one row to have fewer columns (i.e. one column takes up several of the usual table columns), use the command \multicolumn. In the example above, we had as our first row
\multicolumn{7}{c}{USAMTS Scores Round 1}\\
The first { } indicates how many regular columns this entry will take up. The second { } indicates whether the text in this entry is right (r), left (l) or center (c) justified. The final { } contains our entry. As with the regular column declaration, use | if you want a vertical line before or after the entry of \multicolumn.
In general, you can use \vline to introduce a vertical line anywhere in a table (try putting one between John and Nash in the example below and see what happens).
Finally, at the end of some of the rows in our example, we have the command \hline. This produces a horizontal line after the row it follows. If you want a horizontal line atop a table, use \hline right before the first row. If you only want a horizontal line under a portion of the row, use \cline{start column-end column} as indicated in the example below:
\begin{tabular}[t]{|l|l|cccc|c|}\hline
\multicolumn{7}{|c|}{USAMTS Final Scores by Round}\\\hline
Medal&Name&\#1&\#2&\#3&\#4&Total\\\hline\hline
&Richard Feynman&25&25&25&25&100\\\cline{3-7}
Gold&Albert Einstein&25&25&25&25&100\\\cline{3-7}
&Marie Curie&25&24&24&25&98\\\hline
Silver&John Nash&20&20&25&24&89\\\hline
&Jane Doe&23&\multicolumn{2}{c}{None}&25&48\\\cline{3-7}
None&John Doe&\multicolumn{2}{c}{None}&25&20&45\\\cline{3-7}
&Lazy Person&5&\multicolumn{3}{c|}{None}&5\\\hline
\end{tabular}
Note how we made a double horizontal line after the table headings.
Finally, sometimes you’ll want to create a table that consists solely of items in math mode. For such a table, use the array environment. The array environment works exactly like tabular, except that all its entries are rendered in math mode:
\[ \begin{array}[b]{ccc}
x&y&z \\
y&x&z \\
1&2&3
\end{array} \]
Change both array declarations to tabular and delete the \[ and \] and see what happens. You can do a number of other things with the array and tabular environments, but the above should cover most of what you’ll want to do with them.
If you build a table in which some entries are text that will take up multiple lines, you’ll probably want to learn about boxes (below).
Boxes
To create boxes of text that behave differently from the rest of the text, we can use
\makebox[width][pos]{text}
The width sets the width the of the box. The pos sets the positioning of the text – either r (right justified text), l (left justified), or s (stretched to fill the box). If the pos parameter is left out, as in \makebox[1in]{centerme}, the text is centered. The text is placed in the box. If you want to draw a box around the text, use \framebox just as you would use \makebox.
\mbox{text} and \fbox{text} are quick versions of \makebox and \framebox, which create a box to fit the size of the text.
Try the following example to see some of these features:
I can create basic boxes for text \makebox[2in]{like this}. Notice
that there's a 2in wide space with `like this' in the middle of it.
If I want to put a box around the text, I can use a frame box. The
result looks \framebox[2in]{like this}.
I can also justify the text to the right within a box
\makebox[1.5in][r]{like so} or \framebox[2.5in][l]{like so}.
We can also use quick versions of these. We can just \mbox{do this}
or \fbox{this} to create a quick box that's exactly the size of what we put in it.
In some of the boxes above, you’ll see the limitations of these boxes. In particular, LaTeX won’t do a line break in the middle of a box, so your box might run off the right side of the page. For boxes that span many lines, we can use
\parbox[pos]{width}{text}
The pos is used to align the box with the text outside the box – set it to either b or t to align the bottom or top edge of box with the current baseline (try both and see what happens). You can also just omit this parameter and the box is centered vertically on the current line of text. Look at the following example to see how pos affects the box.
The parameter width sets the width of the box and the text is what is in the box.
Here’s an example:
\parbox[b]{2in}{I like using parbox to create funny little boxes of
text all over my page. This one has its bottom edge aligned with the
current line}
CURRENT LINE.
\parbox[t]{2in}{The top of my text is aligned with that current
line.}
\parbox{1.5in}{I'm just centered on the current line.} CURRENT LINE
\parbox{2.5in}{You probably got a few Overfull or Underfull warnings
when you typeset this. Sometimes narrow boxes will do that; if you're
happy with the output, don't sweat it.}
\parbox is very useful with tables, as it can be used to include multirow input on a single row. Without it, long input in a cell will sometimes cause a table to stretch out into the margins. Note how boxes are used to create the tables below, and notice the effect of not using the \parbox in the second table.
\begin{tabular}[t]{ccccc}
\makebox[2.0in]{}&&\makebox[1.5in]{}&&
\makebox[1.5in]{}\\\cline{1-1}\cline{3-3}\cline{5-5}
Student Name&&\parbox[t][0.2in]{1.5in}{Art of Problem Solving
Community Username}&&Student Email\\
\end{tabular}
\begin{tabular}[t]{ccccc}
\makebox[2.0in]{}&&\makebox[1.5in]{}&&
\makebox[1.5in]{}\\\cline{1-1}\cline{3-3}\cline{5-5}
Student Name&&Art of Problem Solving Community Username&&Student
Email\\
\end{tabular}
Lists
There are three simple versions of lists you can make with LaTeX.
To separate list items with bullet points, use itemize:
\noindent Here's my list:
\begin{itemize}
\item Item 1.
\item Item 2.
\item Item 3.
\end{itemize}
The \noindent is there to exhibit how the list is indented.
If you want a numbered list, use enumerate:
\noindent Here's my list:
\begin{enumerate}
\item Item 1.
\item Item 2.
\item Item 3.
\end{enumerate}
If you want to make your own headings for each item, use description:
\noindent Here's my list:
\begin{description}
\item[Heading 1.] Item 1.
\item[Heading 2.] Item 2.
\item[Heading 3.] Item 3.
\end{description}
Lists can be nested up to four levels. Here’s one nested 2 levels deep:
\noindent Here's my list:
\begin{itemize}
\item Item 1.
\begin{itemize}
\item List 2, Item 1
\item List 2, Item 2
\end{itemize}
\item Item 2.
\item Item 3.
\end{itemize}
You can use the command \renewcommand to change the labels for items. The labels for itemize have the names \labelitemi, \labelitemii, \labelitemiii, \labelitemiv (one for each level of nesting, with \labelitemi being the first):
\renewcommand{\labelitemi}{$\heartsuit$}
\renewcommand{\labelitemii}{$\spadesuit$}
\noindent Here's my list:
\begin{itemize}
\item Item 1.
\begin{itemize}
\item List 2, Item 1
\item List 2, Item 2
\end{itemize}
\item Item 2.
\item Item 3.
\end{itemize}
As expected, you can also get pretty fancy with enumerate:
\renewcommand{\labelenumi}{\Roman{enumi}.}
\renewcommand{\labelenumii}{\Roman{enumi}. \alph{enumii}}
\noindent Here's my list:
\begin{enumerate}
\item Item 1.
\begin{enumerate}
\item List 2, Item 1
\item List 2, Item 2
\end{enumerate}
\item Item 2.
\item Item 3.
\end{enumerate}
The \labelenumi and \labelenumii are our labels for the two levels of nesting (as with itemize, there are four levels possible). The
\renewcommand{\labelenumi}{\Roman{enumi}.}
causes each item in the primary list to have a capitalized Roman numeral followed by a period. The enumi is the counter for the main items. The
\renewcommand{\labelenumii}{\Roman{enumi}. \alph{enumii}}
causes each item in the secondary list to have a capitalized Roman numeral of the primary list followed by a period, then a lowercase letter corresponding to the item of the secondary list. As you probably guessed, enumii is the counter that tells us which number of the secondary list we’re on.
In addition to \Roman and \alph, you can use \arabic, \roman, and \Alph to format counter numbers such as enumii to numbers, lowercase Roman numerals, or uppercase letters. Try them in place of \Roman and \alph above.
Cross referencing
Sometimes you’ll want to refer to past equations, or even to past pages. Rather than having to manually number equations then change your text if the equation labels change, or having to manually put in page numbers which might change later, you can use the referencing built into LaTeX. To tell LaTeX to note where something is so you can refer to it later, use
\label{label name}
To refer to it later, use
\ref{label name}
or
\pageref{label name}
The latter gives you the page number, and the former gives you the Theorem number or equation number or whatever other special number might be attached to the item labeled with label name. For referencing equations, it is preferable to use \eqref instead of \ref so that the equation is displayed in parentheses.
When using \label, you’ll have to compile your LaTeX code twice. The first compilation will produce a .aux file that contains all the label information based on your \label commands. The second compilation reads and uses that information to fill out your \ref and \pageref commands.
Here’s an example with several uses of \label and \ref.
\section{Some Sums}\label{sec:formulas}
Here are a few sums I know.
\begin{eqnarray}
1+2+3+\cdots+n&=&\frac{n(n+1)}{2}\label{eqn:linear}\\
1^2+2^2+3^2+\cdots+n^2&=& \frac{n(n+1)(2n+1)}{6}\label{eqn:squares}\\
1^3+2^3+3^3+\cdots+n^3&=& \frac{n^2(n+1)^2}{4}\label{eqn:cubes}
\end{eqnarray}
I can find the sum of the first 10 squares easily with formula~\eqref{eqn:squares} above.
\pagebreak
\section{A Cool Relationship}
Take a look at formulas~\eqref{eqn:linear} and~\eqref{eqn:cubes} on
page~\pageref{eqn:linear} of section~\ref{sec:formulas}. Notice that the
right side of~\eqref{eqn:cubes} is the square of the right side
of~\eqref{eqn:linear}.
Remember, you have to compile the previous example twice in order the see the references. After the first compilation, LaTeX will warn you about “undefined references”, and you’ll see “??” in your document where the references should be. Compile it a second time, and the references will work.
The \label commands in the \begin{eqnarray} allow us to use \ref later to refer back to the equations. The \label{sec:formulas} allows us to use \ref to refer to the section. Note that we can use the same label for the \pageref as we use for the \ref. Also, note the ~ before each of the references; these aren’t required, but are used for style concerns. They prevent a line break right before the reference. Generally, it’s poor style to start off lines with numbers or references; these ~ symbols prevent that if possible.
See Also
LaTeX:Symbols
Finding Other Symbols
Here are some external resources for finding less commonly used symbols:
- Detexify is an app which allows you to draw the symbol you’d like and shows you the
code for it! - MathJax (what allows us to use
on the web, (technically an AJAX library simulating it.)) maintains a list of supported commands. - The Comprehensive LaTeX Symbol List.
Operators
| Symbol | Command | Symbol | Command | Symbol | Command |
|---|---|---|---|---|---|
| \pm | \mp | \times | |||
| \div | \cdot | \ast | |||
| \star | \dagger | \ddagger | |||
| \amalg | \cap | \cup | |||
| \uplus | \sqcap | \sqcup | |||
| \vee | \wedge | \oplus | |||
| \ominus | \otimes | \circ | |||
| \bullet | \diamond | \lhd | |||
| \rhd | \unlhd | \unrhd | |||
| \oslash | \odot | \bigcirc | |||
| \triangleleft | \Diamond | \bigtriangleup | |||
| \bigtriangledown | \Box | \triangleright | |||
| \setminus | \wr | \sqrt{x} | |||
| x^{\circ} | \triangledown | \sqrt[n]{x} | |||
| a^x | a^{xyz} | a_x |
Relations
| Symbol | Command | Symbol | Command | Symbol | Command |
|---|---|---|---|---|---|
| \le | \ge | \neq | |||
| \sim | \ll | \gg | |||
| \doteq | \simeq | \subset | |||
| \supset | \approx | \asymp | |||
| \subseteq | \supseteq | \cong | |||
| \smile | \sqsubset | \sqsupset | |||
| \equiv | \frown | \sqsubseteq | |||
| \sqsupseteq | \propto | \bowtie | |||
| \in | \ni | \prec | |||
| \succ | \vdash | \dashv | |||
| \preceq | \succeq | \models | |||
| \perp | \parallel | ||||
| \mid | \bumpeq |
Negations of many of these relations can be formed by just putting \not before the symbol, or by slipping an n between the \ and the word. Here are a couple examples, plus many other negations; it works for many of the many others as well.
| Symbol | Command | Symbol | Command | Symbol | Command |
|---|---|---|---|---|---|
| \nmid | \nleq | \ngeq | |||
| \nsim | \ncong | \nparallel | |||
| \not< | \not> | \not= or \neq | |||
| \not\le | \not\ge | \not\sim | |||
| \not\approx | \not\cong | \not\equiv | |||
| \not\parallel | \nless | \ngtr | |||
| \lneq | \gneq | \lnsim | |||
| \lneqq | \gneqq |
To use other relations not listed here, such as =, >, and <, in LaTeX, you must use the symbols on your keyboard, they are not available in
.
Greek Letters
| Symbol | Command | Symbol | Command | Symbol | Command | Symbol | Command |
|---|---|---|---|---|---|---|---|
| \alpha | \beta | \gamma | \delta | ||||
| \epsilon | \varepsilon | \zeta | \eta | ||||
| \theta | \vartheta | \iota | \kappa | ||||
| \lambda | \mu | \nu | \xi | ||||
| \pi | \varpi | \rho | \varrho | ||||
| \sigma | \varsigma | \tau | \upsilon | ||||
| \phi | \varphi | \chi | \psi | ||||
| \omega |
| Symbol | Command | Symbol | Command | Symbol | Command | Symbol | Command |
|---|---|---|---|---|---|---|---|
| \Gamma | \Delta | \Theta | \Lambda | ||||
| \Xi | \Pi | \Sigma | \Upsilon | ||||
| \Phi | \Psi | \Omega |
Arrows
| Symbol | Command | Symbol | Command |
|---|---|---|---|
| \gets | \to | ||
| \leftarrow | \Leftarrow | ||
| \rightarrow | \Rightarrow | ||
| \leftrightarrow | \Leftrightarrow | ||
| \mapsto | \hookleftarrow | ||
| \leftharpoonup | \leftharpoondown | ||
| \rightleftharpoons | \longleftarrow | ||
| \Longleftarrow | \longrightarrow | ||
| \Longrightarrow | \longleftrightarrow | ||
| \Longleftrightarrow | \longmapsto | ||
| \hookrightarrow | \rightharpoonup | ||
| \rightharpoondown | \leadsto | ||
| \uparrow | \Uparrow | ||
| \downarrow | \Downarrow | ||
| \updownarrow | \Updownarrow | ||
| \nearrow | \searrow | ||
| \swarrow | \nwarrow |
(For those of you who hate typing long strings of letters, \iff and \implies can be used in place of \Longleftrightarrow and \Longrightarrow respectively.)
Dots
| Symbol | Command | Symbol | Command | |
|---|---|---|---|---|
| \cdot | \vdots | |||
| \dots | \ddots | |||
| \cdots | \iddots |
Accents
| Symbol | Command | Symbol | Command | Symbol | Command |
|---|---|---|---|---|---|
| \hat{x} | \check{x} | \dot{x} | |||
| \breve{x} | \acute{x} | \ddot{x} | |||
| \grave{x} | \tilde{x} | \mathring{x} | |||
| \bar{x} | \vec{x} |
When applying accents to i and j, you can use \imath and \jmath to keep the dots from interfering with the accents:
| Symbol | Command | Symbol | Command |
|---|---|---|---|
| \vec{\jmath} | \tilde{\imath} |
\tilde and \hat have wide versions that allow you to accent an expression:
| Symbol | Command | Symbol | Command |
|---|---|---|---|
| \widehat{7+x} | \widetilde{abc} |
Others
Command Symbols
Some symbols are used in commands so they need to be treated in a special way.
| Symbol | Command | Symbol | Command | Symbol | Command | Symbol | Command |
|---|---|---|---|---|---|---|---|
| \textdollar or \$ | \& | \% | \# | ||||
| \_ | \{ | \} | \backslash |
(Warning: Using $ for
will result in
. This is a bug as far as we know. Depending on the version of
this is not always a problem.)
European Language Symbols
| Symbol | Command | Symbol | Command | Symbol | Command | Symbol | Command |
|---|---|---|---|---|---|---|---|
| {\oe} | {\ae} | {\o} | |||||
| {\OE} | {\AE} | {\AA} | {\O} | ||||
| {\l} | {\ss} | !` | |||||
| {\L} | {\SS} |
Bracketing Symbols
In mathematics, sometimes we need to enclose expressions in brackets or braces or parentheses. Some of these work just as you’d imagine in LaTeX; type ( and ) for parentheses, [ and ] for brackets, and | and | for absolute value. However, other symbols have special commands:
| Symbol | Command | Symbol | Command | Symbol | Command |
|---|---|---|---|---|---|
| \{ | \} | \| | |||
| \backslash | \lfloor | \rfloor | |||
| \lceil | \rceil | \langle | |||
| \rangle |
You might notice that if you use any of these to typeset an expression that is vertically large, like
- (\frac{a}{x} )^2
the parentheses don’t come out the right size:
If we put \left and \right before the relevant parentheses, we get a prettier expression:
- \left(\frac{a}{x} \right)^2
gives
For systems of equations or piecewise functions, use the cases environment:
f(x) = \begin{cases} x^2 & x \ge 0 \\ x & x < 0 \end{cases}
which gives

In addition to the \left and \right commands, when doing floor or ceiling functions with fractions, using
\left\lceil\frac{x}{y}\right\rceil
and \left\lfloor\frac{x}{y}\right\rfloor
give both ![]()
And, if you type this
\underbrace{a_0+a_1+a_2+\cdots+a_n}_{x}
Gives
![]()
Or
\overbrace{a_0+a_1+a_2+\cdots+a_n}^{x}
Gives
![]()
\left and \right can also be used to resize the following symbols:
| Symbol | Command | Symbol | Command | Symbol | Command |
|---|---|---|---|---|---|
| \uparrow | \downarrow | \updownarrow | |||
| \Uparrow | \Downarrow | \Updownarrow |
Multi-Size Symbols
Some symbols render differently in inline math mode and in display mode. Display mode occurs when you use \[…\] or $$…$$, or environments like \begin{equation}…\end{equation}, \begin{align}…\end{align}. Read more in the commands section of the guide about how symbols which take arguments above and below the symbols, such as a summation symbol, behave in the two modes.
In each of the following, the two images show the symbol in display mode, then in inline mode.
| Symbol | Command | Symbol | Command | Symbol | Command |
|---|---|---|---|---|---|
| \sum | \int | \oint | |||
| \prod | \coprod | \bigcap | |||
| \bigcup | \bigsqcup | \bigvee | |||
| \bigwedge | \bigodot | \bigotimes | |||
| \bigoplus | \biguplus |
See Also
LaTeX:Commands
This page introduces various useful commands for rendering math in LaTeX, as well as instructions for building your own commands.
Contents
Subscripts and Superscripts
Subscripts and superscripts (such as exponents) can be made using the underscore _ and carat ^ symbols respectively.
| Symbol | Command | Symbol | Command |
|---|---|---|---|
| 2^2 | a_i | ||
| 2^{23} | n_{i-1} | ||
| a^{i+1}_3 | x^{3^2} | ||
| 2^{a_i} | 2^a_i |
Notice that we can apply both a subscript and a superscript at the same time. For subscripts or superscripts with more than one character, you must surround with curly braces. For example, x^10 produces
, while x^{10} produces
.
Math Commands
Here are some commonly used math commands in LaTeX:
Fractions
| Symbol | Command |
|---|---|
| \frac{1}{2} or \frac12 | |
| \frac{2}{x+2} | |
| \frac{1+\frac{1}{x}}{3x + 2} |
Notice that with fractions with a 1-digit numerator and a 1-digit denominator, we can simply group the numerator and the denominator together as one number. However, for fractions with either a numerator or a denominator that requires more than one character, you need to surround everything in curly brackets.
Use \cfrac for continued fractions.
| Expression | Command |
|---|---|
![]() |
\cfrac{2}{1+\cfrac{2}{1+\cfrac{2}{1+\cfrac{2}{1}}}} |
Radicals
| Symbol | Command |
|---|---|
| \sqrt{3} | |
| \sqrt{x+y} | |
| \sqrt{x+\frac{1}{2}} | |
| \sqrt[3]{3} | |
| \sqrt[n]{x} |
Sums, Products, Limits and Logarithms
Use the commands \sum, \prod, \lim, and \log respectively. To denote lower and upper bounds, or the base of the logarithm, use _ and ^ in the same way they are used for subscripts and superscripts. (Lower and upper bounds for integrals work the same way, as you’ll see in the calculus section)
| Symbol | Command |
|---|---|
| \sum_{i=1}^{\infty}\frac{1}{i} | |
| \prod_{n=1}^5\frac{n}{n-1} | |
| \lim_{x\to\infty}\frac{1}{x} | |
| \lim\limits_{x\to\infty}\frac{1}{x} | |
| \log_n n^2 |
Some of these are prettier in display mode:
| Symbol | Command |
|---|---|
| \sum_{i=1}^{\infty}\frac{1}{i} | |
![]() |
\prod_{n=1}^5\frac{n}{n-1} |
| \lim_{x\to\infty}\frac{1}{x} |
Note that we can use sums, products, and logarithms without _ or ^ modifiers.
| Symbol | Command |
|---|---|
| \sum\frac{1}{i} | |
| \frac{n}{n-1} | |
| \log n^2 | |
| \ln e |
Mods
| Symbol | Command |
|---|---|
| 9\equiv 3 \bmod{6} | |
| 9\equiv 3 \pmod{6} | |
| 9\equiv 3 \mod{6} | |
| 9\equiv 3 \pod{6} |
Combinations
| Symbol | Command |
|---|---|
| \binom{1}{1} | |
| \binom{n-1}{r-1} |
These often look better in display mode:
| Symbol | Command |
|---|---|
| \dbinom{9}{3} | |
![]() |
\dbinom{n-1}{r-1} |
Trigonometric Functions
Most of these are just the abbreviation of the trigonometric function with simply a backslash added before the abbreviation.
| Symbol | Command | Symbol | Command | Symbol | Command |
|---|---|---|---|---|---|
| \cos | \sin | \tan | |||
| \sec | \csc | \cot | |||
| \arccos | \arcsin | \arctan | |||
| \cosh | \sinh | \tanh | |||
| \coth |
Here are a couple examples:
| Symbol | Command |
|---|---|
| \cos^2 x +\sin^2 x = 1 | |
| \cos 90^\circ = 0 |
Calculus
Below are examples of calculus expressions rendered in LaTeX. Most of these commands have been introduced before. Notice how definite integrals are rendered (and the difference between regular math and display mode for definite integrals). The \, in the integrals makes a small space before the dx.
| Symbol | Command |
|---|---|
| \frac{d}{dx}\left(x^2\right) = 2x | |
| \int 2x\,dx = x^2+C | |
| \int^5_1 2x\,dx = 24 | |
| \int^5_1 2x\,dx = 24 | |
| \frac{\partial^2U}{\partial x^2} + \frac{\partial^2U}{\partial y^2} | |
| \frac{1}{4\pi}\oint_\Sigma\frac{1}{r}\frac{\partial U}{\partial n} ds |
Overline and Underline
| Symbol | Command |
|---|---|
| \overline{a+bi} | |
| \underline{747} |
LaTeX
Other Functions
| Symbol | Command | Symbol | Command | Symbol | Command |
|---|---|---|---|---|---|
| \arg | \deg | \det | |||
| \dim | \exp | \gcd | |||
| \hom | \inf | \ker | |||
| \lg | \liminf | \limsup | |||
| \max | \min | \Pr | |||
| \sup |
Some of these commands take subscripts in the same way sums, products, and logarithms do. Some render differently in display mode and regular math mode.
| Symbol | Command | Symbol | Command | Symbol | Command |
|---|---|---|---|---|---|
| \dim_x | \gcd_x | \inf_x | |||
| \liminf_x | \limsup_x | \max_x | |||
| \min_x | \Pr_x | \sup_x |
Matrices
We can build an array or matrix with the \begin{array}…\end{array} commands, and use \left and \right to properly size the delimiters around the matrix:
The characteristic polynomial $f(\lambda)$ of the
$3 \times 3$ matrix
\[
\left(
\begin{array}{ccc}
a & b & c <br />d & e & f <br />g & h & i \end{array}
\right)\]
is given by the equation
\[ f(\lambda)
= \left|
\begin{array}{ccc}
\lambda - a & -b & -c <br />-d & \lambda - e & -f <br />-g & -h & \lambda - i \end{array}
\right|.\]
More simply, we can use the shortcut matrix environments in the amsmath package:
The characteristic polynomial $f(\lambda)$ of the
$3 \times 3$ matrix
\[
\begin{pmatrix}
a & b & c <br />d & e & f <br />g & h & i
\end{pmatrix} \]
is given by the equation
\[ f(\lambda)
= \begin{vmatrix}
\lambda - a & -b & -c <br />-d & \lambda - e & -f <br />-g & -h & \lambda - i
\end{vmatrix}.\]
You can read more about how the array environment works here (it works the same as tabular).
We can also use this environment to typeset any mathematics that calls for multiple columns, such as piecewise-defined functions like this one:
\[ f(x) = \left\{ \begin{array}{ll}
x+7 & \mbox{if $5< x$};<br />x^2-3 & \mbox{if $-3 \le x \le 5$};<br />-x & \mbox{if $x < -3$}.\end{array} \right. \]
But it would be better to use the cases environment and \text command that the amsmath package provides:
\[
f(x) = \begin{cases}
x+7 & \text{if $5< x$}; <br />x^2-3 & \text{if $-3 \le x \le 5$};<br />-x & \text{if $x < -3$}.
\end{cases}
\]
Text Styles in Math Mode
You can render letters in various styles in math mode. Below are examples; you should be able to use these with any letters. The \mathbb requires the amsfonts package to be included in your document’s preamble. Do not try to do \mathbb{year}. You’ll get
, and that looks nothing like it!
If you’re persistent, you can dig a few more out of this document.
If you want to drop a little bit of text in the middle of math mode, you can use the \text command. The \text command is most useful in $$…$$ or $…$ mode, where breaking up the math mode would force the output on to a new line entirely. So
$$n^2 + 5 = 30\text{ so we have }n=\pm5$$
gives
How to Build Your Own Commands
The command \newcommand is used to create your own commands. We’ll start with an example:
\documentclass[11pt]{article}
\usepackage{amsmath}
\pdfpagewidth 8.5in
\pdfpageheight 11in
\newcommand{\reci}[1]{\frac{1}{#1}}
\newcommand{\hypot}[2]{\sqrt{#1^2+#2^2}}
\newcommand{\cbrt}[1]{\sqrt[3]{#1}}
\begin{document}
The reciprocal of 2 is $\reci{2}$.
The hypotenuse has length $\hypot{3}{4}$.
I'm sick of writing `$\backslash$sqrt[3]{2}$' all the time, just to get $\cbrt{2}$.
\end{document}
The \newcommand declarations are in the preamble. Each is of the form
\newcommand{name of new command}[number of arguments]{definition}
The name of the new command, which must begin with a \, is the name you’ll use in the document to use the command. The number of arguments is how many inputs will be sent to the command. The definition is just normal LaTeX code, with #1, #2, #3, etc., placed where you want the inputs to go when the new command is called.
New commands can be used for all sorts of purposes, not just for making math commands you’ll use a lot easier to call. For example, try this:
\documentclass[11pt]{article}
\usepackage{amsmath}
\pdfpagewidth 8.5in
\pdfpageheight 11in
\newcounter{prob_num}
\setcounter{prob_num}{1}
\newcommand{\prob}[5]{\bigskip \bigskip\arabic{prob_num}.\stepcounter{prob_num} #1
\par\nopagebreak[4]\medskip A.\ #2\hfill B.\ #3\hfill
C.\ #4\hfill D.\ #5\hfill E.\ NOTA}
\begin{document}
\prob{What is $2+2$?}{4}{5}{6}{7}
\prob{What is $\sqrt{100}$?}{81}{10}{9}{1}
\prob{Evaluate $\sum_{n=1}^\infty \frac{1}{n^2}$.}
{$\frac{1}{e}$} {$\frac{2}{\pi}$}
{$\frac{\pi^3}{8}$} {$\frac{\pi^2}{6}$}
\end{document}
In the example above, we create a new command called \prob. Each time we call \prob, we supply 5 arguments, one for the question and one for each of the multiple choices.
In the preamble and the definition of \prob, you’ll see a few new LaTeX commands:
\newcounter{prob_num} creates a counter variable called prob_num
\setcounter{prob_num}{1} setsprob_num to equal 1.
In the definition of \prob, the \bigskip and \medskip commands create vertical space.
\arabic{prob_num} prints out the current value of the counter prob_num as an arabic numeral.
\stepcounter{prob_num} increments the counter prob_num by 1.
\nopagebreak[4] tells LaTeX not to break the page between the problem and the choices unless it really, really, really has to.
The \hfill commands put roughly equal space between the choices.
Once you build a body of custom commands that you will be using in many LaTeX documents, you should learn about creating your own package so you don’t have to copy all your custom commands from document to document.
See Also
LaTeX:Packages
General Information
In the Commands pages, we discussed building your own commands. Fortunately, many, many people have built their own commands already and made them available to
users in packages. Packages allow us to use extra commands without having to include tons and tons of code in the preamble of a document. Instead, we just add the line
\usepackage{package name}
and we have access to all the commands, symbols, environments, etc., that are in the package.
Common Packages
This section will cover the packages released by the American Mathematical Society, as well as xypic and fancyhdr.
AMS Math packages
The American Mathematical Society has produced several packages for use with LaTeX. These packages allow much of the mathematical formatting we have introduced on these pages, as well as much, much more. The packages are included with the installation of MiKTeX. In order to be able to use the commands and symbols in these packages, they must be included in each document in which you require them. To include the packages, include the following in the preamble of your document:
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{amsthm}
Most of the basic parts of these packages which will be useful to you are covered somewhere on these pages. If you’d like to read about more options these packages provide, read the documentation for them at the following links:
Note that including these packages can slow down the compilation of your document, so you may not want to include them unless you really need them.
xypic
The xypic package allows you to make tricky diagrams such as ones which include various items connected by arrows or lines. Take a look at the xypic home page to see what xypic can do. The package comes with MiKTeX. To use it, include the following in your preamble:
\usepackage{xy}
fancyhdr
The fancyhdr package provides an easy way to place text on the top and/or bottom of every page. The fancyhdr package does not come with MiKTeX, but assuming that you installed MiKTeX using our recommendations, it should prompt you to install it the first time you try to use it. You include it in your source file in the usual way:
\usepackage{fancyhdr}
The fancyhdr user’s guide has detailed instructions on how to use the package. Here’s an example that illustrates most of the basics you’ll need:
\documentclass[11pt]{article}
\usepackage{fancyhdr}
\pdfpagewidth 8.5in
\pdfpageheight 11in
\pagestyle{fancy}
\headheight 35pt
\rhead{Right top}
\chead{Middle top}
\lhead{Left top}
\rfoot{Right bottom}
\cfoot{\thepage}
\lfoot{Left bottom}
\begin{document}
First page
\pagebreak
Second page
\pagebreak
Third page
\end{document}
\pagestyle{fancy} tells LaTeX that you’ll be using the fancy style which allows you to use the header and footer commands.
The \headheight command makes space at the top of the page for the header. Try compiling without it and look through the errors to see what happens.
The \rhead, \chead, \lhead, \rfoot, \cfoot, and \lfoot commands create headers and footers in the obvious places (you’ll see them in the output when you create the example).
The \pagebreak forces new pages so you can see how the headers and footers appear on every page.
If introducing headers and footers makes you want to resize the dimensions of the text portion of your page, consult the Layout article. Note that if you do resize the dimension of the text portion of your page, you should execute those commands before executing the header and footer commands.
Finding Other Packages
Many, many packages have been created for LaTeX. If there’s something unusual that you want to do, there’s a good chance that there’s already a package out there that does it.
The TeX Catalogue Online has a very comprehensive list of all the LaTeX packages out there.
Making Your Own
Making your own high-powered, fancy package like the ones created by the AMS requires a great deal of work and detailed knowledge about LaTeX. However, making your own set of commands or a basic preamble that you can include in any of your LaTeX documents is simple. Just follow these steps:
- Create a file for the information you’d like to include in multiple files and name it something with a .sty extension, such as mylay.sty or mycommands.sty.
- Put in the .sty file all the commands you’d like to use in multiple source files and save the file.
- You have two options for where to save the file to be able to include it in LaTeX source files:
- (Recommended) Install the .sty file with MiKTeX as follows:
- Navigate to the folder where LaTeX packages get installed. If you accepted the default location when you installed MiKTeX, this folder is probably C:\Program Files\MiKTeX 2.9\tex\latex. (If you have an older MiKTeX installation, this folder is probably C:\texmf\tex\latex.)
- Create a new folder inside this folder and call it whatever you want.
- Save your .sty file into this folder.
- Open the MiKTeX Options program. (You can do this by clicking Start, then All Programs, then MiKTeX, then MiKTeX Options. It might also be labelled ‘Settings.’)
- Click ‘Refresh Now’ or ‘Refresh FNDB’ on the window that opens. Now you should be able to use any information in that .sty file in any LaTeX source file.
- Simply put your .sty file in the same folder as your source files. Unfortunately, this means if you have LaTeX source files in other folders, you’ll have to carry copies of your .sty files to those folders, too. The real problem with this approach comes when you want to change one of these files – if you use this method rather than method 1, you have to change it all those copies (or have many different versions of the same file, which is dangerous).
- (Recommended) Install the .sty file with MiKTeX as follows:
- Now you are able to include your package in the usual way: \usepackage{your file name}, where you don’t include the ‘.sty’ in the file name in the \usepackage statement.
- You can now call any command you have created in your .sty file.
We recommend that you use different .sty files for all your new commands and for your layout. You may end up having many different layouts, but you’ll probably want your custom commands available to all your documents. Separating the commands from the layout preamble makes it easy to include those commands with many different layouts. Do note that every time you create a new .sty, you must go through the steps above to use it (i.e. if you install it with MiKTeX, you have to put it in the folder mentioned above, then use MiKTeX Options as described above to complete the installation). You don’t have to reinstall every time you add a command to an existing installed .sty file.
Here’s a quick example. In mylay.sty, include the following:
%packages
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{amsthm}
\usepackage[pdftex]{graphics}
\usepackage{mycommands}
%layout
\pdfpagewidth 8.5in
\pdfpageheight 11in
\topmargin 0in
\headheight 0in
\headsep 0in
\textheight 9in
\textwidth 6.5in
\oddsidemargin 0in
\evensidemargin 0in
In the file mycommands.sty, put the following:
%commands
\newcommand\rttw{\sqrt{2}}
\newcommand\rtth{\sqrt{3}}
\newcommand\rtfi{\sqrt{5}}
\newcommand\rtsi{\sqrt{6}}
\newcommand\rtse{\sqrt{7}}
Install these two files using either method from above.
In a LaTeX source file, put the following:
\documentclass[11pt]{article}
\usepackage{mylay}
\begin{document}
This is my first document with my own packages.
I can use my new commands, watch: $\rttw + \rtse$.
\end{document}
Notice that we only have to include mylay with \usepackage; mycommands is included in the file mylay.sty (look at the mylay.sty file contents and you’ll see the line \usepackage{mycommands}).
If you installed everything properly, your source file should compile just fine now.
Command Basics
Of course, the previous section was very basic. You still need to know, for example, how to include variables. To begin with, the syntax is
\newcommand\<commandname>[number of variables]{what it does}
So, for example, you want an addition package. Here is a basic addition function.
\newcommand\addition[2]{#1 + #2}
Here, #1 and #2 refer to the variables in order. If you had 3 variables, and you wanted to make a division package, you would have
\newcommand\division[3]{#1 \div #2 = #3}
And in a document:
\documentclass[11pt]{article}
\usepackage{division}
\begin{document}
\begin{math}\division{6}{3}{2}\end{math}
\end{document}
Would result in
.
See Also
LaTeX:Help
| LaTeX |
| About – Getting Started – Diagrams – Symbols – Downloads – Basics – Math – Examples – Pictures – Layout – Commands – Packages – Help |
This article will answer common LaTeX questions and provide solutions to common LaTeX problems. For more active help, please consult the AoPS message board.
Frequently Asked Questions—–
Here are commonly asked LaTeX questions and possible answers.
Contents
- 1 Installation
- 2 Coding Questions
- 3 LaTeX Errors with Error Messages
- 3.1 Extra alignment tab has been changed to \cr
- 3.2 LaTeX Error: \begin{something} on input line line number ended by \end{document}
- 3.3 LaTeX Warning: Reference … undefined on ….
- 3.4 LaTeX Warning: There were undefined references.
- 3.5 Missing $ inserted
- 3.6 Paragraph ended before \end was complete.
- 3.7 Runaway argument?
- 3.8 Undefined control sequence.
- 4 Other LaTeX Errors
- 4.1 Forget the error messages, when I try to compile, it starts, but then just hangs. Nothing happens, and I can’t do anything. Then when I try to save the file, it won’t let me save it with the same name.
- 4.2 I’m trying to use fancyhdr to make headers and footers, but they don’t align with the rest of my page. The rest of the page extends beyond the header line to the right or left.
- 4.3 I’m using referencing, but the page numbers or equation numbers aren’t coming out right.
- 5 Helpful Links
- 6 See Also
Installation
I’m using a Macintosh. What do I do?
Some Mac users use TeXShop, OzTeX or Textures (we wrote the Art of Problem Solving textbooks in 1993 using Textures). The TeXShop is free. (Special thanks to Community member nr1337 for letting us know about TeXShop.)
How can I install LaTeX on my website like you have on the TeXer and your message board?
We used LatexRender, which can be found here. You will need to be able to install LaTeX and imagemagick on your server to use this. That site also has a TeX Converter that doesn’t require LaTeX and Imagemagick.
I’ve upgraded MiKTeX and now TeXnicCenter won’t work
You need to tell TeXnicCenter where MiKTeX has moved to. If you are using MiKTeX 2.7 it will probably be C:\Program Files\MiKTeX 2.7\miktex\bin (this folder contains lots of exe files including latex.exe). You do this by clicking on Build, Define Output Profiles and clicking on the Wizard button in the bottom left corner. This video will show you what to do.
Coding Questions
My pages start too low!
Use \topmargin in the preamble as described here to set the top margin smaller.
LaTeX Errors with Error Messages
When you use TeXnicCenter, a separate window contains output from LaTeX concerning various compiling information, including errors. Much of this text can be ignored. However, if you see a symbol that looks like this:
Then you have a serious problem.
If you have a whole string of these error messages, scroll back until you hit the first one. Often one error will cause many others, so you should fix them starting with your first problem. Also, the output from the compiler will give you a line number to help you find the error in your code. In TeXnicCenter, the line number you are on is given at the bottom of the TeXnicCenter window on the right.
Here are some common error messages, as well as other possible problems, and a little information about what the problem usually is. Most of these error messages will appear right after the red symbol. Occasionally, they will appear 1-2 lines before it. A few of these are just warnings – they generate yellow triangles in TeXnicCenter when compiling.
Extra alignment tab has been changed to \cr
You have too many &s in a row of a table or array or eqnarray.
LaTeX Error: \begin{something} on input line line number ended by \end{document}
You’ve left out the \end{whatever}. Perhaps you’re building a table or an eqnarray that has a bunch of lines and you’ve forgotten the \end statement.
LaTeX Warning: Reference … undefined on ….
You have references in your document. You need to compile again so LaTeX will be able to get the references right.
LaTeX Warning: There were undefined references.
You have references in your document. You need to compile again so LaTeX will be able to get the references right.
Missing $ inserted
You left out a $ or \[ or \] or $$ somewhere when you were writing text that needs to be in math mode.
Paragraph ended before \end was complete.
You are probably missing an } at the end of an \end{something} statement.
Runaway argument?
A runaway argument occurs when you leave off a }.
Undefined control sequence.
You’ve tried to call a command that doesn’t exist. Usually this is the result of a typo. Go to the line number mentioned in the error and look at all your commands. You’ll probably find something like \rfac where you wanted \frac.
Other LaTeX Errors
Here are some problems that might occur that don’t have error messages in the compiler. If your problem isn’t solved by this page, consider posting on the AoPS message board for help.
Forget the error messages, when I try to compile, it starts, but then just hangs. Nothing happens, and I can’t do anything. Then when I try to save the file, it won’t let me save it with the same name.
You probably have tried to use a package that you haven’t installed. For example, if you try \usepackage{fancyhdr} but haven’t installed the package, then you’ll hang TeXnicCenter so badly that you should just restart your computer (or, if you know how, kill the latex process you’ve started).
You probably have called \textwidth (or some other command dealing with page dimensions) after the \pagestyle command in your preamble. The \pagestyle and header and footer commands should come after all the page dimensions are set in the header.
I’m using referencing, but the page numbers or equation numbers aren’t coming out right.
You have references in your document. You need to compile again so LaTeX will be able to get the references right.
Helpful Links
- <url>index.php?f=123 Art of Problem Solving LaTeX forum</url> – ask your question to our knowledgable LaTeX community.
- <url>../LaTeX/AoPS_L_TeXer.php AoPS TeXeR</url> – the AoPS TeXer, which allows you to practice LaTeX typesetting and/or create pictures for your own website.
- TeX Users Group home page – a source for all matters TeX.
- TeX Catalogue Online – a comprehensive database of LaTeX packages.
- MiKTeX home page – a popular TeX engine for Windows-based computers.
- TeXnicCenter home page – a popular TeX editor for Windows-based computers.
- MetaPost home page – MetaPost is a programming language for creating graphic images for inclusion into LaTeX documents. It is not easy to install or use but the quality of the images produced by MetaPost is outstanding. (All of the Art of Problem Solving’s new Introductory textbooks used MetaPost to generate the diagrams.)
See Also



