A Brief Introduction to LaTeX

by Dr. A. Kay

You're probably reading this because you're expecting to write up a Mathematics project, and it's been recommended that you use LaTeX. This is very different to any word processing that you're used to doing - most of your experience is probably something like Microsoft Word, which is WYSIWYG ("what you see is what you get"), meaning that when you type something, it instantly appears on the screen exactly as it would be represented on the printed page. You apply a bold formatting, and it appears instantly. LaTeX is not like this; it's more like a programming language where you write the program, and, once it's complete, you hit 'compile' and run the finished program. It's also like a programming language in the sense that you have to enter a command that says "make this bit of text bold", and this is where the learning curve for LaTeX comes from; you need to know the commands for all the things that you might want to do.

So, with all of this messing around, you're probably asking why you should bother with LaTeX. The answer is fairly simple - something like Word is really clunky when it comes to equations. Yes, there's an equation editor, but it's horribly slow. LaTeX, once you're used to it, is much, much quicker.

This tutorial is a brief introduction to some of the features of LaTeX. It is not exhaustive. There are all sorts of things that you can choose to do with it, but the important thing is to get used to the basic functionality, and then there are many resources on the web that can tell you about all the bells and whistles that you can add if you want. The way that we're going to do this is that we've mocked up a LaTeX simulator for you to practice with. It doesn't do everything; it could never hope to. But it simulates almost everything that you are likely to need or want for writing up your project. I designed this for firefox, but assume it will work with other web browsers (with the possible exception of internet explorer). Practice with this simulator, see the results instantly, and you'll be a bit more comfortable when you start writing your own LaTeX documents.

What you need

To get up and running in LaTeX, you need three things - LaTeX itself, and a text editor (this is different from a word processor, such as Microsoft Word), and a pdf viewer (such as adobe acrobat reader). You don't need any of these to proceed with the tutorial. You've got a web browser, so that's enough for now, but you'll want to get them set up for when you're ready to start writing your project.

Obviously what programs you choose will depend on what platform you're using. The text editor, in particular, has many options. You could go for something as basic and featureless as notepad in windows, but I wouldn't recommend it. If you find a specific TeX editor, then it will perform syntax colouring, which just adds extra visual clues about what you're doing, and helps find unmatched brackets. It will also interface with LaTeX so that you can compile your document just by clicking a button. I strongly recommend finding a LaTeX editor that performs spell checking. It is amazing how many people don't use this basic feature, and it's usually very obvious in the finished product!

  • Windows: The standard LaTeX compiler is MikTeX. Personally, I used to use WinShell as a free editor with good spell checking capabilities, although I have recently moved over to using the Sublime text editor with the LaTeXTools add-on. It's got more of a learning-curve to get set up and working properly, but one reason to persevere is that if you use a different pdf-viewer, such as SumatraPDF, then when you edit your latex and compile, the file opens at the point you were editing, so you can easily see the effect of your changes. It's also quite good when you need to reference other parts of your document, or your bibliography, and you can't quite remember what you called it!
    The computer labs have TeXnic Center available.
  • Linux: You will almost certainly already have LaTeX installed. If not, go to your package manager and search for latex. You'll probably already have a favourite text editor. If not, Kile is a common choice.
  • Mac: TexShop and Excalibur are the ones that IT provides. You can obtain them from here.

There are 2 more items that are 'good practice', and worthwhile thinking about from the start - choosing to adopt them later makes for much more work!

  • A reference manager, such as a BibTeX database editor. We'll talk more about this later.
  • A version control system such as GitHub or SVN. Using the likes of DropBox is OK, but doesn't offer the full power of version control. For a project, you could get away without using it, but if you're thinking about doing a PhD, it makes a lot of sense!

First Steps

A standard LaTeX document is divided into two parts, a header region and the main document body. There's various bits and pieces that get specified in the header, but we're going to entirely ignore it for now. When you want to start compiling LaTeX documents properly, the best way to get started is to borrow someone else's document (we've got a couple for you to look at). Instead, we're just going to look at the main body of the document.

