TFM:LaTeX

From ProgSoc Wiki

Jump to: navigation, search

Contents

The 5 or 60 Minute Guide to LaTeX

Matthew Beauregard and Murray Grant

What is LaTeX?

LaTeX is a macro language to make writing documents easy. The design principle is that as a writer you should be concerned with your document's content and structure, and leave to your computer things like choosing font sizes, numbering headings and lists, tracking cross-references, formatting equations and other drudgework that you can fob off to the machine.

LaTeX[1] contains the rules for attractive and readable typesetting developed over hundreds of years and distilled into the form of a program. You provide the text and the images, LaTeX will provide the kerning, leading, pagination, hyphenation, whitespacing, figure placement and everything else that professional typesetters fuss over when given the opportunity to produce a well-set book (an increasing rarity these days).

You can learn enough LaTeX to write simple documents and reports in only a few minutes. The subsidiary goal of this chapter is to teach you enough LaTeX to contribute to TFM.

What is LaTeX, really?

LaTeX is a typesetting language, so it is like HTML. But it is a compiled typesetting language, so LaTeX is also like C. The source file (which is what you will deal with) is a .tex file. The TeX file format is similar to HTML. You type your text and apply tags to mark it up. The biggest difference between TeX and HTML is that TeX is designed for documents that will be printed on paper, while HTML is aimed at the computer screen.

Before this author[2] was forced to learn LaTeX, it had a certain stigma about it. It was something that only an elite few could learn. In actual practice, it is not much harder than HTML and far more powerful. If you know the basics of HTML, you won't have much difficulty in learning LaTeX.

Quick Start

LaTeX will be compared with HTML through this document since most people have some degree of familiarity with HTML.[3] Before starting a LaTeX document, let's look at how tags work.

Elements

HTML has inline elements and block elements. Inline elements (such as <hr>) have some immediate effect. Block elements (such as <table>) enclose other tags and text and affect their whole contents. LaTeX has commands and environments, which are analogous.

Starting and Ending a Document

In LaTeX, as in HTML, your whole document is enclosed by a block element/environment, and you have a header region at the top called the preamble where you can set some global options for the document.

In HTML:

<html>
<head>
...header...
</head>

<body>
...body...
</body>
</html>

In TeX :

\documentclass[a4paper,12pt]{article}
...preamble...

\begin{document}
...body...
\end{document}

Because LaTeX is designed for typesetting paper documents, you can specify options setting the paper size. You also specify what type of document you're editing (the {article} part). This will determine what levels of headings are available to you and the layout of the finished document.

Entering Text

The LaTeX above is enough for you to start writing. As in HTML, you can type plain text straight in.

This is plain text in HTML.
Even though there is a hard break at the end of the previous line, 
this will still be on the same line.
<br>This will be on a different line as there was a break tag.
<p>This is in a different paragraph.

This is plain text in LaTeX.
Even though there is a hard break at the end of the previous line, 
this will still be on the same line.\\
This will be on a different line as there was a new line command.

This is in a different paragraph.

LaTeX paragraphs are produced by a blank line. There is no new paragraph element as in HTML.

Compiling

As you learned in the introduction, LaTeX is a compiled language. The TeX files you've been dealing with are the source files (just like C or Java source). To view and print your document you must compile them.

