Introduction to LaTex

A quick search on the web will turn up many websites with lengthy instructions on how to use LaTeX. This site is much more limited in scope. It is written with the beginning LaTeX user in mind. It is also written with the idea that the user is an undergraduate student who needs to create lab reports, term papers, and class presentations. As a result, this site is limited to the following:

How can I get LaTeX and Why should I use it?

LaTeX is an extension of the programming language TeX, created in the 1970's by Prof. Don Knuth at Stanford University. It is free to use and can be obtained at http://www.tug.org

While there are several packages available to run LaTeX, as of 2012, I suggest using ProTeXt on a PC, http://www.tug.org/proTeXt and MacTeX on a Mac, http://www.tug.org/MacTeX

I use it for several reasons:

First Document

LaTeX is like a programming language so to set up even a simple document to say ``Hello'' takes four lines. Your first task is to get LaTeX to work on your machine. You will need to learn the particulars for the software on your machine.


\documentclass{article}
\begin{document}
Hello!
\end{document}

Setting Up a Document

Often, you will want to do some basic changes to the default document style, such as change the font and the page margin. Each is these is done using a ``package'', which is a set of commands that someone has written. I generally like to use 1.25 inch margins and the Palatino font. This can be achieved using the geometry and mathpazo packages, respectively.


\documentclass{article}
\usepackage[margin=1.25in]{geometry}
\usepackage{mathpazo}
\begin{document}
Hello!
\end{document}

Two other preferences I often use are to turn off page numbers and to have the ouput with a ragged right edge, rather than justified on both edges. To see the effect, the ``blindtext'' package is being used to generated random text. Also note the use of the ``%'' sign. Anything following it is treated as a comment and is not shown in the output.


\documentclass{article}
\usepackage[margin=1.25in]{geometry}
\usepackage{mathpazo}
\raggedright %create a ragged right edge
\pagestyle{empty} %eliminate page numbers

\usepackage{blindtext} %to generate random text

\begin{document}
\blindtext
\end{document}

Finally, I often like to display stuff in landscape format and in two columns so that it fits nicely on a computer screen. This can be achieved nicely using more options with the geometry package, as shown in the second line.


\documentclass{article}
\usepackage[margin=1.25in,landscape,twocolumn]{geometry}
\usepackage{mathpazo}
\raggedright %create a ragged right edge
\pagestyle{empty} %eliminate page numbers

\usepackage{blindtext} %to generate random text

\begin{document}
\blindmathpaper %creates a few paragraphs, including some math
\end{document}

Typing Math

One of main reasons for using LaTeX is its capability of displaying beautiful mathematical notation. You tell LaTeX that you want to type math using one of the following:

To get access to some of these commands, you will also need to use the ``amsmath'' package.


\documentclass{article}
\usepackage[margin=1.5in]{geometry}
\usepackage{mathpazo}
\usepackage{amsmath}
\begin{document}

Here is some math that is on a line of its own.
\[\sum_{k=1}^{\infty}\dfrac{(-1)^{k+1}}{2k-1}=\dfrac{\pi}{4}\]

Here is some math that is inline. 
Note the use of two different sizes of fractions in the code: 
$\sum_{k=1}^{\infty}\frac{(-1)^{k+1}}{2k-1}=\dfrac{\pi}{4}$
and the math is followed by more text that really doesn't matter and you can ignore it. \\[12pt]

Here is the same thing but using the ``displaymath'' command so that it is not so scrunched. 
$\displaystyle\sum_{k=1}^{\infty}\dfrac{(-1)^{k+1}}{2k-1}=\dfrac{\pi}{4}$. 
Notice the difference in how the summation sign is drawn. 
It looks like the displayed equation at the beginning of this page.

\end{document}

When typing math in LaTeX, be sure to use commands for standard math operations. By this, I mean use the command \cos rather than simply typing ''cos''. Thus you should use the commands $\cos\theta$ and not $cos\theta$. To see further math examples, compile the following example.