In a LaTeX document, you point out that you're working on the main body by putting everything between a statement of \begin{document} and \end{document}. In our simulator, on the right, assume this has been done for you. Everything that you enter in the 'main body' part is as if you had sandwiched it between these commands in a LaTeX document.

So, where do you start? A title is a good place. To specify a title, you just write \title{Hello World!}. Try typing that in now (or just click on the bit of code to automatically copy it). You should see your title appear in the bottom right corner as a big, centred heading. In fact, most LaTeX commands are very sensibly named. If you're thinking "I wonder how I do...", think of a simple one word description, put a back slash in front of it, and you've got a fair chance of getting it right!

Your document should only have one title statement, but you can structure it by using sections, subsections and subsubsections. These can either be numbered (default) or unnumbered (add a * after the name of the command). For example, \section{My first section}, \subsection{Some more structure} \subsubsection{Finer detail} and \subsection*{An un-numbered subsection}.

Within each of your sections, you can just start writing text. Write whatever you want (what you should chose to write is not the subject of this introduction). It will come up just as you expect it to, except that starting a new line in a LaTeX file does not start a new paragraph. Instead, you have to do it twice!

LaTeX is very good at taking care of the formatting of your text, but if you want to make certain bits bold, or italic, or underlined, you need to know the right commands: \textbf{this is bold}, \textit{these are italics}, and \underline{this} is underlined. Try it now!

We've now met a lot of simple commands. Things that say "format the block of text within the curly brackets in this way". Other commands apply to larger regions of text, and for these we define environments. These are commands that are started by \begin{xxx} and ended by \end{xxx} so that everything between those two commands is formatted in a certain way. For example, if we want a numbered list, then we use the enumerate environment, and denote the start of each item in the list with the \item command. For example: \begin{enumerate} \item Item 1 can be anything \item Something else \end{enumerate}. Unordered lists work identically, but using the itemize environment. Try changing your numbered list into an unnumbered one.

SIMULATOR LIMITATION: You need to put the sectioning commands on new lines, otherwise it gets confused with bracketing.


Entering maths is where LaTeX really comes into its own. There are several different maths environments, which we won't go through in fine detail right now. We'll work with two. By encasing an equation inside single dollar symbols, that says 'process this bit of maths, keeping it on the same line as the rest of the text', while if you put the maths between two pairs of dollar symbols, the maths is separated onto its own line.

What are we going to put in our equations? It's quite common to need some greek letters. For example, $\alpha$ will create the symbol $\alpha$. All lower case greek letters work the same. If you want a capital greek letter, capitalise the first letter of the command: $\Psi$ creates $\Psi$. These are both examples of doing something within the body of the text. To try something more complex, take a look at $$\int_0^{\pi}x^2 dx=\frac{\pi^3}3$$. This equation appears on its own line, and demonstrates the use of lots more commands. See if you can pick it apart and see what each statement does. You can always modify bits of the code and see what difference it makes. You'll see, for example, the characters _ and ^. What these do is say "take the next character and make it subscript/superscript, respectively". If you want it to be more than just the next character, you enclose the thing that you want to be sub/superscript in curly brackets. So, $\pi^31$ creates $\pi^31$ while $\pi^{31}$ gives what you might have wanted, $\pi^{31}$. If the superscipt and subscript symbols follow some special commands, such as \int or \sum (integral and summation symbols), then the subscript is interpreted as the lower limit, while the superscript defines the upper limit.

