Department of Engineering

IT Services

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?)
  • Many "add-ons" (in fact, LaTeX is an add-on to TeX)
  • 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 pdfTeX, Version 3.1415926-2.5-1.40.14 (TeX Live 2013)
 restricted \write18 enabled.
entering extended mode
(./foo.tex
LaTeX2e <2011/06/27>
Babel  and hyphenation patterns for english, dumylang, nohyphenation, lo
aded.
(/usr/share/texlive/texmf-dist/tex/latex/base/article.cls
Document Class: article 2007/10/19 v1.4h Standard LaTeX document class
(/usr/share/texlive/texmf-dist/tex/latex/base/size10.clo))
(/usr/share/texlive/texmf-dist/tex/latex/psnfss/times.sty)
No file foo.aux.
(/usr/share/texlive/texmf-dist/tex/latex/psnfss/ot1ptm.fd) [1{/usr/share/texliv
e/texmf/fonts/map/pdftex/updmap/pdftex.map}] (./foo.aux) ){/usr/share/texlive/t
exmf-dist/fonts/enc/dvips/base/8r.enc}
Output written on foo.pdf (1 page, 14173 bytes).
Transcript written on foo.log.

You don't usually need to worry about these details, unless something goes wrong ...

  • " pdfTeX, Version 3.1415926 ..." - this tells you the program that's going to do the eventual processing. The version number converges to pi.
  • "LaTeX2e <2011/06/27> ..." - information about the LaTeX "add-on"
  • "/usr/share/texlive/texmf-dist/tex/latex/base/article.cls" - where the article class file is.
  • "/usr/share/texlive/texmf-dist/tex/latex/base/size10.clo" - where the file to set things up for a 10pt font is (clo stands for 'class option')
  • "/usr/share/texlive/texmf-dist/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/texlive/texmf-dist/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.

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 (see Squeezing Space in LaTeX), 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 
% \usepackage{sfheaders}
% If you don't have the package on your machine, download it
% from http://www.ctan.org/tex-archive/macros/latex/contrib/sfheaders/

% 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
\begin{equation}
x=y\sum_{i=0}^\infty\sin(i)\label{SIN}
\end{equation}

\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.

Sources of Information

What Next

Maybe try