An Introduction to LaTeX for Examination Papers

LaTeX

A LaTeX (pronounced "lay-teck") document is a plain text file with a ".tex" extension, and if you look at the file doesn't look like the finished output at all! It comprises plain text and commands, and it is these commands that tell LaTeX what sort of content you're writing and how you want it to handle it. Commands start with a backslash "\".

Getting LaTeX

For MS Windows there's a free version of LaTeX called MikTeX; this also includes the TeXworks editor. Most Linux distributions will come with LaTeX installed.

Starting with LaTeX

Start by downloading the

Put all these files in the same directory - they will all be used by LaTeX.

If you're using a computer with MS Windows, then you can use TeXworks or TexNicCenter to open the preamble and document template file. Under Linux you can use your favourite text editor (Emacs, vi, kate, gedit, nano etc.).

The basic idea is that you write your text, tell LaTeX what sort of text it is if it's unusual (e.g. tables, equations etc.) using LaTeX commands, and then run LaTeX to produce a beautiful, error-free PDF.

Customising TeXworks

If you're using TeXworks, then I recommend you set it to colour the different parts of your LaTeX document according to whether they're comments, commands or plain text. To do that go to the Format menu, choose "Syntax Coloring" and then "LaTeX".

The Front Page

Comments

The first part of the file has a lot of percentage signs "%" - these are comments in LaTeX, and will be ignored when it produces the PDF. In the example file they are used to explain what each LaTeX command is doing.

Preamble

The first thing this file does is include all the commands from the "preamble.tex" file. This file defines some new commands that are handy for us when making exam questions. The commands are:

It also sets the correct page margins and other useful setup details.

Beginning the document

The next non-comment line tells LaTeX where the real document starts with a "\begin{document}" command. The curly-brackets used here are used by a lot of LaTeX commands when they need arguments. You also need to tell LaTeX where the document ends, and if you scroll to the end of the file you'll see a "\end{document}" command.

You use \begin{...} and \end{...} commands to tell LaTeX how you want it to treat the stuff in between. LaTeX knows that everything between \begin{document} and \end{document} is designed to appear on the page, or control the appearance of things on the page. This is an example of what in LaTeX we call environments.

\FrontPage and LaTeX errors

Straight after the document begins, we have the \FrontPage command. Remember this is not standard LaTeX, we've defined what it does in the "preamble.tex" file. Please note that the capitalisation is important; if you get it wrong then when you try to make a PDF you'll get the error:

! Undefined control sequence.
l.11 \frontpage
               {PHY00001I}
This tells you that LaTeX doesn't know what a particular command is ("undefined control sequence"), the problematic command is on line 11 ("l.11"), and the command in question is "\frontpage" - the problem is that it should have a capital F and P, i.e. "\FrontPage".

The argument to \FrontPage is the module code. Sometimes an exam paper will have several module codes, and these should be typeset on different lines. To do this, we use the special command "\\", i.e. two consecutive backslashes, which tells LaTeX you want a line break. E.g.

\FrontPage{PHY00001I\\ PHY00001G}
would put the code PHY00001G on the line below the code PHY00001I.

The \FrontPage command takes a second argument, which is basically the rest of the page! Like most arguments this starts with an opening curly-bracket, and ends with a closing curly-bracket it's just that in this case there's quite a lot of text in between those two.

Hopefully the rest of the front page is self-explanatory. The \underline command does the obvious thing, and \textbf sets all the text in the curly brackets in a bold typeface. Please ensure you amend:

for your own module!

The Questions

The exam questions themselves are each started with the command \Question, and ended with \EndOfQuestion. You don't need to put the question number in, LaTeX will add that automatically. Similarly sub-questions (like 1 (a), (b) etc.) are started with \SubQuestion, but you don't need to end these. There is also a \SubSubQuestion command which will make e.g. 1 (a) (i), 1 (a) (ii) and so on. There isn't a sub-sub-sub-question!

\Question \SubQuestion \SubSubQuestion Indicate the (200) and (111) planes on a sketch of a cubic crystal. 
This will put
1 (a) (i) Indicate the (200) and (111) planes on a sketch of a cubic crystal. 
in the PDF.