Brackets are essential in maths, they help to group terms together, and it's important to be able to tell at a glance what bracket matches with what. So, while you could just write (something in brackets), better practice (at least within mathmode) is to open a bracket pair with \left(, and close it with \right). LaTeX will then automatically resize them for you depending on what they are enclosing: $$\left(\sum_{i=0}^n\frac{\left(x+2\right)^3}{7}\right)^2\leq \frac{n^2(n+2)^6}{49}$$. You can use different bracket types, and you don't have to match brackets of the same type (but each \left must be matched by a \right of some description). If you want to use curly brackets, you have to distinguish these from a LaTeX command by using \{ or \}. If you want double straight lines, use \|, as in $\left\|\vec{x}-\vec{y}\right\|^2$. If you want an empty matching bracket, use \. We'll give an example of why you wight want to do that in the next section. Note that angular brackets use a different symbol to the less than or greater than symbols. You need to write $\left\langle x^2\right\rangle$

Now that you've got the hang of this, you can typeset pretty much anything you want, the only limitation is knowing how to create the symbols that you want. There are many great resources, including:

Numbered Equations

We mentioned before that there are a number of different maths environments. One useful one is \begin{equation}/\end{equation}. These behave like the double dollar equations before, except that the equations get automatically numbered. You can then give each of these equations a unqiue name, using the command \label{equation_name}. That lets you, at other places in your document, refer back to that equation, by using the command \ref{equation_name}. This is much better than using the equation number itself because if you rearrange your document, insert an equation, etc., the whole thing automatically updates itself. For example, A numbered equation:
f:\{0,1\}\mapsto \{0,1\}^4\label{eq:thisone}
We can now refer to Eq. (\ref{eq:thisone}).
It's convention to put the equation number inside brackets. You can actually name and reference all sorts of things in LaTeX like this - sections, figures, tables...

It is also possible to insert multi-line equations (or break a single really long equation over several lines). To do this nicely requires a few extra formatting shortcuts that are the same as when you program a matrix, so let's start by talking about how you write a matrix. Look at the bit of code
x^2 & 1 & 2 \\
3 & 4 & \int_0^1x^2dx
This does exactly what we need. The \begin{array} is obvious: it says "we're going to create an array here". Its argument of {ccc} conveys that there will be 3 columns, and each should be centered. Then you start specifying what you want the entries of your matrix to be. The & symbols denote a marker between two columns, while \\ conveys that we should start a new line. There's no limit to the number of lines you have. \end{array} then conveys that we've got to the end of the matrix data. In fact there are specific matrix commands that include the brackets automatically, but I just remember the one command and add my own as needed.