The LaTeX compiler is called, funnily enough, latex. It is on ProgSoc and FEIT computers. From the TeX source, you can create a variety of final outputs, such as .dvi (DeVice Independent), .ps (PostScript) and .pdf (Adobe's Portable Document Format). If you use the latex compiler, you will get a .dvi file as your output.

Once compiled, you can view your resulting .dvi file in xdvi on a UNIX machine or MiKTeX on a Windows machine. There is also a log file created that will show you any errors or warnings that occured during the compile[4].

[~@fray]\$ latex tfm-latex.tex
This is TeX, Version 3.14159 (C version 6.1)
(tfm-latex.tex
LaTeX2e <1995/12/01> patch level 2
Hyphenation patterns for english, german, loaded.
(/opt/tex/texmf/tex/latex/base/report.cls
Document Class: report 1995/12/20 v1.3q Standard LaTeX document class
(/opt/tex/texmf/tex/latex/base/size12.clo)) (tfm-latex.aux)
Chapter 1.
[1] [2] [3] (tfm-latex.aux) )
Output written on tfm-latex.dvi (3 pages, 4220 bytes).
Transcript written on tfm-latex.log.
[~@fray]\$ 

DVI isn't the world's most popular format and so you will want to transform the DVI file into a PostScript document, using dvips:

[~@fray]\$ dvips tfm-latex.dvi -o tfm.ps
[~@fray]\$ 

You can then produce a PDF to send to all your Windows-using friends:

[~@fray]\$ ps2pdf tfm.ps tfm.pdf
[~@fray]\$ 

Slow Start

Once you've learned to write and compile a simple LaTeX document, refer to this section for some of the formatting and layout commands you might want to use.

Inverted commas, dashes, ellipses

LaTeX is a typesetting language, so it has to provide ways for you to use things like typographer's quotes, dashes and spaces, but without using any symbols that can't be found on your keyboard and/or the ASCII character set.

To ensure that your inverted commas 'face' the right way, use ` and `` for opening and ' and '' for closing. Don't use " unless you actually want an ugly double inverted comma that doesn't face in either direction.

If you want an ellipsis (...) don't write ... , write \ldots -- ellipses are spaced closer than three full stops in a row are. As with all commands, if you want a space after your ellipsis it must be a backslash-space.

In LaTeX, - is a hyphen, -- is the en-dash (--) and --- is the em-dash (---). Now that you can use the correct punctuation for the job, only use a hyphen for hyphenating!

Document class

In the example above we used the article document class. This is good for things that are short enough not to need multiple chapters. The report and book classes let you have chapters and parts. The slides class generates presentation slides. There is a letter class for producing (mail-type) letters.

Headings

There are seven levels of headings available in LaTeX. These are commands: to start a new section, for instance, write

\section{Next section}

The headings are, from highest level,

\part
\chapter
\section
\subsection
\subsubsection
\paragraph
\subparagraph

It is uncommon, and possibly indicative of poor structure, to use heading levels lower than \subsubsection.

LaTeX will automatically number your headings. If you don't want a particular heading to be numbered use the *-form of that command. That is, you could write

\section*{Acknowledgments}

Footnotes

We will next examine footnotes, because of their vital importance to TFM. \footnote is a command, and its argument becomes the footnote text.

Take care to bathe once a day.
\footnote{But if you're bathing more often please, please, don't cut back.}

Latex-footnote.jpg

Footnotes can be placed anywhere in your plain text. If you're footnoting at the end of a sentence, put the footnote command after the full stop -- it just looks better.

Putting footnotes in the arguments to commands, such as section headings, exposes a complication in LaTeX that would require a lot of space to cover properly. Sometimes it'll work, sometimes it won't. This is true of commands other than \footnote, too. If it doesn't work but you need it to, read about fragile commands in a more expansive LaTeX reference.

Lists of things

LaTeX has three types of list that you'll use frequently.

\begin{itemize}
 \item Hoopy
 \item Frood
 \item Knows where towel is
\end{itemize} 
  • Hoopy
  • Frood
  • Knows where towel is
\begin{enumerate}
 \item Sunshine,
 \item Lollipops, and
 \item Rainbows
\end{enumerate}
  1. Sunshine,
  2. Lollipops, and
  3. Rainbows
\begin{description}
 \item[MN] motoneuron
 \item[EIN] excitatory interneuron
 \item[IN] inhibitory interneuron
\end{description}

MN motoneuron

EIN excitatory interneuron

IN inhibitory interneuron

Changing type style

Firstly, LaTeX provides some content-oriented commands that have the effect of changing type style. \emph{} is used to emphasise something, and sets it in italics. You can nest \emph{}s; if you emphasise something in a region of text that is italicised, it will be set in Roman and vice versa.

If you really just want to set the typeface,[5] you can do that in two ways. Firstly, here is a list of styles available:

\rm Roman
\it Italics
\em Emphasis (toggles between \it and \rm)
\bf Boldface
\sl Slanted
\sf Sans serif
\sc Small caps
\tt Typewriter text

These are declarations and affect all the text following them until the end of the environment they appeared in, or the end of the scope. You define a scope using { } as in C:

whence he intoned, {\sc I am the angel of death.} whence he intoned, I AM THE ANGEL OF DEATH.

All the declarations have equivalent command forms that affect only their arguments:

\textsc{The time of retribution is at hand.} THE TIME OF RETRIBUTION IS AT HAND.

Typesetting program text

If you only want to set a short command or a little bit of output, and it doesn't have any backslashes or reserved symbols in it, you can write

{\tt ls -laR}

If it does, write

\verb|cd ~user; touch \*|

Unlike all other commands, the argument to \verb-\verb- is not enclosed in braces. It is instead bounded on both sides by the same non-reserved symbol of your choice. In the example we chose |, which is the typical choice... except of course when the text that you're setting has a | in it.

Both of these options will set the command or output inline with your plain text. For longer texts the \verb command has an analogous \verbatim{} environment:

\begin{verbatim} 
 
 # ln -s /dev/null /var/spool/mail/user
 # echo "echo 'You have new mail.'" >> ~user/.bashrc
 # chmod -R 000 ~user/*
 
\end{verbatim}

The output will appear in typewriter text style, with spacing and linebreaks exactly as you wrote. In the \verbatim{} environment backslashes and braces lose their usual meaning (that is, LaTeX commands have no effect and are typeset exactly as written). If you need something like \verbatim{} but in which LaTeX commands still work, put \usepackage{alltt} in your preamble and use the \alltt{} environment.

Tables

Tables can be very complex in LaTeX, but this simple example might be enough for you to start with.

\begin{tabular}{r|l}
 \hline Position & Type \\
 \hline 1        & motoneuron \\
        2        & excitatory interneuron \\
        3        & contralateral inhibitory neuron \\
        4        & lateral inhibitory neuron \\
 \hline
\end{tabular}

Latex-table.jpg

The second argument to the tabular environment, which in this case was {r|l}, specifies the columns and their format. l gives a left alignment, c centering and r right alignment. | will set a vertical line between two columns. \hline sets a horizontal line; refer to the example for usage.

Surprises

Commands inline with text

LaTeX is a macro language; if you've ever programmed in C, imagine for a moment what it'd be like to write your entire program using only #defines. That's the basic flavour of writing in LaTeX and it results in some strangenesses that you need to be aware of.

If you write a command without arguments (without braces) LaTeX finds the end of that command by looking for a non-alpha character. If that character is a space, it will be consumed and the output of the command will be hard up against the next word of your document. To end the command and also write a space, use a backslash-space. If the character is a piece of punctuation it won't be consumed. So, write

\LaTeX\ is, not \LaTeX is,
but
\LaTeX, not \LaTeX\,

Latex-is.jpg

Most symbols have meaning

Some symbols have meaning just by themselves: % begins a comment, $ enters math mode for equation writing, & is used as a delimiter for table cells and other things, # is the argument placeholder, _ is reserved for something non-useful. If you need to use one of these symbols in your document, put a backslash in front of it.

Some symbols have special meaning when backslashed. Examples are \, and \@, which insert different-sized spaces.

A single backslash is the symbol to begin a command. A double backslash starts a new line or a new table row. If you need to write an actual backslash use \verb|\|.[6]

You don't need to know all the symbols, you just need to resist the temptation to put backslashes in front of every bit of punctuation out of reflex. Using a backslash-comma after another LaTeX command is a common mistake; instead of a comma you will get a very thin space.

Cross-referencing

Cross-references are the bane of technical writers everywhere. If you insert them as you write you need to update them all whenever you add a section. If you leave it all to the end it's even more of a nightmare. But now you're using LaTeX, your computer can do it for you.

Use \label{} to define a label that you can refer to elsewhere in your document. Then use \ref{} to insert that label's current section, table or figure number.

\section{Emacs: not just an editor but a lifestyle choice}
...
Emacs (see Section \ref{sec:emacs}) has excellent support for
editing LaTeX.

Because you can label sections (or subsections, etc.), figures, tables and equations, it's usual to prefix your label names with sec:, fig: and so on. This is helpful but not required.

Bear in mind that \ref{} will only insert the actual number that corresponds with the label; it's up to you to write Section, § (\S), Table or whatever other identifier goes with that number.

Resolving cross-references

LaTeX will often need two passes through your document to get the numbering of your cross-references sorted out. It will tell you in its output if you need to run it again, but most people are in the habit of just running latex && latex all the time.[7]

Graphics

Images in LaTeX are a pain. Here's a recipe that's almost guaranteed to do what you want without your having to understand too much of what's going on.

First get your TIFF-, JPEG- or whatever-formatted image and resize it in an image editor until it's a "suitable" size, when zoomed to 100% on your screen. Then run

convert -density 150 image.jpg image.eps

density specifies the DPI of the generated PostScript file (which is the only graphics format LaTeX supports). We choose 150 because dvips will by default typeset your text at 150 DPI and your image will thus appear at "actual size."[8]

Next, in your document's preamble (the part between \documentclass{}<\tt> and <tt>\begin{document}), write

\usepackage{graphics}

and wherever you want your image to appear write something like

\begin{figure}[h]
 \centering
 \includegraphics{image.eps}
 \caption{African swallow in full flight}
 \label{fig:africanswallow}
\end{figure}

Strictly the only part of that you need is \includegraphics{}, but for technical writing at least you'll want to have all your images set as figures, with automatically-numbered captions and labels for cross-referencing.

As part of LaTeX's desire to make your document look its best, it can reposition your figures so that they don't break up the flow of the text or end up on an otherwise-empty page. Use the figure environment's optional argument to hint to LaTeX where you want the figure placed: h for 'here', t for 'top of page', b for 'bottom of page' and p for 'on a page just for figures.'[9] The default placement is [htbp].

Lists that LaTeX can generate for you

\tableofcontents
\listoftables
\listoffigures

As with cross-references, you may need to run LaTeX twice to get the numbering correct.

Farther reading

  • If you're using LaTeX for any decent-sized task like an assignment, you'll spare yourself a lot of trouble if you get the standard manual LaTeX: A Document Preparation System by Leslie Lamport, author of LaTeX. If you're writing a thesis or other major work you should also get The LaTeX Companion by Goossens, Mittelback and Samarin, which describes all of the packages and environments in the standard distribution and others that you can optionally install.
  • CTAN -- the Comprehensive TeX Archive Network (http://www.ctan.org/) contains thousands of extensions to LaTeX that you can download and use. Anything you might want to do -- letters, printed labels, music score setting -- has probably already been done and turned into a package so you don't have to fiddle.

  1. LaTeX is a series of document-related extensions to TeX, which is the actual typesetting package. For simplicity we will describe the whole bundle as LaTeX and not make the distinction.
  2. Murray Grant
  3. If not, try the HTML chapter.
  4. A 200 page thesis, or for instance a TFM, may have lots more warnings than will fit on your terminal
  5. A typeface is a particular style of lettering, such as Times New Roman. A font is the digital analogue of a rack of lead slugs with letters on them: Times New Roman Medium 8pt. Since we're talking typography here we'll use the terms that typographers use.
  6. The level of recursion required to typeset the command that typesets a backslash came this close to breaking my brain...
  7. latex && bibtex && latex && latex for BibTeX users.
  8. It is possible to resize your image by increasing the density setting, rather than scaling it down in an image editor. This may improve the quality of output on high-resolution devices. It also may make your images look horrible if you output to PDF.
  9. well, figures and floating tables.
Personal tools