Lesson 07

Preparing Documents with LaTeX

INTRODUCTION

LaTeX is a document preparation system built on Donald Knuth’s original 1978 system called TeX. LaTeX is a sophisticated program designed to produce high–quality typesetting, especially for documents with mathematical content.

The creation of a LaTeX document requires two steps:
  1. Prepare a source file, using your favorite text editor, containing a mixture of content and LaTeX commands.
  2. Process the source file using the LaTeX software.

This tutorial assumes that you have the LaTeX software installed on your computer. There are many distributions of LaTeX, and most of them are free. Some popular distributions include MiKTeX for Microsoft Windows, Tex Live for linux (although you may already have it installed), and MacTeX for Macintosh.

There are also many resources available for LaTeX, including the LaTeX project site

http://www.latex-project.org/

and the wiki book

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

You can find lists of commands and symbols on the internet as well; see, for example,

http://www.artofproblemsolving.com/Wiki/index.php/LaTeX:Symbols

A SIMPLE EXAMPLE

Let’s say you want to prepare a document with a single line that says “Physics is fun, and $e^{i\pi} = -1$”. Here is how to do it. Open up your favorite text editor, for example, gedit in Linux or Notepad in Windows or TextEdit in Macintosh. Now type the following:


\documentclass{article} 
\begin{document}

Physics is fun, and $e^{i\pi} = -1$. 

\end{document}

Save the file with a filename that ends in “.tex”. For example, you might call the file “simpleexample.tex”. Now you need to process the file with the LaTeX software. This process depends on your operating system and the exact latex package you are using. If you are working in a tex editor like TeXShop, you click on the Typeset button, which will process your tex file to produce a pdf file and display it in a pdf viewer. If you are using Linux or Mac, you can open a terminal window, navigate to the directory (folder) containing “simpleexample.tex” and type

pdflatex simpleexample.tex

The pdflatex command will invoke the LaTeX software, process your file, and produce a pdf document called “simpleexample.pdf”. You can view this document using your favorite pdf viewer.

You probably have some questions about this example. Here are some answers:

  • Any word that begins with the symbol \ (the backslash symbol) is a special LaTeX command. For example, \pi is the command for that produces the Greek letter $\pi$. LaTeX commands are case sensitive!
  • Some LaTeX commands take an argument. For example, \documentclass with the argument article is a LaTeX command that specifies the type of document. Likewise, \begin with the argument document signals the beginning of the document.
  • The dollar sign '$' is a special LaTeX character that signals the beginning or end of an inline mathematical formula or expression. Inline mathematical formulas are always surrounded by dollar signs. (An inline formula is one that occurs in a line with surrounding text.)
  • Within a mathematical formula the caret symbol ^ denotes exponentiation. The caret is also used for superscripts; an underscore symbol _ is used for subscripts.
  • Curly braces { and } are used for grouping in LaTeX, and must occur in pairs. For example, in the mathematical formula e^{i\pi}, the argument of the exponential is i\pi.
  • Blank spaces are ignored by LaTeX. For example, the spaces around the = sign do not affect the final document.

EXERCISE: Create a source file for the simple example above. Process your file using pdflatex and view the pdf output. Modify the contents of your source file so that the line “Physics. . .” now reads:

Physics is {\em really} fun, and $ e^{i \pi }	=- {1}$

Note the extra spaces before the = sign, and the lack of space after. Also note the extra curly braces around the number 1. The command \em, which stands for “emphasis”, tells LaTeX to use italic font. Compare this to your previous result. How has the equation changed? What happens if you remove the curly braces around \em really?


ANOTHER EXAMPLE

Here is a more complicated example:


\documentclass{article} 
\begin{document}

% Begin discussion of Newton’s laws of motion 
\section{Newton’s Laws} 
Newton’s first law says: ‘‘Every body continues in its state of rest, 
or of uniform motion in a right line, unless it is compelled to change 
that state by forces impressed upon it."

Newton’s second law is 
\begin{equation} 
\vec F = m \vec a 
\end{equation}
where $\vec F$ is the force on a particle of mass $m$, 
and $\vec a$ is the particle’s acceleration.

% Begin discussion of Maxwell’s equations 
\section{Maxwell’s Equations} 
Maxwell’s equations include Gauss’s law, which reads 
\begin{equation} 
\oint \vec E \cdot d\vec a = \frac{Q}{\epsilon_0} 
\end{equation} 
in integral form.

\end{document}

Observe the following features of this example:

  • The command \section{Newton’s Laws} tells LaTeX to begin a new section with the title “Newton’s Laws”. Section numbering is automatic.
  • The percent sign % is used to begin a comment. Comments do not show up in the processed document.
  • A displayed equation is an equation that occupies its own line in the document. The LaTeX commands \begin {equation} and \end {equation} surround a displayed equation. Equation numbering is automatic.
  • A blank line signals a paragraph break.
  • The particle mass m is typeset as a formula, surrounded by $’s.
  • The LaTeX command \frac contains two arguments, which are the numerator and denominator of a fraction.