Arrays aren't just used for matrices, but for other formatting options. For example,
f(x)=\left\{\begin{array}{cc} 0 & x\in\mathbb{Q} \\
1 & x\notin\mathbb{Q}

Observe how we've matched the opening left { with a blank right closing element.

Now that we're familiar the array environment, multi-line equations should be straightforward; just look at an example: \begin{align*}
f(x) &= (x+a)(x+b) \\
&= x^2 + (a+b)x + ab
. This align environment (numbered) or align* (unnumbered) provides a two-column layout, separated by &, and the new lines are specified by \\. By placing the = sign immediately after the &, we get nicely aligned equals signs, which makes the maths look much better.

SIMULATOR LIMITATION: referencing only works for equations.


While you're writing your project, it is important that you accurately acknowledge all of your sources. There are two aspects to this: creating a bibliography, a list at the end of your document that has information on all the papers, and citations at appropriate points in the text, which convey which papers support each statement that you make.

To set up the bibiliography, go to the end of the main body of your text and add \begin{thebibliography}{9} \bibitem{entryname} This is my first bibiliographic entry. \end{thebibliography} You can add more entries into your bibiliography just by putting more \bibitem{} commands inside the thebibliography environment. Just remember to give them each a unique name (the bit inside curly brackets). You might wonder what the {9} is doing in the \begin{thebibliography}{9} statement. That's not something very important. Basically, it conveys the number of digits that LaTeX should expect to be used to repesent the largest reference number. The precise value given doesn't matter, it's only the difference between 9 (one digit) and 99 (two digits) or even 999 that's important.

The \cite{} command then works just like the \ref{} command did before. You place a reference somewhere in the text using \cite{entryname}, and it will automatically figure out the correct number to refer to. You can simultaneously reference several sources by separating their names with a comma, \cite{entryname,secondentry}.

It's up to you to collect all your references together, make sure the information is accurate, that your references are correctly ordered, and formatted according to some convention (there are many different conventions!). However, there are some tools that can help you:

  • BibTeX. You create an extra 'bibtex' file. This contains all the references you've ever looked at. When you compile your LaTeX document, the compiler automatically goes into your BibTeX file and grabs just the information your document needs, orders it for you, and inserts it at the end of your document. So long as you maintain that separate file, life is much easier. Personally, I have found one major downside to this - when you start to collaborate with other people, they need to use the same bibtex file as you, and be willing to update it properly. I stopped using BibTeX because one of my co-authors couldn't cope with it!
  • Zotero. This is what I use, although there are various other products. It integrates with your web browser so that when you read a paper, you just click on a button, and it saves all the information on the paper into a database. There is also an add-on for it called Better Bib(La)TeX that can be set up to automatically output that database as a bibtex file so that you can access it directly from LaTeX. This makes for a very stream-lined work flow.
Theorem environments

A great way to structure your writing is to use a theorem/proof structure. LaTeX doesn't know by default about theorems because there are so many similar structures that you might want: theorem, lemma, conjecture, definition... the list goes on. So, LaTeX lets you use whatever you want, you just have to tell it about what you want to use. We do this in the header section of the document. So, click on "Header information >>" to reveal the header information. It should start as an empty box. Add the command \newtheorem{thm}{Theorem}. What does this do? It defines a new environment called thm, so we can insert into the document \begin{thm}My shiny new theorem\end{thm}. The second argument is what the environment displays. In this case, it'll say it's a Theorem, but you can write whatever you want there. Just try changing it, and see the effect.

The \begin{thm}/\end{thm} provide for the statement of your theorem. You can also include a proof by using \begin{proof}This is a deep and meaningful proof.\end{proof}, but this is optional.

Tables and Figures

Tables are figures are important structural elements in your document. Unfortuntaely, we cannot simulate tables here, but the basic concepts are fairly straightforward. If you want to insert a figure, use the following code:
\caption{This is the caption for my figure}\label{fig:unique}

The structure is fairly self-explanatory. The caption will appear underneath your figure, and you can refer to your figure from anywhere within the document using \ref{fig:unique}. For the \includegraphics statement, the optional argument (inside [..]) can be used to specify several things, but mostly it's used to give the width of the image. You can either experss it as a fraction of the page width, as done here, or you could give a fixed value such as 12cm (this may not be rendered very well in your web browser). The second argument, the file name, specifies the location where the image file is saved. This can either be relative (i.e. file.png for a file in the same directory as the tex file, or ../file.png for a file one directory higher) or absolute. You can see that I referred to an image that I already have stored on my website, 2 folders up, and then in the sub-folder called images. You can use several different image types, although which ones depends on what LaTeX compiler you're using, and possibly what optional add-ons you use. By default, PDFLaTeX handles .pdf, .png and .jpg files.

Tables are a combination of figures and matrices. Take a look at this example:
Mineral & Color \\ \hline
Ruby & red \\
Sapphire & blue \\
\caption{This is the caption for my table}\label{tab:unique}

Once you're inside the tabular environment, it's built exactly like an array, with & to separate the columns, and \\ to separate the rows. The only addition is that we can insert some lines. For example, the command {l|l} says that there wil be two left-justified coumns, separated by a line. If you wanted lines around the whole table, you'd use {|l|l|}. To get horizontal lines, you use the \hline command. If you want to insert maths into a table, you have to enter math mode (i.e. using $ symbols) within each element of the table.

Note that both tables and figures appear as 'floats'. This means that they don't appear exactly where specified in the text, but the LaTeX compiler moves them, usually to be either at the top or bottom of a page, as close as it can to the appropirate place.

SIMULATOR LIMITATION: Tables don't work, and probably never will. Figure floating isn't exactly what LaTeX would do because we don't have separate pages.

Advanced Topics: Custom Commands

Sometimes, we use some combinations of functions repeatedly. For example, maybe we want to refer to something being a real number $x\in\mathbb{R}$, but perhaps you want to be lazy. Instead of having to type \mathbb{R} everytime, you'd prefer just to type \Real. This is where we start using the power of LaTeX's headers to define custom commands. If you haven't already got the header section open, open it now by clicking on "Header Information >>". Now insert the \newcommand{Real}{\mathbb{R}} statement into the header. Now in the main text, you can use $x\in\Real$. Simple, right?

Custom commands can become even more sophisticated by allowing the use of argument. For example, in quantum, you often want to write $|\alpha\rangle$, but it won't always be $\alpha$ that you put in the middle. So, we define a new command using \newcommand{ket}[1]{\left| #1 \right\rangle}, and you use it by typing $\ket{\psi}$. Since this newcommand statement is a little more complicated, let's explain it. The first argument, inside curly braces, is what the name of the new command is going to be. The number in the square brackets indicates the number of arguments to accept (the fact that it's in square brackets rather than curlys is the way the LaTeX distinguishes an optional argument from a compulsory one). Then the last statment, inside the last set of curlies, is the new function definition. It scans exactly like normal LaTeX would, and you use #1 to say "insert the contents of argument 1 here". Similarly, if you had a second argument, you would use #2 to access its value.

It's probably worth asking your supervisor what custom commands they use -- chances are they'll be useful to you, and you won't need to reinvent the wheel.

SIMULATOR LIMITATION: custom commands only apply within maths environments, rather than the whole document as they would in LaTeX.

Advanced Topics: Subtleties

LaTeX has its own idiosyncracies that you slowly pick up as you use it more. Old hands have their pet hates that stand out in an otherwise well prepared document. Some of these include:

  • LaTeX contains 3 different types of dash: -, –, —. To use the different ones, just type one, two, or three minus signs: -, --, ---. Now the trickier question: under what circumstances do you use each of the 3?
  • LaTeX interprets a full stop followed by a space as the end of a sentence. It then makes the space a bigger space than the space between two words! So, if you're using abbreviations, such as Eq. (1), then you don't want to use a full stop followed by a space because the space will be too big. Instead, you write Eq.\ (1).
  • Mathematical functions should not be italicised. To avoid this, many standard functions such as cos and sin have their own predefined functions such as \cos, so that you get $\cos(x)$ instead of $cos(x)$. If you need to typeset your own function name, use the \text command, such as $\text{Tr}(A)$.

Graduating to LaTeX Itself

You've now met all of the important features of LaTeX and should be raring to get going (if you haven't already!). The easiest way to get started is to start from a pre-existing file, such as the three below. Open one of these files in your text editor, and hopefully you'll recognise many of the structures present. The question is how should you turn this file into your final output? There are several output options, but the one that we recommend is PDFLaTeX. If you've got a proper LaTeX aware text editor, there'll be a button or a shortcut to run this command (in Winshell, press F10). It's usually worth doing this at least 3 times before you look at the output because the compiler builds up more information each time (for example, it won't get the numbering of references correct until at least the second time). Then you can view the output pdf (F11 in Winshell). Depending on which editor you're using, if you're using BibTeX, you may need to tell it to run that separately (there's usually an option that you can change so that this becomes unnecessary).

At some point, you'll get so used to using LaTeX that you won't want to go back to using the tools you used to use - you might want to exchange emails with your supervisor, talking about maths, or you might want to prepare a presentation with some mathematical content. When that happens, and if you have Microsoft Office, ask Dr. Kay about ConTeXi, and he can install the current version for you. It automatically converts maths that is typeset using LaTeX in your outlook emails, word documents, and powerpoint presentation into beautiful images.

Header Information >>

Main body of document:
Preview is shown here: