A Very Short Introduction to LaTeX

This is a very brief introduction to using LaTeX on a Mac. It covers just enough to allow you to create simple documents. If you continue to use LaTeX it will pay to check out some of the references listed on the CC Scientific Computing Resources page.

What is LaTeX?

LaTeX is a typesetting program that is very good at producing scientific and mathematical documents. It's based on TeX which was created by Donald E. Knuth. Knuth is a computer scientist who was dissatisfied with the quality of mathematical typesetting tools available at the time. TeX works much like a computer language like C. The writer creates a source file which is "compiled" by TeX into a printable document. TeX commands are somewhat obscure so Leslie Lamport wrote LaTeX which simplifies creating the source files. LaTeX works just like TeX — you write a source file which is converted by LaTeX into a printable document.

LaTeX is not a WYSIWYG editor like MS Word. A LaTeX source file looks nothing like the final document. It looks more like a program. So, why use it? After all MS Word has a nice GUI interface and what you see IS what you get. There are several reasons, but the greatest advantage is the ease of creating mathematics expressions and incorporating graphics. After you have some experience with it, it is actually easier and faster to use than the MS Word equation editor. It is also the preferred way to submit articles to journals for review and publication in astronomy, mathematics, and physics.

Using TeXShop

Originally LaTeX was a command-line-driven program. It was, after-all written, before Apple and Microsoft existed. But, things have changed. There are a number of graphical user interfaces available for both Windows and Mac interfaces. We will use an open source implementation called TeXShop that runs on a Mac. TeXShop has a built in text editor and can take your LaTeX files and produce standard PDF files.

When you launch TeXShop it will present you with a new text document window. After typing your LaTeX input in this window, clicking Save... will bring up the standard Mac dialog box to allow you to save your document. Your LaTeX document is just an ASCII text document, but LaTeX expects it to have a .tex extension. Select LaTeX in the second button at the top of the editor then click Typeset to create a PDF document.

The text below produces about the simplest LaTeX document you can imagine. Type it into a TeXShop document and try it out.

\documentclass{article}
\begin{document}

This is the simplest \LaTeX\ document I can create. \LaTeX\ ignores single
carriage returns. It just treats them like spaces. A blank line triggers a
new paragraph.

Here's the new paragraph. Notice that \LaTeX\ doesn't put a blank line
between paragraphs. It just indents paragraphs. Notice also the special 
notation $\backslash$LaTeX$\backslash$ creates the fancy looking \LaTeX\ logo.

\end{document}  

TeXShop is quite easy to use. Most of the menus are self explanatory. See the Help menu if you need it. TeXShop has a spell checker. Look in the Edit menu.

Structure of a LaTeX Article

The first line in a LaTeX document is the \documentclass{} command. LaTeX accepts all kinds of documents from books to letters. We will only use the article document class. The \begin{document} and \end{document} commands set off a LaTeX environment. LaTeX environments allow LaTeX to display different kinds of information. For example, the math environment allows you to typeset equations. One or more math environments can be set inside the document environment. More on that later. Notice how every LaTeX command is set off by a "\".

LaTeX takes care of formatting and structuring your document for you so you can concentrate on writing. Click on ShortPaper.tex to see a LaTeX document that generates a very short scientific paper scientific paper. This document illustrates many of LaTeX' most useful features. Here's a short summary of those features.

Download ShortPaper.tex and process it using TeXShop. Play with some of the commands and see if they have the desired effect. Make sure you understand how the math commands work. This is where LaTeX shines.

Using the LaTeX Panel

Select TeXShop's Window → LaTeX Panel... menu item. This will present a window with a collection of mathematical and other symbols at the top. Just click on a symbol to place it at the location of the cursor in your document. The bottom of the window has buttons to place environment and other commands at the cursor. This panel is very handy if you don't know the name of the symbol or remember the syntax for an environment. Play around with the LaTeX Panel commands to see how they work.

Including Graphics

The LaTeX figure environment in conjunction with the \includegraphics[]{} command is handy to display graphics in your documents. In order to use the \includegraphics[]{} command you must load the graphicx package first by including the line:

\usepackage{graphicx}

just below the \documentclass{} command at the beginning of your document. You can create the figure environment in the usual way by using the \begin{figure} and \end{figure} tags. The LaTeX example below shows how you could use the figure environment to place a graph in your document.

\documentclass{article}
\usepackage{graphicx}
.
.
.
\begin{figure}[htbp] 
   \centering
   \includegraphics[width=4in]{GraphFileName} 
   \caption{Figure caption text for the graph.}
   \label{fig:Graph}
\end{figure}
.
.
.

The [htbp] option on the \begin{figure} tag tells LaTeX where to place the figure in the document. The h stands for here, t for top, b for bottom, and p for on a new page. The order of the letters determines the preference for the location of the figure. In this example, LaTeX will first try to place the figure at the location of the figure environment in the source tex document. If there isn't enough room it will try to place it at the top of the page. If it can't place it at the top it will try the bottom and if there's no room it will put it on a separate page.

The \centering command centers the graph on the page. The \caption{} command places a nicely formated caption at the bottom of the figure.

The \label{} command works the same way it does for equations. You can use \ref{} to substitute the figure number where ever the \ref{} appears. This means if you change the order of the figures you don't have to change any figure numbers.

The \includegraphics[width=4in]{GraphFileName} command actually places the graphic in the document. In this case LaTeX will scale the graphic to be 4 inches wide. You can make it any width you like. On the lab Macs, if there is no extension on GraphFileName, the graphicx package will assume that it is a PDF file with the .pdf extension.

You can try this out yourself by downloading GraphDemo.zip. This archive contains a sample tex document, a graph in PDF format, and the data and gnuplot script that were used to create the graph.

The commands above will get you started, but there is a lot more to learn about both TeXShop and LaTeX. Checkout the TeXShop Help menu to learn more about TeXShop.


WYSIWYG
what you see is what you get