\documentclass{article}

% amsmath provides access to some math notation not generally available
\usepackage{amsmath}

% units allows for nice looking units such as \unitfrac[12]{m}{sec}
\usepackage{units}


\begin{document}

\subsection*{More Math}
The ``amsmath'' package allows for two environments to help type complicated equations. 
\begin{itemize}
\item The ``cases'' environment opens a large brace at the beginning of a multi-line function.
\item The ``aligned'' environment allows you to align any part of a multi-lined equation.
\end{itemize}
In addition, the ``quad'' command is used to create extra horizontal space.

\[y=4+\begin{cases}
\begin{aligned}
\cos x  \quad &\textrm{ if }\quad x<-7\sqrt{2}\\
x^2-\sqrt{x}+\sin x  \quad &\textrm{ if } \quad x\ge 8
\end{aligned}\end{cases}\]

\subsection*{Example of the Units package}
The ``units'' package allows you to type nice looking scientific units 
in the middle of some text. For example, it is useful to know that \unitfrac[60]{mi}{hour} 
is equivalent to \unitfrac[88]{ft}{sec}.

\end{document}

Math Example (pdf)

The Listings Package

The listings package allows you to insert computer code into a LaTeX document. This is useful if you are working in a program such as MATLAB and need to create a report that show the discussion of your results and the MATLAB code you used.

The listings package allows you to type the computer code inside of you LaTeX document. However, a more useful technique is simply to use an ''\include{your_computer_file}'' command to have your computer code inserted at the appropriate point in the LaTeX document. See the following three files as examples of what you can do.

Listing Example #1 (pdf)

Listing Example #2 (pdf)

Listing Example #3 (pdf)

All the Listings files (zip)

English Essays

If you are going to school, you may need to format your papers the way your teacher requires. For example, you may need to have your papers double-spaced, page numbers at the bottoms center of the page and your name in the upper right corner of every page. You may also need to embed a graphic in your essay.

In the following example there is a full demonstration of such styling. In particular, the following packages are used:

An example of how to set up the headers is shown below


% pagestyle tells whether to have headers/foots/page numbers etc
\pagestyle{fancy}

\lhead{English 104-03\\Professor F. Bacon}
\chead{}
\rhead{Bob Bradshaw\\ Due: \today}
\lfoot{Essay on Pseudo-Latin}
\cfoot{}
\rfoot{page \thepage{} of \pageref{LastPage}}

% creates a footer line
\renewcommand{\footrulewidth}{0.4pt}

For the full example, see

English Essay Example (pdf)

English Essay Example with picture (zip)

Graphics

There are many packages for creating graphics within LaTeX but the one that I like is called TikZ. It can be used to draw an amazing variety of graphics. For examples, see the site

http://www.texample.net/tikz/examples/

Of particular use for students is the capability to graph data and functions. While you can certainly graph a function using a program such as MATLAB, there are a couple advantages of creating the graph within LaTeX.

  1. The formatting of the fonts in the graph will match the fonts in the rest of the document. This will give your document a more professional appearance.
  2. By creating the graph within LaTeX, you are do not need to manually keep track of each separate graphic.

After much experimentation, the package I like the most for creating such graphs is PGFPlots, which is based on the TikZ package. The following code plots a set of data. The code only marks simply prevents the data from being connected by a curve.


\documentclass{article}
\usepackage{pgfplots}
\pagestyle{empty}

\begin{document}

\begin{figure}
\begin{center}
\begin{tikzpicture}
\begin{axis}[xlabel=time (in seconds),ylabel=Temperature ($^\circ$C)]
\addplot[only marks,color=blue,mark=*]     
  coordinates {
    (2,-2.8)
    (3,-3.5)
    (4,-3.3)
    (5,-5.1)
    (6,-6.1)
    (7,-8.7)
    (8,-7.2)
   };
    \end{axis}
\end{tikzpicture}
\caption{Imaginary data}
\end{center}
\end{figure}
\end{document}

