LaTeX:Commands

Revision as of 18:36, 12 July 2007 by Chris_bayhill (talk | contribs) (Trigonometric Functions)
LaTeX
About - Getting Started - Diagrams - Symbols - Downloads - Basics - Math - Examples - Pictures - Layout - Commands - Packages - Help

This page introduces various useful commands for rendering math in LaTeX, as well as instructions for building your own commands.

Math Commands

Here are some commonly used math commands in LaTeX.

Exponents and Subscripts

Make exponents in LaTeX with ^ and subscripts with _ as shown in the examples below.

Symbol Command Symbol Command
$2^2$ 2^2 $a_i$ a_i
$2^{23}$ 2^{23} $n_{i-1}$ n_{i-1}
$a^{i+1}_3$ a^{i+1}_3 $x^{3^2}$ x^{3^2}
$2^{a_i}$ 2^{a_i} $2^a_i$ 2^a_i

Notice that we can apply both a subscript and an exponent at the same time, and that we can use {} to tell LaTeX what to apply a subscript or exponent to (compare the examples on the bottom row).

Finally, notice that we use {} for any exponent or subscript that is more than one character. You have to do so, or you'll end up with $2^234$ or $a^i+1_n-1$ when you really want $2^{234}$ or $a^{i+1}_{n-1}$.

Fractions

Symbol Command
Frac1.gif \frac{1}{2}
Frac2.gif \frac{2}{x+2}
Frac3.gif \frac{1+\frac{1}{x}}{3x + 2}

Most fractions look better in \displaystyle (remember, you don't need the \displaystyle declaration if you are in \[...\] or $$...$$ mode.) You can use \dfrac as a shortcut:

Symbol Command
$\frac{1}{2}$ \dfrac{1}{2}
$\frac{2}{x+2}$ \dfrac{2}{x+2}
$\frac{1+\frac{1}{x}}{3x + 2}$ \dfrac{1+\frac{1}{x}}{3x + 2}

Use \cfrac for continued fractions:

Symbol Command
$\cfrac{2}{1+\cfrac{2}{1+\cfrac{2}{1+\cfrac{2}{1}}}}$ \cfrac{2}{1+\cfrac{2}{1+\cfrac{2}{1+\cfrac{2}{1}}}}

Radicals

Symbol Command
$\sqrt{2}$ \sqrt{2}
$\sqrt{x+y}$ \sqrt{x+y}
$\sqrt{x+\frac{1}{2}}$ \sqrt{x+\frac{1}{2}}
$\sqrt[3]{3}$ \sqrt[3]{3}
$\sqrt[n]{x}$ \sqrt[n]{x}

Sums, Products, Limits and Logarithms

We use _ to get the 'bottom' parts of summations, products, and limits, as well as the subscripts of logarithms. We use ^ to get the 'top' parts of sums and products. (Integration symbols work the same way, as you'll see in the calculus section.) Click here for a few other commands which take 'bottom' parts.

Symbol Command
Sum1.gif \sum_{i=1}^{\infty}\frac{1}{i}
Prod1.gif \prod_{n=1}^5\frac{n}{n-1}
Lim1.gif \lim_{x\to\infty}\frac{1}{x}
Log1.gif \log_n n^2

Some of these are prettier in display mode:

Symbol Command
$\sum_{i=1}^{\infty}\frac{1}{i}$ \displaystyle\sum_{i=1}^{\infty}\frac{1}{i}
$\prod_{n=1}^5\frac{n}{n-1}$ \displaystyle\prod_{n=1}^5\frac{n}{n-1}
$\lim_{x\to\infty}\frac{1}{x}$ \displaystyle\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}$ \displaystyle\sum\frac{1}{i}
$\frac{n}{n-1}$ \displaystyle\frac{n}{n-1}
Log2.gif \log n^2
Ln1.gif \ln e

Mods

Symbol Command
$9\equiv 3 \bmod{6}$ 9\equiv 3 \bmod{6}
$9\equiv 3 \pmod{6}$ 9\equiv 3 \pmod{6}
$9\equiv 3 \mod{6}$ 9\equiv 3 \mod{6}
Pod1.gif 9\equiv 3 \pod{6}

Combinations

Symbol Command
Binom1.gif \binom{9}{3}
Binom2.gif \binom{n-1}{r-1}

These often look better in display mode:

Symbol Command
Binom3.gif \dbinom{9}{3}
Binom4.gif \dbinom{n-1}{r-1}

Trigonometric Functions

Symbol Command Symbol Command Symbol Command
$\cos$ \cos $\sin$ \sin $\tan$ \tan
$\sec$ \sec $\csc$ \csc $\cot$ \cot
$\arccos$ \arccos $\arcsin$ \arcsin $\arctan$ \arctan
$\cosh$ \cosh $\sinh$ \sinh $\tanh$ \tanh
$\coth$ \coth

Here are a couple examples:

Symbol Command
$\cos^2 x +\sin^2 x = 1$ \cos^2 x +\sin^2 x = 1
$\cos 90^\circ = 0$ \cos 90^\circ = 0

Calculus

Other Functions

Matrices

We can build an array or matrix with the \begin{array} command, 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 \\
d & e & f \\
g & h & i \end{array}
\right)\]
is given by the equation
\[ f(\lambda)
= \left|
\begin{array}{ccc}
\lambda - a & -b & -c \\
-d & \lambda - e & -f \\
-g & -h & \lambda - i \end{array}
\right|.\]

More simply, we can use the shortcut commands in the amsmath package:

The characteristic polynomial $f(\lambda)$ of the
$3 \times 3$ matrix
\[
\begin{pmatrix}
a & b & c \\
d & e & f \\
g & h & i
\end{pmatrix} \]
is given by the equation
\[ f(\lambda)
= \begin{vmatrix}
\lambda - a & -b & -c \\
-d & \lambda - e & -f \\
-g & -h & \lambda - i
\end{vmatrix}.\]

You can read more about how the array command works here (it works the same as tabular) and more about using \left and \right here.

We can also use this environment to typeset any mathematics that calls for multiple columns, such as funky function definitions like this one:

\[ f(x) = \left\{ \begin{array}{ll}
x+7 & \mbox{if $5< x$};\\
x^2-3 & \mbox{if $-3 \le x \le 5$};\\
-x & \mbox{if $x < -3$}.\end{array} \right. \]

Text Styles in Math Mode

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 $\displaystyle\sum_{n=1}^\infty \frac{1}{n^2}$.}
{$\displaystyle\frac{1}{e}$} {$\displaystyle\frac{2}{\pi}$}
{$\displaystyle\frac{\pi^3}{8}$} {$\displaystyle\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