Difference between revisions of "LaTeX:Math"

m
m (Display Style (\displaystyle))
 
(38 intermediate revisions by 21 users not shown)
Line 4: Line 4:
  
 
==Math Mode==
 
==Math Mode==
LaTeX uses a special math mode to display mathematics. To place something written in TeX in '''math mode''', use <nowiki><math></nowiki> 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:   
+
LaTeX uses a special math mode to display mathematics. To place something written in TeX in '''math mode''', use <nowiki>$</nowiki> 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:   
  
  
Line 10: Line 10:
 
\documentclass{article}
 
\documentclass{article}
 
\begin{document}
 
\begin{document}
The solution to </math>\sqrt{x} = 5<math> is </math>x=25<math>.
+
The solution to $\sqrt{x}=5$ is $x=25$.
 
\end{document}  
 
\end{document}  
 
</nowiki></pre>
 
</nowiki></pre>
Line 19: Line 19:
 
[[Image:Mathsamp1.gif]]
 
[[Image:Mathsamp1.gif]]
  
If you remove the <nowiki></math></nowiki> symbols from your source file then try to compile, you should get 'Missing <nowiki><math></nowiki> 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).
+
If you remove the <nowiki>$</nowiki> symbols from your source file then try to compile, you should get 'Missing <nowiki>$</nowiki> 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.
 
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 ==
 
== Display Math ==
As we saw above, when using <nowiki></math></nowiki>math stuff here<nowiki><math></nowiki> to typeset math, the resulting math expression appears right in the text at the location of the <nowiki></math></nowiki>...<nowiki><math></nowiki>. Sometimes we want to break some of the math out of the text and give it its own special line. To do so, we use <nowiki><cmath></nowiki>math stuff here<nowiki></cmath></nowiki> or <cmath>math stuff here</cmath> to put the math text in math mode:
+
As we saw above, when using <nowiki>$</nowiki>...<nowiki>$</nowiki> 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 <nowiki>\[</nowiki>math stuff here<nowiki>\]</nowiki> or <nowiki>$$math stuff here$$</nowiki> (the former is usually preferred now) to put the expression in display math mode:
 
<pre><nowiki>
 
<pre><nowiki>
\documentclass{article}
+
\documentclass{article}  
 
\begin{document}
 
\begin{document}
The solution to <cmath>\sqrt{x} = 5\] is \[x=25.</cmath>
+
The solution to \[\sqrt{x} = 5\] is \[x=25.\]
 
\end{document}  
 
\end{document}  
 
</nowiki></pre>
 
</nowiki></pre>
Line 40: Line 40:
 
\begin{document}
 
\begin{document}
 
The solution to
 
The solution to
<cmath>\sqrt{x} = 5</cmath>
+
\[                                                                                                                                                                                                 
 +
\sqrt{x} = 5
 +
\]
 
is
 
is
<cmath>x=25.</cmath>
+
\[
 +
x=25.
 +
\]
 
\end{document}  
 
\end{document}  
 
</nowiki></pre>
 
</nowiki></pre>
Line 48: Line 52:
 
We can also use
 
We can also use
  
\begin{equation} math \end{equation}
+
\begin{equation} ... \end{equation}
  
to display mathematics. This approach also creates a label, which we can refer to later if we like. Make sure you read our notes about referencing before using these labels for references - it's much better to use \label and \ref than to refer to the equations by number in your source file.
+
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:
 
<pre><nowiki>
 
<pre><nowiki>
 
\documentclass{article}
 
\documentclass{article}
 +
\usepackage{amsmath}
 +
 
\begin{document}
 
\begin{document}
\begin{equation}
+
The quadratic formula is shown in Equation~(???).
2+2=4
+
 
\end{equation}  
+
\begin{equation} \label{eqn:quadratic}
 +
x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a}
 +
\end{equation}
 +
 
 +
Using Equation~(???), we obtain...
 
\end{document}  
 
\end{document}  
 
</nowiki></pre>
 
</nowiki></pre>
  
