Difference between revisions of "LaTeX:Math"

Line 33: Line 33:
 
</nowiki></pre>
 
</nowiki></pre>
 
After you compile this and view it, you should see:
 
After you compile this and view it, you should see:
 +
 
[[Image:Mathsamp2.gif]]
 
[[Image: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:
 
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:
 
<pre><nowiki>
 
<pre><nowiki>

Revision as of 18:44, 10 July 2007

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:

For example, look at the following segment of code:

\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 $math stuff here$ to typeset math, the resulting math expression appears right in the test at the location of the $...$. Sometimes we want to break some of the math out out of the text and give it its own special line. To do so, we use $$math stuff here$$ or \[math stuff here\] to put the math text in 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

\begin{equation} math \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.

\documentclass{article}
\begin{document}
\begin{equation}
2+2=4
\end{equation} 
\end{document} 

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 referencing commands.

Generally, you'll only use \begin{equation} when you need the label.


Display Style (\displaystyle)

Equation Arrays (\eqnarray)

Additional Packages

See Also