An Introduction to LaTeX

Introduction

LaTeX is a way of writing documents that is more like writing a program than using Word. You write the "source code" using a text editor (Notepad or Word will do) then you "compile" it. You can get "front-ends" of various complexity to help you, but I'll concentrate on the basics here without depending on particular operating systems or extra programs.

So why use LaTeX What are the pros and cons?

• Free! On Macs, PCs, Linux, etc.
• Source files are small and easily transferable. You can see how effects are achieved. Tricks can be passed on.
• Typesetting's better, especially maths (used by CUP) - see Why TeX?
• Solid? (TeX yes, LaTeX no?)
• Font selection is difficult, and there are few fonts
• LaTeX was designed with technical reports very much in mind. It encourages (almost insists on) structured writing and the separation of style from content. This is not the way that many people (especially non-programmers) are used to working.
• Not good at graphics, layouts
• Without a WYSIWYG front end, it's not always easy to find out how to do things.

Simple Usage

Here's a latex file

   \documentclass{article}
\usepackage{times}

\begin{document}
\section{Introduction}
Hello!
\end{document}


If I save it as foo.tex and process it I get the following, which isn't as scary as it looks.

This is pdfeTeX, Version 3.141592-1.21a-2.2 (Web2C 7.5.4)
entering extended mode
(./foo.tex
LaTeX2e <2003/12/01>
Babel <v3.8d> and hyphenation patterns for american, french, german, ngerman, b
ahasa, basque, bulgarian, catalan, croatian, czech, danish, dutch, esperanto, e
stonian, finnish, greek, icelandic, irish, italian, latin, magyar, norsk, polis
h, portuges, romanian, russian, serbian, slovak, slovene, spanish, swedish, tur
(/usr/share/texmf/tex/latex/base/article.cls
Document Class: article 2004/02/16 v1.4f Standard LaTeX document class
(/usr/share/texmf/tex/latex/base/size10.clo))
(/usr/share/texmf/tex/latex/psnfss/times.sty)
No file foo.aux.
(/usr/share/texmf/tex/latex/psnfss/ot1ptm.fd) [1] (./foo.aux) )
Output written on foo.dvi (1 page, 300 bytes).
Transcript written on foo.log.


You don't need to worry about any of this for now, but some details might later be of use

• "This is pdfeTeX, Version 3.141592 ..." - this tells you the program that's going to do the eventual processing. The version number converges to pi.
• "/usr/share/texmf/tex/latex/base/article.cls" - where the article class file is.
• "/usr/share/texmf/tex/latex/base/size10.clo" - where the file to set things up for a 10pt font is (clo stands for 'class option')
• "/usr/share/texmf/tex/latex/psnfss/times.sty" - where the package for the times font is
• "No file foo.aux." - LaTeX looks for a *.aux file when it runs
• "/usr/share/texmf/tex/latex/psnfss/ot1ptm.fd" - a *.fd file is a font description file. This one describes the times font.
• "(./foo.aux)" - an extra file produced for the next latex run. Sometimes several files are produced.

A DVI file is produced, but other formats are usually preferred.

.tex -> (latex)      -> DVI -> (dvips)   -> PS -> (ps2pdf) -> PDF
-> (dvipdfm) -> PDF
-> (latex2html) -> HTML
-> (pdflatex)   -> PDF


Nowadays I usually use pdflatex rather than latex to process files to produce PDF directly.

As you write bigger files you'll need more commands. If you're writing an essay you might not need to know many more: \textit{...} does italics, \includegraphics{filename.jpg} includes graphics (you need \usepackage{graphicx} to make it work), \tableofcontents adds a table of contents. I suggest you look up features as you go along using the resources mentioned below.

LaTeX knows best

Accept LaTeX's funny little ways. It has built-in aesthetics

• The first paragraph of a section isn't indented!
• It will leave space at the end of a page rather than squeeze a section header there.
• It doesn't like squeezing a bit of text onto a page that's mostly graphics

You can change this behaviour if you want, but latex probably knows best.

Front ends

Some free and commercial programs make life easier

• kile is installed on our linux terminals. You still need to type LaTeX code, but Kile has many facilities (templates, wizards, etc) to make it easier - see a screen dump. proTeXt (for Windows) and Scientific Workplace (for Windows - it isn't free) work the same way
• lyx is a WYSIWYG front-end for LaTeX that's getting better all the time. It's installed on our linux servers - see a screen dump
• Overleaf lets you write LaTeX docs and work collaboratively without needing to install anything.

How LaTeX can help

LaTeX is good at maths, automated numbering, and enforcing stylistic uniformity. The following example shows how various types of cross-referencing can be easily maintained.

\documentclass{article}
% It's fairly common to use a sans-serif font for headings
% The following line, if uncommented, does this using a package

% If you want table numbers to be reset in each section, use the next 2 lines
\usepackage{chngcntr}
\counterwithin{table}{section}
\usepackage[font=small,format=plain,labelfont=bf,up,textfont=it,up]{caption}

% The first time you process this document you'll need to process it
% twice to make the references work.

\usepackage{times}
\begin{document}
\tableofcontents
\listoffigures
\listoftables

\section{One}
This document has no tables or figures, but it has the captions for them.
Later you might be interested in table \ref{TABLEB} or in a footnote\footnote{some small print}
\begin{table}[htbp]
\caption{\label{TABLEA}A table}
\end{table}

\section{Two}
Now for some maths
$$x=y\sum_{i=0}^\infty\sin(i)\label{SIN}$$

\subsection{Detail}
We'd better go into detail now
\begin{figure}[htbp]
\caption{\label{FIGA}A figure}
\end{figure}

\subsubsection{More detail}
\begin{table}[htbp]
\caption{\label{TABLEB}Another table}
\end{table}
Earlier in equation \ref{SIN} we set x to a value.

\end{document}


Running pdflatex on this twice will produce this PDF file. Then if you have latex2html installed you can type "latex2html filename" to produce a tree of WWW pages.

What Next

Maybe try

• © Cambridge University, Engineering Department, Trumpington Street, Cambridge CB2 1PZ, UK (map)
Tel: +44 1223 332600, Fax: +44 1223 332662
Contact: helpdesk