This example also includes several new LaTeX commands. The command \vec tells LaTeX to put a vector arrow over the symbol that follows. The command \oint creates a closed–path integral sign. (The ordinary integral sign is \int.) The command \cdot creates a “centered dot”. Can you guess what \epsilon_0 produces?

EXERCISE:

  • Create a source file for this example. Process the file and view the pdf output.
  • Remove the dollar signs from around m. How does this change the processed document?
  • Create a new paragraph with a statement, in your own words, of Newton’s third law.
  • Add a sentence to the section on Maxwell’s equations, and include Faraday’s law

    $\oint \vec E \cdot d\vec s = - \frac{d\Phi}{dt}$

    as a displayed equation. (The symbol $\Phi$ is the capital Greek letter Phi. Can you guess how it is created? If not, look it up on the internet.)
  • Add a sentence to the section on Maxwell’s equations that includes Ampere’s law in differential form,

    $\vec\nabla \times \vec B = \mu_0 \vec J + \mu_0 \epsilon_0 \frac{\partial \vec E}{\partial t}$

    as a displayed equation. (The command for the “del” symbol $\nabla$ is \nabla. Can you guess the command for the “times” symbol ×? How about the “partial” symbol $\partial$? How about the Greek letter $\mu$)
  • Add one more sentence that includes the final Maxwell equation $\nabla\cdot B = 0$ as an inline formula.

BELLS AND WHISTLES

Let’s add more features to the example from the last section. First, modify the \documentclass command to include an option,

\documentclass[11]{article}
that tells LaTeX to use 11 point font. (The default font size is 10 point). Immediately after the \documentclass command, insert the following lines:
\usepackage{graphicx} 
\newcommand\be{\begin{equation}} 
\newcommand\ee{\end{equation}}

The \usepackage command tells LaTeX to load a package that extends its functionality. The graphicx package allows us to insert figures into the document. The \newcommand command defines a shortcut. We can now write \be in place of the cumbersome command \begin{equation}. Likewise, \ee is now a shortcut for \end{equation}.

Just after the \begin{document} command, insert the following:

\title{Newton and Maxwell} 
\date{} 
\maketitle

These commands create a title. The command \date{} tells LaTeX to leave the date empty.

Process your file and make sure there are no errors. Try replacing some of the \begin{equation} commands with the shortcut \be. You can also use \ee. Try commenting out the command \date{} to see what happens. What if you leave \date{} uncommented, but place some text between the curly braces?

Now try including a figure. Pull up one of your Python programs that generates a figure. Save that figure (in .png format) and put it in the same directory as your source latex document. Add the following lines just below your statement of Newton’s third law:

\begin{figure} 
\includegraphics[scale=0.5]{yourfigure.png} 
\caption{Newton’s third law.} 
\end{figure}

Process the document. It should contain Figure 1 with a caption. Can you see what the scale option does to the \includegraphics command? Note that LaTeX does not necessarily place the figure precisely where the \begin{figure} command occurs in the source file. Instead, LaTeX tries to place the figure at the top or bottom of a page, close to the corresponding text.

Your figure is probably off center. Place the command \begin {center} just after \begin {figure} and \end {center} just before \end {figure}. Now is it centered?

LaTeX supports an important feature that allows you to label equations for later reference. Modify the text that produces Newton’s second law to the following:

\vec F = m \vec a \label{Newton2}
The new feature here is the label command, which has attached the label “Newton2” to this equation. Now somewhere in your document add the line
Newton’s second law (\ref{Newton2}) allows us to predict the future.

It is important to note that the pdflatex command must be executed twice. This is because LaTeX cannot always know which numbers correspond to which labels until after it has completely examined the document once. The ability to reference equations with labels is an important feature of LaTeX. Why? Imagine that your document contains 73 references to equation (1), and then you decide to add another displayed equation before equation (1)!

JOURNAL STYLES

When you are writing up your work for publication, be it in a peer-reviewed journal, a book, a conference proceedings, or just an abstract for a meeting, the publisher typically provides a LaTeX style file that typesets your paper in their preferred format. For this class we will use the format for The Astrophysical Journal, so to start on your assignment you need to download emulateapj.cls. This formatting is invoked by starting your file with

\documentclass{emulateapj}

Here is a sample paper that you can download and edit to begin your assignment.

Assignment: Start your first paper. Don't worry about the real words, but include a title, abstract, a figure that you generated (with a caption), and at least one equation. Upload the pdf output of your paper.