Two basic types of formula (or math) environments are available in : (1) a text mode in which math expressions are placed in a line of text, and (2) a display environment in which the math expression is place on a separate line.
text mode: To place a math expression in a line of text, simply enclose the math expression with $. . .$ or, equivalently, (…). In this mode, some symbols such as and are written more compactly compared to their treatment in the “displaymath” mode.
displaymath (without equation numbering): To center a math expression or formula on a separate line, embed the math expression between the environment delimiters egin{displaymath} … end{displaymath}
. Shorthand equivalents are [… ] or $$…$$.
displaymath (with equation numbering): To place automatically an equation number to the right of a math expression that is centered on a separate line, use the egin{equation} . . . end{equation}
environment. There is no built-in shorthand equivalent (although a shorthand version can be defined in the preamble; see Section 7.2).
In , a large variety of mathematical expressions can be created; however, you often have to resort to fairly complex programming for “difficult” expressions. These difficult expressions are handled much more easily with the macros of However, in these notes we stick to pure (mixed occasionally with ). Here are some examples.
For many math expressions, it is important to perform some small horizontal spacing adjustments to move parts together or apart. Here are the math-mode spacing commands ( indicates a space).
Three basic types of equations are frequently encountered when using : (1) simple equation, (2) multi-line or multi-conditional equations, and (3) an array of equations.
A simple equation, such as y (x) = 3 + x2 or Eq. (4.1),
(4.1)
is produced by
A simple equation, such as $y(x) = 3+x^2$
or Eq.~(
ef{simpleeq}),
egin{equation}
f(x) + g(x) = sqrt{l + x^2}
label{simpleeq}
end{equation}
Note the use of the optional label
in the single-line equation definition so that this label can be referenced later in the text with
ef{simpleeq}
.
Often an equation will have multiple lines on one side of the = sign that depend on various conditions. For example,
(4.2)
is produced by
egin{equation}
f(x) = left{ egin{array}{ll}
z-y & mbox{if $y>0$} \
z+y & mbox{otherwise}
end{array}
ight.
label{eqtype2}
end{equation}
Note the use of mbox{. . .}
to place roman fonts in the equation.
Sometimes you will want to break a long equation into several lines or to have several equations in one block. For this, provides the eqnarray environment.
(4.3)
(4.4)
is produced by
egin{eqnarray}
f(x) & = & sqrt{l - x~2} label{eqmultil} \
g(x) & =&a + b + c + d + e + f
onumber \
& &+ g + h + i + j label{eqmulti2}
end{eqnarray}
Notice that when an equation is broken between lines with \ the
onumber
command is used to prevent an equation number being automatically printed to the right of the broken line.
There is also an eqnarray*
environment that is exactly like the eqnarray
environment except that equation numbers are not automatically placed at the end of each equation line. To force an equation number in this environment, insert yesnumber
after the line and before the line break \.
The spacing around the equal sign in an eqnarray environment is larger than that in an equation
environment, as can be seen by comparing Eqs. (4.1) and (4.3). The spacing can be made the same for both environments by defining in the preamble the following new environment:
ewenvironment {Eqnarray}%
{arraycolsep 0.14emegin{eqnarray}}{end{eqnarray}}
Then with the use of egin{Eqnarray}. . .end{Eqnarray}
, Eqs. (4.3) and (4.4) are typeset as
(4.5)
(4.6)
In the eqnarray
environment, we often want to insert text between equations without leaving the environment and the alignment on the = sign. For example,
(4.7)
a = b + c
(4.8)
d = f + g
and finally
(4.9)
h = d + e + f
uses
oalign
to insert the words”and finally” between the second and third equation.
In the { t eqnarray} environment,
we often want to insert text between
equations without leaving the
environment and the alignment on
the = sign. For example,
egin{eqnarray}
a & = & b + c \
d & = & f + g \
oalign{hbox{andand}}
h & = & d + e + f
end{eqnarray}
uses verb|
oalign| to insert the
words ’‘and finally” between the
second and third equation.
A strut, such as
ule{0in}{.25in}
, is a box of some specified height but of zero width so that nothing is printed. However, the height of a strut forces extra space above the line containing the strut. A strut can thus be used to increase spacing between lines of a multiline equation as shown in the second example below. Note the use of s
, which has been defined as a shorthand notation for a strut by the defs{
ule{0in}{.30in}}
statement. This is a useful definition to place in the document preamble if you use a lot of struts.
The strut is also useful for tricking into thinking that the line is taller than it really is. In this way, variable delimiters (for example, left[ or
ight}
) on different lines of a multiline equation can be made the same size. Consider the following examples. In the second example, a strut has been placed in the second line to force the closing square bracket to have the same size as the square bracket on the first line.
Four font sizes and styles are available in math mode: extstyle
, displaystyle
, scriptstyle
, and scriptscriptstyle
.
displaystyle
is used for displayed equations (equations on their own lines), whereas extstyle
is used for mathematical expressions in a line of text. These fonts are almost the same except that the large operators are different: for example,
in extstyle
we get and and in displaystyle
we get and .
The math style scriptstyle
is used for first-level superscripts and subscripts; scriptscriptstyle
is normally used for higher-level superscripts and subscripts.
The frac
command produces small fonts when they are nested, as in the first example below. Fonts can be returned to normal size with the displaystyle
command, as in the second example. However, the third construction is the preferred way to typeset this type of expression.
has a nasty habit of using small fonts in an array or eqnarray environment (see the first example that follows). The second example shows a way to avoid this problem. Notice also in the second example that the strut (
ule{0in}{5ex}
) is used to increase the spacing between the two lines of the array.
The easiest way to gain control of equation numbers in is not to use them! In some situations the normal convention of chapter, eqno is not flexible enough and neither are most of the “subeqn” styles (see Section 8.4). But the command eqno()
also works in , so that you can write
$$ pV = nRT . eqno(mbox{em ideal gas law: } 12345a)$$
to produce
pV = n RT. (ideal gas law: 12345a)
As a special effect, you may occasionally want a displayed equation to have a tag both on the left and on the right of the equation line, as in the two following examples.
The first example, with the equation centered on the line, is produced by
[ Longrightarrow setboxO=hbox{$Longrightarrow$}kern-wdO
hspace{.5columnwidth minus .5columnwidth}
g(x)=int_aˆb dx, frac{f(x)Hsqrt{l-xˆ2}}
hspace{.5columnwidth minus .5columnwidth}
llap{em a difficult integral}
]
The second example, with the equation centered between the tags, is produced by
[ Longrightarrow
hspace{.5columnwidth minus .5columnwidth}
g(x)=int_aˆb dx, frac{f (x)Hsqrt{l-xˆ2}}
hspace{.5columnwidth minus .5columnwidth}
mbox{em a difficult integral}
]
uses the math italics font for variables in a math environment; for example, $f (x)$ is typeset as f(x). More importantly, treats adjacent letters as different variables multiplied together. This can produce ugly results when a multiletter variable is used. For example, suppose you want to define a symbol for a variable called “Working-Level-Month—effective.” The natural $WLM_{eff}$
produces the strangely spaced result WLMeff. What needs to be done is to use regular italics font. Thus, to produce the more pleasing result WLMeff, you would use ${em WLM}_{ ! ! em eff}$.
Notice the use of negative spaces ! to move the superscript closer to the variable name. If this variable is to be used many times in your document, it is a good idea to define a command to produce this variable by placing in the preamble
ewcommand{WLMeff}{{em WLM}_{!!em eff}}.
Then $WLMeff$
will produce WLMeff.
Often roman font and not italic font is needed in the math environment such as when typing chemical equations. For example, $$C + H_2
ightarrow CH_4$$
C + 2H2 → CH4
The chemical symbols should be in roman font, and CH is not two separate variables multiplied together. To produce a better result, use
$$ mbox{C} + mbox{2H_2} ightarrow mbox{CH_4}$$
to produce
C + 2H2 → CH4
As an alternative to using mbox{
m
roman text }, you can define a chem
command to cause all characters to be roman. The chem
command is defined in the preamble as
defchem{everymath={fam0 }fam0 }
Note the space after each fam0
command. Then, in math mode, the command chem
will cause all lettering to be in roman font. For example,
$$chem C + 2H_2 ightarrow CH_4$$
produces
C + 2H2 → CH4
Alternatively, if the command everymath={fam0 }
is placed in the preamble of a document, all math will be typeset in roman face.
Math italic is the default font in math mode. In this mode, the f
command causes all uppercase Greek letters and numbers to be printed in boldface. Also, the alphabetic characters are changed from math italics to bold Roman font. However, in math mode, lower case Greek letters and all operators are treated as symbols, and are thus unaffected by the command f
. Consider the following example.
To create bold lowercase Greek letters, operators, and bold math italics, you must use the oldmath
command before entering the math mode. The command oldmath
causes uppercase Greek letters to be set in bold italics and numbers to become large bold subscripts. Here is how oldmath
affects the preceding example.
Because oldmath
is a text mode command, its use becomes rather cumbersome in the displayed math environment where you have to do things like
[ mbox{oldmath $sigma$} = mbox{oldmath $alpha$}
- mbox{f b} ]
to produce
σ = α—b
Here is a preamble macro for use in math mode that will produce the appropriate boldface (either oldmath
or f
) for any symbol:
ewcommand{mathbold} [1] {mbox{oldmath $f#1$}}
With this macro, the example equation can be composed more easily as
[ mathbold{ sigma = alpha - b} ]
As explained above, to obtain a bold lowercase Greek letter it is necessary to use oldmath
; for example, to produce µ you enter $mbox{oldmath $mu$}$
. However, to put an accent on such a lowercase Greek letter is not so easy. There is a warning on page 201 of Lamport’s book that oldmath
may have problems, but it does not mention accents. Apparently, some accents cause oldmath
great problems. For example, $mbox{oldmath $hat{mu}$}$
produces which gives an accent that looks like a smile going through the µ.
However, widehat
does not cause such problems. To produce , you enter
$mbox{oldmath $widehatmu$}$.
To produce a regular size, but not bold, hat symbol on a bold µ, that is, requires an equally awkward sequence:
$hat{mbox{oldmath $mu$}}$
The cdot
in math mode for the dot product operator is too small, as in U . V ($f U cdot V$)
and the ullet
symbol produces too large a dot, as in U • V ($f U ullet V$)
. Moreover, the spacing around the dot is a bit large. Here are two alternatives that differ only in their spacing around the dot:
U.Vfrom$f U !stackrel{scriptscriptstyleullet}{{}}! V$
U-V from $f U scriptstyle strackre{ullet}{{}}} V$.
To use one of these alternatives, it is best to define a dotprod
command in the preamble with, for example,
ewcommad{dotprodM{scriptscriptstyle stackrel{ullet}{{}}}}
Then, to produce U • V, simply type $f U dotprod V$
.
In many math documents the “greater than or approximately equal to” () or the “less than or approximately equal to” () operators are needed. However, does not include these compound operators in its fonts (although they are included in the fonts under the NFSS). These two operators can be constructed by placing the following in your preamble.
makeatletter
%------- Compound Math Operators by D. Arseneau
defgsim{compoundrel>oversim}
deflsim{compoundrel<oversim}
defcompoundrel#lover#2{mathpalettecompoundreL{{#l}over{#2}}}
defcompoundreL#l#2{compoundREL#l#2}
defcompoundREL#l#2over#3{mathrel
{vcenter{hbox{$m thuildrel{#l#2}over{#l#3}$}}}}
makeatother
Then use $gsim$
to produce and $lsim$
to produce.
’s array environment can be used to create matrix equations, as shown in the following example:
If you use parentheses to delimit the arrays (as in this example), it is easier to use ’s pmatrix
command as in
’s vphantom
command can also be used to give vertical alignment of a row vector times a matrix. Consider these two cases:
A continued fraction such as
is created by
[
t_0+frac{l}{t_l+frac{ extstyle 1}
{ extstyle t_2+frac{ extstyle 1}
{ extstyle t_3+frac{ extstyle 1}
{ extstyle t_4+cdots}}}}
]
A continued fraction can also be written in “short” form as
$$l+cfrac{l}{t_l}+cfrac{l}{t_2}+cfrac{l}{t_3}+
cfrac{l}{t_4}+cdots ,$$
To use this short form, you must first define the following definition of cf rac in the preamble.
defcfrac#l#2{array{c}multicolumn{l}{|c}{#1}\
hlinemulticolumn{l}{|c}{#2}endarray}
To produce a chemical reaction equation such as
a double-arrow reaction rate symbol is needed. One method is to define in the preamble the macro
def eactionrates#l#2{mathrel{mathop{ ightleftharpoons}limits^{#l}_{#2}}}
and then use
mbox{A} + mbox{B} eactionrates{k_l}{k_2} mbox{C}
Sometimes you will want to e
mphasize an equation by placing a box or frame around it, such as or even
To produce this, use
… equations such as framebox{$x^2 = z + sin(z)$} or even
{fboxsep=.2in
$$ framebox{$ f(x, y) = x^2 + y^2 $} $$ }
To add equation numbers to a boxed equation is a bit more complex. To produce
(4.10)
use the following:
egin{equation}
mbox{fboxsep=.lin framebox{$ f(x, y) = x^2 + y^2 $}}
end{equation}
Putting a box around multiline equations is more difficult. The easiest way is to use the macros (see Section 5.4). To produce
(4.11)
use the following (it is assumed that the macros have been loaded):
egin{equation}
BeginTable