LaTeX

From PHYSpedia
Revision as of 09:59, 14 March 2013 by Cclark (talk | contribs) (Symbols)

Jump to: navigation, search

Latex is a program for typesetting papers. It is very good at typesetting math, so it is commonly used in fields that require equations to be displayed in papers.

Installation

To start writing Latex documents, you two things. A Latex ``compiler (this is a set of programs and files that allow you to generate pdfs from a Latex file), and a Latex editor (you can use a plain text editor, but an editor designed for Latex will have many more handy features). There are a few different Latex compilers and editors. The instructions below are to install the texlive compiler and texmaker editor.

Windows
  1. download and run the install-tl.zip file from here http://www.tug.org/texlive/acquire-netinstall.html
  2. install texmaker, which is cross platform, from here http://www.xm1math.net/texmaker/
  3. configure texmaker: go to toolbar, choose options/quick build/User, add the following text
latex -interaction=nonstopmode %.tex|bibtex %.aux|latex -interaction=nonstopmode %.tex|latex -interaction=nonstopmode %.tex|dvips -t letter %.dvi -o %.ps|ps2pdf -dPDFSETTINGS=/prepress %.ps
Mac
  1. Follow the instructions here http://www.tug.org/mactex/2011/
  2. install texmaker, which is cross platform, from here http://www.xm1math.net/texmaker/
Ubuntu Linux
use synaptic to install the texmaker which will automatically install texlive as well
> sudo apt-get install texmaker

You may also want to to install the REVTeX package (https://authors.aps.org/revtex4/). Many APS journals use a style included in this package. However, it is not necessary to do this before starting to use Latex. For more information on installing custom document and bibliography styles, see http://www.math.uiuc.edu/~hildebr/tex/tips-customstyles.html

Here is a template that can be used for Advanced Lab: File:Advanced lab.tar (Use 7-Zip, http://www.7-zip.org/, on Windows to extract this .tar file.)

Using LaTeX

External References

There are many, many different websites full of tutorials and references for Latex. Here are a couple of good ones.

LaTeX Wikibook: http://en.wikibooks.org/wiki/LaTeX

LaTeX Cookbook: http://www.personal.ceu.hu/tex/cookbook.html

Simple Tasks

Latex is basically a language for "marking up" plain text to indicate how it should be formatted. The Latex compiler reads the marked up text and generate the output that is indicated. In Latex terminology, the user used commands to tell the Latex compiler what to do. Latex commands start with an '\'. For example, the latex command to create the Greek letter alpha (in math mode) is \alpha. Some commands take arguments. These arguments are given to the command inside of curly brackets {}. For example, the Latex command to bold face some text is \textbf, and this command takes one argument, the text to bold face; \textbf{make this bold}. Some commands take optional arguments. These arguments are given in square brakets [].

A bare Latex document must declare the document class and have a document environment. These are specified with the \documentclass command and a \begin{document} and \end{document} commands.

\documentclass{article}

\begin{document}
This is the simplest document I could think of.
\end{document}

By default, Latex has a lot of useful features, but you will quickly find yourself needing to do something that is not possible with plain Latex. Latex allows you to use packages that have extra functionality. These packages are similar to libraries used to program in other languages. To use a package, you give the package name to the \usepackage command. A package that contains a lot of useful features for writing equations is the amsmath package.

\documentclass{article}
\usepackage{amsmath}  % include some useful tools for writing equations.

\begin{document}
This is the simplest document I could think of.
\end{document}

Notice that we used a Latex comment in this cases. Anything after a % is ignored and can be used to provide extra information that will not get put into the formatted document. In this case, we indicate why the amsmath package is being included.

Inserting Math

There are several different ways to display equations in a Latex document. Latex uses what it calls "math mode" to display equations. It does this because equations are formatted differently than normal text, so it is necessary to explicitly indicate equations. So, the many different ways of displaying equations are just different ways of entering math mode. There are many commands that only work in math mode (for example, the commands for Greek letters). Luckily, it is very simple to get into math mode. Here is a short list of the most common ways of displaying equations.

single dollar sign
$
any text inside of a pair of dollar signs is formatted in math mode.
double dollar sign
$$
text inside a pair of double dollar signs is formatted in math mode, but the equation is displayed centered on its own line.
equation environment
the equation environment will display an equation centered on its own line, like the $$, but it will also number it
align environment
the align environment can be used to format multi-line equations so that they align (for example, you may want the = sign in all of them to align). You must use a & to indicated where equations should be aligned, and a \\ to indicate the end of an equation line. If you don't want all lines to get numbered (which will happen by default) you can insert the \nonumber command at the end of the line.

Both the equation and align environments have "starred" versions that will format equations without numbers. This is sometimes useful to display small, simple equations that will not be referenced later in the paper.

One of the most powerful features of Latex is the ability to automatically number and reference equations. Rather than manually numbering each equation in your paper and then referring to those numbers directly in your papers text, Latex allows you to label your equation and then refer to that equation with the label. This has the advantage that you can then reorder your equations if needed, and the equation numbers will all be updated automatically. Here is a simple example document demonstrating the different methods for entering math mode.

\documentclass{article}

\usepackage{amsmath}

\begin{document}
To insert equations, or math symbols, inline with the text, just use the dollar sign like this. Greek letter alpha is $\alpha$. This can be used in equations; $y = e^{\alpha t}$
The double dollar signs will get you an equation on its own line $$y = mx + b$$, even if it is inline with the text.

To number equations use the equation environment
\begin{equation}
\label{eq:quadradic}
y = ax^2 + bx + c
\end{equation}
The equation above can now be referenced as Equation \ref{eq:quadradic}. Sometimes, we want multi-line equations. The align environment allows you to align you lines. Just use a & to indicate where they should be aligned, and a \\ to indicate new lines. The \nonumber command will cause a specific line to not be numbered.
\begin{align}
  Q(t) &= CV \nonumber \\
       &= C \left( \mathcal{E} - V_c \right) \\
  \frac{d Q}{dt} &=  \frac{d }{dt} \left(C\left( \mathcal{E} - V_c \right)\right) \\
                 &=  C\left( \frac{d \mathcal{E} }{dt} - \frac{d V_c}{dt} \right)
\end{align}

\end{document}


Symbols

There are hundreds (probably thousands) of commands for writing equations. The Latex wikibook has a page on mathematics here. A very dense list of math symbols can be found here. A downloadable pdf containing a dense list of symbols can be found here