LaTeX | Chris Hughes | PCC Mathematics
An introductory page to LaTeX (Last modififed January 14 2013)
|
What is LaTeX?
LaTeX is one of the definitive ways of writing professional quality Mathematical documents; it is a markup language
for the TeX typesetting system. Here are some of the many good things about LaTeX:
- it is open source, and is freely available for all OS
- it handles chapter, section, figure, table, equation, etc enumeration and cross referencing elegantly, efficiently, and easily
- it is easily customizable through the use of packages and newcommands
- LaTeX files are stored as plain text, which makes for small file sizes that are easily search-able, and are as stable as the text editor that you use
- large documents can be built piece-meal, and easily manipulated to change ordering; enumerations and cross-referencing change automatically
- LaTeX emphasises content over form- you spend time typing the content in, and worry about the global appearance once the content is finished
A brief history
As LaTeX users we are all in debt to Donald Knuth (pronounced Ka-NOOTH) and
Leslie Lamport.
Donald Knuth invented the type setting system TeX. Leslie Lamport made LaTeX, which is based on TeX. It is hard to imagine the world of Mathematics without these two heroic contributions.
Donald Knuth invented the type setting system TeX. Leslie Lamport made LaTeX, which is based on TeX. It is hard to imagine the world of Mathematics without these two heroic contributions.
About this page
This page is mainly designed as a 'newbie-tutorial', and does not cover many advanced
features, although some are hinted at in the 'Sample documents' at the bottom of this page.
If you are completely new to LaTeX and have never used it before, I suggest you read and work through
each topic sequentially.
You can read about more advanced features in the Not so short guide to LaTeX (for example), and on many, many other websites.
If you find mistakes, errors, or inaccuracies on this website, then please e-mail me with the details.
You can read about more advanced features in the Not so short guide to LaTeX (for example), and on many, many other websites.
If you find mistakes, errors, or inaccuracies on this website, then please e-mail me with the details.
Resources
You will almost certainly want to go into more depth with all of the features
covered here, so once you want to do so you may like to begin with the following resources
- http://www.latex-project.org/ is the LaTeX home page
- http://ctan.org/ which stands for 'Comprehensive TeX Archive Network has all of the currently officially supported packages with documentation (assuming that they have been provided by the author)
- Not so short guide to LaTeX is an awesome (but encyclopaedic) document
- The Comprehensive LaTeX Symbol List
- http://tex.stackexchange.com/ is a community-based Q&A site
- Wikipedia has some useful pages too
Installation
There are many options for installing a LaTeX distribution, the following directions describe some of the options
available.
Windows users:
Option 1: You can either follow these steps:
Important: Go to Start-> Programs-> MikTeX 2.9-> Maintenance (admin)-> Settings (admin) select either, 'Yes', or, 'No' for the option, 'Install missing packages on-the-fly'. There is a known issue if you leave it as, 'Ask me first'. You can read more here: http://miktex.org/2.8/issues
- Download and install MikTeX
- Download and install Ghost Script (gs864w32.exe) from here
- Download and install Ghostview (gsv49w32.exe) from here
- Download a LaTeX capable text editor such as TeXnicCenter
- Start up TeXnic center; it will ask you for the location of the executable files- navigate to the directory: "C:\Program Files\MikTeX 2.9\miktex\bin"
Important: Go to Start-> Programs-> MikTeX 2.9-> Maintenance (admin)-> Settings (admin) select either, 'Yes', or, 'No' for the option, 'Install missing packages on-the-fly'. There is a known issue if you leave it as, 'Ask me first'. You can read more here: http://miktex.org/2.8/issues
MAC users
- Follow instructions on the MacTeX site; once you've installed it edit text files with your favourite text editor
Linux users
Follow the instructions here: http://tug.org/texlive/acquire-netinstall.html
Your first LaTeX document
To test that you have your LaTeX distribution up and running, we will
first make the 'obligatory' Hello World document.
- Copy and paste the following into your text editor, and save it as myfile.tex
- Compile it using pdflatex Note that performing this operation varies in the different IDEs; it may involve clicking a button that says, 'Build' or 'Tex', or something similar. From the command line the syntax is pdflatex myfile.tex
- View the pdf file.
\documentclass{article}
\begin{document}
Hello world
\end{document}
Comments
You can add comments to your file using the % symbol:
\documentclass{article}
% Comments do not appear in the output!
\begin{document}
% Here we are in the body of the text- this line is a comment
Hello world
\end{document}
Mathematical content (a brief introduction)
Adding Mathematical content will be our first instance of having to usepackage, which
we do in the preamble
Please note that this is the barest of introductions to including Mathematical content. You can read about more advanced features in the Not so short guide to LaTeX, together with viewing the documents in the sample files listed at the end of this page.
\documentclass{article}
% The part of the document before the \begin{document} is called the preamble
\usepackage{amssymb}
\usepackage{amsmath}
\begin{document}
Hello world. Here is my first equation in \LaTeX
\[
f(x)=3x^2+1
\]
Another way of presenting an equation is
\begin{equation}
g(x)= \int_a^x f(t) dt
\end{equation}
or perhaps you prefer no numbering
\begin{equation*}
g(x)= \int_a^x f(t) dt
\end{equation*}
Notice how one equation is numbered, and one is not?
If you want to write an in-line equation, you
can do so like this: $h(x)=e^x \sin(x)$
\end{document}
Note in particular that we have used \[ \], $$
and \begin{equation} \end{equation} to enter into Math mode- you will not be able to enter Mathematical
content without entering the correct mode.
Please note that this is the barest of introductions to including Mathematical content. You can read about more advanced features in the Not so short guide to LaTeX, together with viewing the documents in the sample files listed at the end of this page.
Chapters, Sections, subsections
LaTeX handles all enumerations of Chapters, Sections, and Subsections for you (all of which can be customized)
Most articles are not long enough to require chapters, but if you need to make use of the chapter facility, change the \documentclass{article} line to \documentclass{report} and see what happens.
\documentclass{article}
\begin{document}
\section{In the beginning...}
Some text
\subsection{Subsections have numbers too}
Sub text
\subsubsection{So do Subsub sections}
Sub sub text
\section{Another section}
Now try putting another section at the beginning of this document,
and watch the enumeration change!
\end{document}
Most articles are not long enough to require chapters, but if you need to make use of the chapter facility, change the \documentclass{article} line to \documentclass{report} and see what happens.
\label and \ref
One of the many powerful features that LaTeX possesses is its ability to cross reference dynamically; it
does so through the use of \label and \ref.
Consider the following example
There are a number of things that should be noted here:
Consider the following example
\documentclass{article}
\usepackage{amssymb}
\usepackage{amsmath}
\begin{document}
\section{In the beginning...}\label{sec:myfirstsection}
Some text
Here is my first equation
\begin{equation}\label{myfirstequation}
g(x)= \int_a^x f(t) dt
\end{equation}
and here is my second equation.
\begin{equation}\label{mysecondequation}
h(x)= \int_a^x g(t) dt
\end{equation}
\section{My second section}
We saw in Section \ref{sec:myfirstsection} that we could
label equations such as \ref{myfirstequation}
and \eqref{mysecondequation}
\end{document}
There are a number of things that should be noted here:
- you need to compile LaTeX twice in order to get the cross references correct. The reason for this is that on the first compilation the references are written (and read, if they exist) to the .aux file; on the second compilation, the references are read from the .aux file
- If you move the 'second' section to the beginning of the document, and compile LaTeX twice again, then the enumerations will all change, and the cross references will change automatically
- We have used \ref and \eqref in the above; the only difference is that \eqref puts () around the reference
- All enumerated objects such as Chapters, Sections, Subsections, Subsubsections, Equations, Tables, Figures, etc can all be labelled and referenced in this way, and the cross referencing will always be kept up to date dynamically with very little effort from the user
- The user should (of course) make sure not to use the same \label for more than one object; LaTeX will compile just fine, but will warn you of your mistake
Tables
The following code shows a very simple table construction
The argument after \begin{tabular} which is {r||c|l} specifies that there are three columns, corresponding to the three letters r, c and l. Furthermore, the letters used specify how the content of each column is to be justified
\documentclass{article}
\usepackage{amssymb}
\usepackage{amsmath}
\begin{document}
\centering
\begin{tabular}{r||c|l}
$t$ & $x$ & $y$ \\ \hline \hline
$0$ & $1$ & $2$ \\
$1$ & $3$ & $4$
\end{tabular}
\end{document}
The argument after \begin{tabular} which is {r||c|l} specifies that there are three columns, corresponding to the three letters r, c and l. Furthermore, the letters used specify how the content of each column is to be justified
- the r, c, l stand for
- l: left justified
- c: centered
- r: right justified
- The pipe | specifies that a vertical line should be drawn between columns. If you specify || then a double line will be drawn between columns.
Compiling options
So far we have been using pdflatex, but as soon as we insert figures into our
document this may cease to be appropriate.
pdflatex will continue to work provided that your figures are .png, .pdf or .jpg files. For all other types of picture files you need to change the way you compile your .tex files.
Windows users of TeXnic center can simply change the compiling options to Latex=>dvi=>ps=>pdf, which should take care of everything for you. Windows users of other IDEs will need to follow steps similar to those listed below.
Users on other OS should perform the following operations from the command line (in order):
latex myfile.tex
dvips myfile.dvi -o myfile.ps
ps2pdf myfile.ps
pdflatex will continue to work provided that your figures are .png, .pdf or .jpg files. For all other types of picture files you need to change the way you compile your .tex files.
Windows users of TeXnic center can simply change the compiling options to Latex=>dvi=>ps=>pdf, which should take care of everything for you. Windows users of other IDEs will need to follow steps similar to those listed below.
Users on other OS should perform the following operations from the command line (in order):
latex myfile.tex
dvips myfile.dvi -o myfile.ps
ps2pdf myfile.ps
Graphics
Please make sure to read the Compiling options section before reading this section.
LaTeX can handle some of the popular image formats, but prefers to deal in .eps files; most Mathematical software will allow you to export directly to .eps.
Note that you will need to include the graphicx package in your preamble
LaTeX can handle some of the popular image formats, but prefers to deal in .eps files; most Mathematical software will allow you to export directly to .eps.
Note that you will need to include the graphicx package in your preamble
\usepackage{graphicx}
-
.eps
\centerline{\resizebox{80mm}{!}{\includegraphics{mypicture.eps}}} -
.bmp
\centerline{\includegraphics[width=3cm,height=4cm]{mypicture.bmp}} - pdflatex will also handle .jpg and .png
Floating objects
Tables and Figures are examples of 'floating' objects; the default settings are to let LaTeX decide
where to put them - this can be a good or bad thing. Fear not, there are ways to stop them
from floating if you are unsatisfied with their placement (see for example the nonfloat package)
\begin{table}[!h]
\centering
\begin{tabular}{r||c|l}
$t$ & $x$ & $y$ \\ \hline \hline
$0$ & $1$ & $2$ \\
$1$ & $3$ & $4$
\end{tabular}
\caption[This bit goes in the list of tables]{My first table}
\label{myfirsttable}
\end{table}
My first table is shown in Table \ref{myfirsttable}
Here is a floating figure
\begin{figure}[!h]
\centerline{\resizebox{80mm}{!}{\includegraphics{mypicture.eps}}}
\caption{$\vec{r}(t)$}\label{fig:motivation}
\end{figure}
- the option [] immediately after the \begin{table} and \begin{figure} can be either
- h: here
- t: top
- b: bottom
- p: on its own page
- we label and reference using the same \label \ref scheme that we described previously
- the caption command has an optional argument which is given in [], which specifies the caption that would be used in the \listoftables (more on this later)
\newcommands
Let's say that you're writing a document that contains a certain constant that will be used
repeatedly throughout the document. For the moment, let's say that the constant is &alpha (alpha).
One option is for you to type $\alpha$ every time you wish to reference this constant. However, if you decide to change the name of your constant, to say &beta (beta), then you would need to change every occurrence of $\alpha$ to $\beta$, which is a exercise in tediousness. Another option is to use LaTeX's \newcommand feature.
Obviously this is a very fabricated example; a more useful \newcommand example is
If you wish to overwrite an existing LaTeX command, then you use the syntax \renewcommand. You can also make \newcommands that take input arguments, as in \com above.
One option is for you to type $\alpha$ every time you wish to reference this constant. However, if you decide to change the name of your constant, to say &beta (beta), then you would need to change every occurrence of $\alpha$ to $\beta$, which is a exercise in tediousness. Another option is to use LaTeX's \newcommand feature.
\documentclass{article}
\usepackage{amssymb}
\usepackage{amsmath}
\usepackage{amsfonts}
% define \newcommmands in the pre-amble
\newcommand{\myconstant}{\alpha}
% define symbol for real numbers
\newcommand{\R}{\mathbb{R}}
\begin{document}
We define our constant to be $\myconstant = \frac{\pi}{3}$.
What if we refer to our constant throughout the text?
$\myconstant$$\myconstant$$\myconstant$$\myconstant$
Oh, and here's the symbol for real numbers, $\R$.
\end{document}
Obviously this is a very fabricated example; a more useful \newcommand example is
\newcommand{\com}[1]{\iffalse{#1}\fi}
This allows you to comment sections of text quickly and efficiently
\com{
This text was originally going to be used,
but I decided to comment it out in one go
using my newcommand.
}
This is the new text that replaced my commented text.
If you wish to overwrite an existing LaTeX command, then you use the syntax \renewcommand. You can also make \newcommands that take input arguments, as in \com above.
toc,lof,lot
When writing a document of some length that has multiple sections, subsections, tables, figures, etc, it
may be appropriate to have a table of contents, a list of figures, and list of tables. LaTeX does all
of this for us using the following commands (respectively)
\tableofcontents \listoffigures \listoftablesAs in our discussion concerning \label and \ref we need to compile LaTeX multiple times
- once to write the aux file (which contains references, headings, etc)
- once to generate the toc from the aux file
Packages
There is a myriad of packages available to help you customize your document; we have already
encountered the amsmath and graphicx packages. It
would be impractical to list every available package, so instead we list a few here that may be useful
to the reader. In each case it is suggested that you see the package documentation for further details, all
of which are available at http://ctan.org/
As you experiment with LaTeX more and more, you may come across a package that is not installed on your distribution. Once you have obtained a copy of the package, you will need to copy the package into a directory where LaTeX knows to find it; this will vary depending on your OS, but typical directories are
- geometry
this package is used to set the page dimensions, for example\usepackage[textheight=25cm, left=3.15cm,right=2.15cm, top=2.5cm, footskip=1.5cm]{geometry} - fancyhdr
this package is used to set headers and footers, for example\usepackage{fancyhdr} \fancyhf{} \fancyhead[r]{CMH} \fancyhead[l]{Math 93} \fancyhead[c]{Portland Community College} \rfoot{\thepage} - hyperref
this package makes all cross references into hyperlinks- truly beautiful- and it also allows you to set hyperlinks to websites\usepackage{hyperref} \hypersetup{colorlinks=true, linkcolor=blue }
As you experiment with LaTeX more and more, you may come across a package that is not installed on your distribution. Once you have obtained a copy of the package, you will need to copy the package into a directory where LaTeX knows to find it; this will vary depending on your OS, but typical directories are
- Windows: note that MikTeX might be able to locate and install the package for you- if not, then you might try putting it here: C:\Program Files\MikTeX 2.9\tex\latex
- Linux: /usr/share/texmf-texlive/tex/latex
Cleaning up
You will notice that when you compile your document (myfile.tex say ) LaTeX produces a number of output files. Once
you have finished working on your document and are ready to print/post/submit it, then the chances
are that you will just want to keep myfile.tex and myfile.pdf
You can read about the various output files on pages 13-14 of the Not so short guide to LaTeX
Once you are ready to delete the output files such as myfile.aux, myfile.log, myfile.dvi, etc then you can either go through and delete them one by one, or you can write a batch/script file to do it for you. e-mail me if you are interested in writing a batch/script file for this purpose. Note that your IDE may have an option to clean up.
You can read about the various output files on pages 13-14 of the Not so short guide to LaTeX
Once you are ready to delete the output files such as myfile.aux, myfile.log, myfile.dvi, etc then you can either go through and delete them one by one, or you can write a batch/script file to do it for you. e-mail me if you are interested in writing a batch/script file for this purpose. Note that your IDE may have an option to clean up.
Sample documents
The following documents are designed to showcase some of the things that you can
achieve in LaTeX; please note that they are by no means comprehensive.
| .tex file(s) | .pdf file | Comments |
|---|---|---|
| Integration tips.tex | Integration tips.pdf | This document has some equations that may help get you started |
| instructions.tex | instructions.pdf | This is an instruction manual I made for a Placement Test I wrote. In particular note the use of the packages in the preamble. |
| Sample exam.tex | Sample exam.pdf | An exam that uses \documentclass{exam} class file. Note in particular that the points are added dynamically. You can do a lot more with this class- this file is very simplistic. |
| presentation.tex | presentation.pdf | Note the use of the beamer class. To get full affect, maximize the pdf document to full screen. |
Further things to explore
Here are some other things that you may like to explore
- bibtex
put all of your references in a file called mybib.bib (for example), then include it in the main document using \bibliography{mybib}. After you have compiled your document (myfile.tex say), use bibtex myfile.tex - makeindex
allows you to create an index for the document - subfigure
allows you to set Figure 1a), 1b), etc - pstricks, xypic
useful for drawing pictures within LaTeX- there is something of a learning curve, but it's well worth it - multicol
allows you to typset in multiple columns - pgfplots
allows you to draw Mathematical functions within LaTeX