LaTeX Math Commands

From Liki

Jump to: navigation, search

See also latex.

Build mathematical expression in LaTeX with these commands:

Example of LaTeX

\begin{equation}
f(k;\lambda)=\frac{e^{-\lambda} \lambda^k}{k!},\,\!
\end{equation}

f(k;\lambda)=\frac{e^{-\lambda} \lambda^k}{k!},\,\!

To not show equation numbers, substitute {displaymath} for {equation}, or if you're really lazy you can use \[ and \] instead of the \begin and \end commands.

Contents

[edit] Delimiters

You often want parenthesis or other delimiters around parts of your equations. You can change the size of these by using the four commands (in increasing size):

\big
\Big
\bigg
\Bigg

where you follow the command immediately with the delimiter you want to use, such as a parenthesis, brackets, braces, a pipe, or almost anything (but you have to put a '\' before braces or a pipe since they're special characters and have to be made 'literal'). So, for example:

\Bigg\{ 2- \bigg( x^2 \bigg) \Bigg\}

Note that the delimiters need not match.

More commonly you'll want the delimiters the exact size of the enclosed text, like an array of some sort. In this case use

\left(
...stuff...
\right)

In this case you must supply both a \left and a \right because the delimiter height are made to match whatever is contained between the two commands. But, the \left doesn't have to be an actual 'left delimiter', that is you can use '\left)' if there were some reason to do it.

A nice trick for doing the verticle bar to denote the limits of an integration at the right height is to use the period as a delimiter which is invisible! So the code

\left.  x^2  \right\|_0^2

gives: \left.x^2\right|_0^2 Note that the period can be used with either \left or \right.

[edit] Defining Macros

Macros are a very nice thing about latex. Command declarations go in the document header (before the \begin{document} command). The basic structure is

\newcommand{commandname}[num]{definition of command}

where 'num' is the number of arguments to pass. If there are none, you can omit this part. A simple example is a command to insert some text, like

\newcommand{\codim}{\textrm{codim}}

which, when you TeX \codim in math mode, will insert the text 'codim' in roman text. A more complicated example is creating the symbol for transversal intersection:

\newcommand{\ti}{\;\;\makebox[0pt]{$\top$}\makebox[0pt]{$\cap$}\;\;}

which is a direct superposition of two other symbols.

Now, for an example involving passed arguments- the Clebsch-Gordon coeffecient:

\newcommand{\cg}[6]{
  \left<
  \begin{array}{cc|c}
  #1 & #3 & #5 \\
  #2 & #4 & #6
  \end{array}
  \right>
}

Then inside a math environment you would type

\cg{m_1}{j_1}{m_2}{j_2}{M}{J}

to get your instant CG-coeff.

\left \langle \left. \begin{matrix}   m_1 & m_2 \\   j_1 & j_2  \end{matrix} \right| \begin{matrix}  M \\ J  \end{matrix} \right \rangle

Similar definitions are great for anything used repeatedly and requiring a bit of typing. Other good examples would be for typesetting bras and kets.

[edit] A Few Tricks

Phantom Spaces

You sometimes want to have a specific amount of white space inserted at a certain spot, but TeX normally ignores white space, so what to do? This is what \phantom{} is for, for example:

\Gamma^i_{\phantom{i}jk}

will insert a space the size of 'i' so that you can keep your index spaces intact when writing tensors (or non-tensors in this case...). As a convenient alternative, see the Tensor Package.

Multiple Sums

Ever need to place multiple summation commands under a summation sign? Use substack:

\sum_{\substack{|m|<l \\ 0<l<\infty}} Q_{ij}

where items are seperated with \\ as in an array. You need to include amsmath [1] to use this one:

\usepackage{amsmath}

Symbol Stacking

How about stacking symbols on atop another? Stackrel (used like \frac):

\int f(x) \stackrel{?}{=} 1

Underbrace

You can create an underbrace grouping with \underbrace:

\underbrace{1 + 1 + \cdots + 1}_{1000 times}

where the '_{}' puts the included text at the point of the underbrace.

Overbrace

You can create an overbrace grouping with \overbrace:

\overbrace{1 + 1 + \cdots + 1}^{1000 times}