Notice the (1) out to the right when you compile the above. Once again, rather than typing (1) in your source file to refer to this equation, use LaTeX [[LaTeX:Layout|referencing]] commands.
+
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 [[LaTeX:Layout|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).
 
 
Generally, you'll only use \begin{equation} when you need the label.
 
  
== Display Style () ==
+
== Display Style (\<!-- -->displaystyle) ==
Sometimes we have complicated expressions that we don't want to put on their own lines, but that don't render well with <nowiki></math></nowiki>...<nowiki><math></nowiki> mode. For example:
+
Sometimes we have complicated expressions that we don't want to put on their own lines, but that doesn't render well with <nowiki>$</nowiki>...<nowiki>$</nowiki> mode. For example:
 
<pre><nowiki>
 
<pre><nowiki>
 
\documentclass{article}
 
\documentclass{article}
 
\begin{document}
 
\begin{document}
Evaluate the sum </math>\sum_{i=0}^n i^3<math>.
+
Evaluate the sum $\sum_{i=0}^n i^3$.
 
\end{document}  
 
\end{document}  
 
</nowiki></pre>
 
</nowiki></pre>
Line 76: Line 84:
 
[[Image:Mathsamp3.gif]]
 
[[Image:Mathsamp3.gif]]
  
That summation symbol is a little ugly. We can make it prettier by using:
+
That summation symbol is a little ugly. We can make it prettier by using \<!-- -->displaystyle:
 
<pre><nowiki>
 
<pre><nowiki>
 
\documentclass{article}
 
\documentclass{article}
 
\begin{document}
 
\begin{document}
Evaluate the sum </math>\sum_{i=0}^n i^3<math>.
+
Evaluate the sum $\</nowiki><nowiki>displaystyle\sum\limits_{i=0}^n i^3$.
 
\end{document}  
 
\end{document}  
 
</nowiki></pre>
 
</nowiki></pre>
Line 87: Line 95:
 
[[Image:Mathsamp4.gif]]
 
[[Image:Mathsamp4.gif]]
  
Notice that the summation symbol looks much nicer now - adding the at the beginning of your math (inside the <nowiki></math></nowiki>...<nowiki><math></nowiki>) will often make complicated math render more nicely.
+
Notice that the summation symbol looks much nicer now - adding the \<!-- -->displaystyle at the beginning of your math (inside the <nowiki>$</nowiki>...<nowiki>$</nowiki>) will often make complicated math render more nicely.  Note that it is not necessary to use \<!-- -->displaystyle when using display mode (<nowiki>\[ and \]</nowiki> or $$ and $$).
  
== Equation Arrays (\eqnarray) ==
+
== Aligning Equations (align) ==
One very useful tool in displaying equations well is eqnarray, which allows you to neatly align a string of equations:
+
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:
<pre><nowiki>
+
<pre>
 
\documentclass{article}
 
\documentclass{article}
 +
\usepackage{amsmath}
 
\begin{document}
 
\begin{document}
\begin{eqnarray*}
+
\begin{align*}
2x^2 + 3(x-1)(x-2)&=&2x^2 + 3(x^2-3x+2)\
+
2x^2 + 3(x-1)(x-2) & = 2x^2 + 3(x^2-3x+2)\&= 2x^2 + 3x^2 - 9x + 6\&= 5x^2 - 9x + 6
&=& 2x^2 + 3x^2 - 9x + 6\
+
\end{align*}
&=& 5x^2 - 9x + 6
 
\end{eqnarray*}
 
 
\end{document}  
 
\end{document}  
</nowiki></pre>
+
</pre>
 
Compiling this should give:
 
Compiling this should give:
 +
<cmath>
 +
\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*} </cmath>
  
[[Image:Mathsamp5.gif]]
+
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
 +
<pre>Math stuff & more math stuff \</pre>
 +
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.
  
There are a few things to notice here. First, the * after eqnarray 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
+
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.
  
Math stuff & more math stuff & more math stuff\
+
Finally, notice that there are no <dollar/> symbols, <nowiki>$$ ... $$</nowiki>, or <nowiki>\[ ... \]</nowiki>, yet everything is rendered in math mode. This happens because align automatically puts everything in math mode - you don't need <dollar/>s or <nowiki>\[ ... \]</nowiki> tags.
  
The & symbols separate the columns. There must be three columns (i.e. two & symbols). 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{eqnarray*} 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.
+
Also note that in an align environment, you can use the \nonumber command if you want only some lines to be numbered. For example,
 
+
<pre>
Usually, we only use relational symbols like =, >, or < in the middle column; eqnarray aligns these symbols as you see above. That's why we like eqnarray.
+
\documentclass{article}
 
+
\usepackage{amsmath}
Finally, notice that there are no <nowiki></math></nowiki> symbols or <cmath>..\], yet everything is rendered in math mode. This happens because eqnarray automatically puts everything in math mode - you don't need <nowiki><math></nowiki>...<nowiki></math></nowiki> or \[...</cmath>.
+
\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}
 +
</pre>
 +
compiles to this:
 +
<cmath>\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}</cmath>
  
 
==Additional Packages==
 
==Additional Packages==
Line 121: Line 147:
 
\usepackage{amsmath}
 
\usepackage{amsmath}
 
\begin{document}
 
\begin{document}
We can write more than just <math>x</math> now.
+
We can write more than just $x$ now.
Now we can write things like <math>\binom{5}{3}</math>.
+
Now we can write things like $\binom{5}{3}$.
 
\end{document}
 
\end{document}
 
</nowiki></pre>
 
</nowiki></pre>
  
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 [[LaTeX:Layout|how to include more packages]], and [[LaTeX:Packages|how you can create packages of your own]].
+
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 [[LaTeX:Layout|how to include more packages]] and [[LaTeX:Packages|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 <nowiki><math>x</math></nowiki> 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!
+
Finally, one last point of style - notice in that last example that we put the x in math mode by writing <nowiki>$x$</nowiki> 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 [[LaTeX:Symbols|symbols]] or [[LaTeX:Commands|commands]], try reading the [ftp://ftp.ams.org/pub/tex/doc/amsmath/amsldoc.pdf user's guide for the amsmath package], which contains some of the really fancy applications of the ams packages.
+
If you find you want to do some math typesetting that you can't find on this page, or among our discussions of [[LaTeX:Symbols|symbols]] or [[LaTeX:Commands|commands]], try reading the [http://texdoc.net/texmf-dist/doc/latex/amsmath/amsldoc.pdf user's guide for the amsmath package], which contains some of the really fancy applications of the ams packages.
  
 
==See Also==
 
==See Also==
 
*[[LaTeX:Examples | Next: Examples]]
 
*[[LaTeX:Examples | Next: Examples]]
 
*[[LaTeX:Basics | Previous: Basics]]
 
*[[LaTeX:Basics | Previous: Basics]]

Latest revision as of 08:03, 5 August 2024

LaTeX
About - Getting Started - Diagrams - Symbols - Downloads - Basics - Math - Examples - Pictures - Layout - Commands - Packages - Help

This article will detail how to work with math mode in LaTeX and how to display equations, formulas, and mathematical expressions in general.

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

Mathsamp1.gif

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:

Mathsamp2.gif

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

...

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

Mathsamp3.gif

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:

Mathsamp4.gif

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 $$ and $$).

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: \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*}

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: \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}

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