Figures

If you want to include figures (images, graphs, diagrams...) you need to put those pictures in a PDF (for diagrams and other vector pictures) or a PNG or JPG (for images and other rasterised pictures). You then include

\begin{figure}[h]
\begin{center}
	\includegraphics[clip=true,trim=1cm 19cm 6cm 1cm,angle=0,width=0.4\columnwidth]{diagram.pdf}
\end{center}
\end{figure}

This tells LaTeX to put the figure "diagram.pdf" in your document. PDFs are often A4-sized and you'll usually want only a portion of this, so you have to tell LaTeX to "clip" the figure and, in the example here, strip away the left 1cm, bottom 19cm, right 6cm and top 1cm which is what the "trim" does for you. You can rotate the figure by using a nonzero angle.

The "width" value tells LaTeX how wide to make the figure. You can specify this as a length, e.g. "width=10cm", but I find it's often more convenient to specify it as a fraction of the width of the text column. In the example above, the figure is 0.4 of \columnwidth - you could use \pagewidth or \textwidth instead.

The [h] bit after the \begin{figure} tells LaTeX you want the figure to appear "here" -- otherwise LaTeX would try to put it wherever is most aesthetically pleasing, which might not work well for an examination question.

Maths and Equations

If you want to put a little bit of maths in the middle of a paragraph, just enclose it in a pair of dollar signs. For example,

If the variable $x=3$ show that $y=\sqrt{2}$.
will typeset the "x" and "y" in italics (LaTeX's default style for variables) and put a square-root sign over the "2" because of the \sqrt command.

If you don't want something in maths to be italicised, use the \mathrm{} command (it stands for "maths Roman font"), and similarly you can make things bold with \mathbf{}, e.g.

\mathbf{a}^\prime = \mathrm{M}\mathbf{a}
would embolden a' and a, indicating they are vectors, but use a normal Roman font for the matrix M. Note that emboldening something automatically stops the italics.

If don't want the maths in the middle of your text, but want a separate block of equations then there are several ways to do this. The way I recommend is to use the "eqnarray*" environment, which is started with a \begin{eqnarray*} and ended with a \end{eqnarray*}, e.g.

\begin{eqnarray*}
\nabla\cdot\mathbf{B} &=& 0\\
\nabla\cdot\mathbf{E} &=& \frac{\rho}{\epsilon_{0}}\\
\nabla\times\mathbf{E} &=& -\frac{\partial \mathbf{B}}{\partial t}
\end{eqnarray*}
I've illustrated a few special commands in this example:

Matrices and resizing brackets

Yes I know, these should be under "maths" really, but they are a bit different. A matrix in LaTeX can be set out in a maths environment by using another environment - the array environment. It's easiest to see using an example:

\begin{eqnarray*}
\left(\begin{array}{ccc}
1 & 0 & 0\\
-1 & 1 & 0\\
0 & 0 & 1
\end{array}\right)
\end{eqnarray*}
So we've used the array environment inside the eqnarray* environment. The array environment takes an argument, which here is "ccc". This argument tells LaTeX how many columns there are in your matrix, and how you want them aligned. "ccc" means 3 centred columns -- it's one letter for each column, and the letters can be "r", "l" or "c" for right-aligned, left-aligned or centred (there are a few other options as well).

Notice that before I began the array environment I put "\left(" and after the end of the environment I put "\right)". This basically puts a left and right bracket at the start and end of the matrix, as you would normally do. So why not just "(" and ")"? Try it...

When LaTeX encounters a bracket, it puts a bracket - so far, so good! However sometimes the brackets won't be as big as the things their supposed to contain, and then we can use the special \left( and \right) commands. These commands tell LaTeX that the first bracket "(" is the left-hand bracket of something, and the second bracket ")" is the right-hand bracket, and to make the brackets big enough to enclose everything in between. This is vital for matrices and vectors, but is also useful for fractions. You can also use \left[, \left\{ (note extra backslash), \left\vert (a vertical line) and \left\langle (a left angled bracket "<"), and of course the corresponding \right commands (the right angled bracket is \rangle).