where the '^{}' puts the included text at the point of the overbrace.

Ellipsis

This is probably a good time to mention the difference between \cdots and \ldots. \cdots makes 3 dots centered on the line (useful for making a list of additions or other operation, as above) while \ldots makes the 3 dots on the line (useful for a comma seperated list of items).

Displaystyle

Sometimes, math gets squashed in ways you don't want it to. This typically occurs in inline equations and arrays. To make a fraction to display properly you could write

\displaystyle\frac{1}{2}

The amsmath package has a macro for this called dfrac:

\dfrac{1}{2}

or you could write your own macro. The same trick can be used to get larger supersctipts:

e^{\displaystyle kT}

if that is desirable. Similarly, this command can be used in arrays to get better height, typically if fractions are included

\begin{array}{cc}
\displaystyle\frac{1}{2} & 0\\
1 & 1
\end{array}

Note that if you do this to 'fix' your fractions, they will probably overlap between the rows of the array, which is undesirable. This can be fixed with the 'array' package (part of 'tools' in standard tetex).

\usepackage{array}
...
\setlength{\extrarowheight}{10pt}
<your squashed array>
\setlength{\extrarowheight}{0pt}

Note that the setting is global, so it has to be unset after the array is completed. This package can do lots of other cool things, see here. Also, see the next item, struts.

Struts

As was pointed out in the previous section, sometimes arrays can get overcrowded. Normally, one would get around this crowding by adding a \vspace command, to add extra vertical space. The problem is that this command is illegal within math environments, like arrays!

The work around is the concept of a 'strut', which is like dark matter - invisible, but affects the structure of what's around it. This is similar to the phantom command mentioned above. To help our arrays, do the following:

\newcommand\ST{\rule[-1em]{0pt}{2.5em}}
...
\left(\begin{array}{c|c}
\ST \dfrac{1}{2} & \dfrac{1}{2}\\ \hline
\ST \dfrac{1}{2} & \dfrac{1}{2}
\end{array}\right)
Image:strut.jpg
where we defined a strut ST and used it on each row of the array. What the command means is make a rule which starts 1em below the line we're on, that has a width of 0 and a height of 2.5em. Normally a rule with nonzero width and height is a drawn line, but setting one dimension to zero makes it invisible, a strut. LaTeX will make room for this strut, and effectively push items away from the hline. You can play with the nonzero values if necessary to get the best results. Notice the difference in the figure:

Special Functions

You can also display the names of special functions in roman letters easily by adding a \ to their names, as in

\cos(x)-\ln(y)

all trig and hyperbolic trig, exp, and log functions are defined this way, as are other mathematical expressions like lim, det, ker, sup, inf, etc... You can look in the short guide for more examples. If a function you want isn't supported then you can write a macro such as

\DeclareMathOperator{\xxx}{xxx}

if you include the amsmath package of AMS-LaTeX. If you don't have amsmath, you can use \textrm like the \codim example above, but the spacing may not be quite right (see section 5.1 of [2]).

You can use the * form

\DeclareMathOperator*{\xxx}{xxx}

to declare an operator \xxx with 'limit-style' superscripts and subscripts.

Breaking Up A Long Equation

If you don't want to use an equation array to break up a long equation, or you want it lined up in a specific way, you can use 'align'.

\begin{align}
dE = \frac{Ka}{2(1-S)} & \Biggl[\frac{4\pi(1-S)}{3T^{2}a}\left(1-\left[\frac{T^{2}a}{\pi}+1\right]^{3/2}\right)\\
                                & +\frac{T^{2}a}{4\pi}+S^{2}+2(1-S)\Biggr]\,ds_{c}.\nonumber
\end{align}

Insert '&' signs where you want the lines to line up, and '\\' to separate lines.

Another way to break up a long equation is with \lefteqn:

\begin{eqnarray}
  \lefteqn{stuff = } \\
  & & stuff + \\
  & & more stuff + \\
  & & \ldots
\end{eqnarray}

This works by assigning width 0 to the stuff in \lefteqn, so that the rest of the columns appear indented because of the inter-column spacing. This method is particularly useful if the left equation is particularly long.

[edit] See also

[edit] External links

Personal tools