PGF plot of imaginary data (pdf)

The following code creates the graph of a typical function from a math course. There are two \addplot statements. The first one plots the curve while the second places blue circles at specific points on the curve.


\documentclass{article}
\usepackage{pgfplots}
\pagestyle{empty}

\begin{document}

\begin{figure}
\begin{center}
\begin{tikzpicture}
\begin{axis}
[xlabel=$x$,
ylabel={$f(x)$},
grid=major,
axis x line=middle,
axis y line=middle]
\addplot[color=red,domain=-2:4] {x^2 - 2*x };
\addplot[color=blue, domain=-2:4,samples=7,only marks,mark=o] {x^2 - 2*x};
\end{axis}
\end{tikzpicture}
\caption{A nice parabola, $y=x^2-2x$.}
\end{center}
\end{figure}


\end{document}

PGF plot of a parabola (pdf)

Slide Shows

LaTeX can be used to create slide shows with many of the features of software like Microsoft PowerPoint. As with graphics, there are many packages to do this but I like the one called Beamer. It comes with an extensive manual (300 + pages) and many option for styles and colors.

When using Beamer, each slide is called a frame. The following will create a simple slide show with three frames.


\documentclass{beamer}
\begin{document}
\begin{frame}
	First slide
	\end{frame}
\begin{frame}
	Second slide with math
	\[\int x^2 \, dx=\frac{x^3}{3}+C\]
	and then some text.
	\end{frame}
\begin{frame}
    Some Trig Identities
	\begin{itemize}
	\item $\cos^2 x+\sin^2 x=1$
	\item $\sec^2 x-\tan^2 x=1$
	\item $\csc^2 x-\cot^2 x=1$
	\end{itemize}
	\end{frame}
\end{document}		

The results can be seen at First Beamer Example (pdf)

While the above works nicely, I want to change a few things:

Beamer has many built-in templates allowing you to change the colors and appearance of the slides. The templates are named after cities and the colors are named after animals. To use this, add the following lines.


\usetheme{Berkeley}
\usecolortheme{crane}

To eliminate the navigation symbols at the bottom of the slides, use the command


\setbeamertemplate{navigation symbols}{}

To add a title to each frame, put the title, in braces, in the frame command. These changes are now assembled into the file.


\documentclass{beamer}
\usetheme{Berkeley}
\usecolortheme{crane}
\setbeamertemplate{navigation symbols}{}
\begin{document}
\begin{frame}{My First Beamer}
	First slide
	\end{frame}
\begin{frame}{Some Math}
	Second slide with math
	\[\int x^2 \, dx=\frac{x^3}{3}+C\]
	and then some text.
	\end{frame}
\begin{frame}{Trig is for Kids}
Some Trig Identities
	\begin{itemize}
	\item $\cos^2 x+\sin^2 x=1$
	\item $\sec^2 x-\tan^2 x=1$
	\item $\csc^2 x-\cot^2 x=1$
	\end{itemize}
	\end{frame}
\end{document}		

The results can be seen at Second Beamer Example (pdf)

The results are nice but I think some improvements can be made.


\documentclass[t]{beamer}
\usetheme{Berkeley}
\usecolortheme{crane}
\setbeamertemplate{navigation symbols}{}
\logo{\includegraphics[scale=0.80]{beachlogo.jpg}}

\begin{document}
\begin{frame}{My First Beamer}
	First slide
	\end{frame}
\begin{frame}{Some Math}
	Second slide with math
	\[\int x^2 \, dx=\frac{x^3}{3}+C\]
	and then some text.
	\end{frame}
\begin{frame}{Trig is for Kids}
Some Trig Identities
	\begin{itemize}
	\item $\cos^2 x+\sin^2 x=1$
	\pause
	\item $\sec^2 x-\tan^2 x=1$
	\pause
	\item $\csc^2 x-\cot^2 x=1$
	\end{itemize}
	\end{frame}
\end{document}

Third Beamer Example (pdf)

All Beamer Files (zip)