Table of Contents
LaTeX is a type-setting software suite that allows you to produce exceptionally elegant documents, letters, posters and presentations. The beauty of LaTeX is that is separates the content from the style, freeing you from the hassle of formatting the content as you go.
Overview
This is by no means a comprehensive guide to LaTeX, its simply a collection of notes, tips and tricks that I've used over the years and find useful. I would highly recommend the book Guide to LaTeX by Kopka & Daly as an indispensible reference. I've two copies, one for work and one at home and use it all the time. The same information can be found on the net, but in my experience a lot of the LaTeX pages are hard to find.
Packages
Packages provide additional functionality and in general help make complex things simple. Which packages are to be used in a given document are defined in the pre-amble, and naturally you can customise which you want to use for each document, but I'm pretty lazy so tend to re-use the same pre-amble in most documents and include the following packages…
- snippet.latex
\usepackage{alltt} \usepackage{amsfonts} \usepackage{appendix} \usepackage{booktabs} \usepackage{color} \usepackage{fancybox} \usepackage{fancyhdr} \usepackage[a4paper,textwidth=17cm,textheight=23cm]{geometry} \usepackage{graphicx} \usepackage{hyperref} \usepackage{longtable} \usepackage{lscape} \usepackage{multirow} \usepackage{pdflscape} \usepackage{rotating} \usepackage{tabularx} \usepackage{underscore} \usepackage{wasysym} \usepackage{todonotes}
Pagelayout
By far the easiest way to specify the page layout is by using the geometry package which allows an overall style to be applied and smartly handles borders, headers and footers. You can the optionally specify the other parameters.
- snippet.latex
\usepackage{geometry} \geometry{a4paper,textwidth=17cm,textheight=23cm} % \paperwidth 21.0cm % \paperheight 29.7cm % \topmargin 0cm \headheight 1cm \headsep 0.5cm % \textheight 25cm % \textwidth 18cm % \oddsidemargin 0cm % \evensidemargin 0cm % \footskip 2cm
Landscape Pages
Normally you can rotate a page to be landscape (as oppossed to portrait) by simply using…
- snippet.latex
\usepackage{lscape} % Standard page rotation \usepackage{pdflscape} % Use with pdflatex so that pages are rotated when displayed \o/ https://tex.stackexchange.com/a/354 \begin{landscape} ... \end{landscape}
…however some document formats are not conducive to this such as the Tufte LaTeX Classes, but there is a work around. Someone more knowledgeable in LaTeX than I am suggested the following to rotate a page to landscape in Tufte Books…
- snippet.latex
\begin{landscape} \advance\vsize6cm \csname @colroom\endcsname=\vsize \textheight=\vsize \csname @colht\endcsname=\vsize ... \end{landscape}
Title Page
The standard method of specifying a title page works, but sometimes (often!) a more elaborate title page is required and this can be achieved using the titlepage
environment.
- snippet.latex
\begin{titlepage} \begin{center} \HRule \\[0.4cm] {\huge Main Title}\\ \HRule \\[1.5cm] \textsc{\LARGE Department} \\[0.5cm] \textsc{\Large Company} \\[1cm] % Left hand column \doublebox{% \begin{minipage}{0.4\textwidth} \begin{flushleft} \emph{Version :}\\[0.5cm] \emph{Date : }\\[0.5cm] \emph{Author :}\\[0.5cm] \emph{Signature :}\\[0.5cm] \emph{Date : }\\[0.5cm] \emph{Authorised :}\\[0.5cm] \emph{Signature :}\\[0.5cm] \emph{Date : }\\[0.5cm] \emph{Chief Investigator :}\\[0.5cm] \emph{Signature :}\\[0.5cm] \emph{Date : }\\[0.5cm] \end{flushleft} \end{minipage} \begin{minipage}{0.4\textwidth} \begin{flushleft} 1.0\\[0.5cm] \today \\[0.5cm] \href{mailto:person1@an.address.org}{Person 1}\\[0.5cm] \underline{\hspace{0.95\textwidth}} \\[0.5cm] \underline{\hspace{0.95\textwidth}} \\[0.5cm] \href{mailto:person2@an.address.org}{Person 2}\\[0.5cm] \underline{\hspace{0.95\textwidth}} \\[0.5cm] \underline{\hspace{0.95\textwidth}} \\[1cm] \href{mailto:person3@an.address.org}{Person 3}\\[0.5cm] \underline{\hspace{0.95\textwidth}} \\[0.5cm] \underline{\hspace{0.95\textwidth}} \\[1cm] \end{flushleft} \end{minipage}} \vfill \end{center} \end{titlepage}
Tables
A useful comparison of tables is available here.
Spanning Rows and Columns
Spanning rows and columns is exceptionally easy. You need to include the multirow
package in the preamble of your document (\usepackage{multirow}
) and to span columns you simply.
- snippet.latex
\begin{table}[!ht] \begin{center} \begin{tabular}{lccc} \hline & \multicolumn{3}{c}{A centralised header} \\ & Column 1 & Column 2 & Column 3 \\ \hline \multirow{2}{*}{A two-row span} & Cell 1 & Cell 2 & Cell 3 \\ & Cell 4 & Cell 5 & Cell 6 \\ \hline \end{tabular} \end{center} \end{table}
This will produce the following table
A centralised header | |||
---|---|---|---|
A two-row span | Cell 1 | Cell 2 | Cell 3 |
Cell 4 | Cell 5 | Cell 6 |
(You'll note that I've been unable to find how to do row-spanning in doku!)
If you wish to control the width of a column and wrap text in multirow spanning you need to make sure you specify the width of the column under both \begin{tabular}{p{2cm}}
and also in the \multirow{2}{2cm}{A two-row span}
.
Longtable
Sometimes you wish to have a table that is too big (in length) to fit onto one page. In such instances the longtable
package can be used to spread tables over multiple pages. You should load the package in your pre-amble with \usepackage{longtable}
. You then define two sets of first rows, one that goes at the very top of the first table, and one that appears at the top of each table on subsequent pages, and similarly for the end of tables, one is specified for the bottom of all pages, and a final row for the very last page.
A basic template is provided below for three columns…
- snippet.latex
\begin{longtable}{p{0.2\textwidth}p{0.1\textwidth}p{0.2\textwidth}p{0.35\textwidth}p{0.35\textwidth}} \caption{Description of variables in the Demographics table.\label{spec-demographics}} \\ \hline \\ Variable & Type & Format & Description & Validation \\ \hline \\ \endfirsthead \multicolumn{5}{c}{ \emph{cont.}} \\ \hline \\ Variable & Type & Format & Description & Validation \\ \hline \\ \endhead \hline \\ \multicolumn{5}{r}{\emph{Continued\ldots}} \\ \endfoot \hline \\ \endlastfoot Variable1 & Type1 & Format1 & Description1 & Validation1 \\ Variable2 & Type2 & Format2 & Description2 & Validation2 \\ \end{longtable}
If you find the caption text is wrapped and you want it to span further across the page you can set the LTcapwidth
in the preamble of your document with…
- snippet.latex
\setlength{\LTcapwidth}{\linewidth}
Rotating Text Within Cells
This is simple to achieve with the rotating
package.
- snippet.latex
\usepackage{rotating} \begin{longtable}{p{0.2\textwidth}p{0.1\textwidth}p{0.2\textwidth}p{0.35\textwidth}p{0.35\textwidth}} \caption{Description of variables in the Demographics table.\label{spec-demographics}} \\ \hline \\ Variable & \begin{turn}{90}Type\end{turn} & \begin{turn}{90}Format\end{turn} & \begin{turn}{90}Description\end{turn} & \begin{turn}{90}Validation\end{turn} \\ \hline \\ \endfirsthead \multicolumn{5}{c}{ \emph{cont.}} \\ \hline \\ Variable & Type & Format & Description & Validation \\ \hline \\ \endhead \hline \\ \multicolumn{5}{r}{\emph{Continued\ldots}} \\ \endfoot \hline \\ \endlastfoot Variable1 & Type1 & Format1 & Description1 & Validation1 \\ Variable2 & Type2 & Format2 & Description2 & Validation2 \\ \end{longtable}
Figures
Lots to be written on figures, one useful thing when using \subfigure{}
that you want to split over multiple pages though is to split them into their own \begin{figure}...\end{figure}
floats but in the second to use \ContiuedFloat
so they are part of the same package and not renumbered.
- snippet.latex
\begin{landscape} \begin{figure}[!ht] \centering \begin{subfigure}[b]{\textwidth} \includegraphics[scale=0.65]{figures/hist_all.png} \caption{Overall} \end{subfigure} \caption{Distribution across everything.} \end{figure} \begin{figure} \ContinuedFloat \begin{subfigure}[b]{\textwidth} \includegraphics[scale=0.65]{figures/hist_by_somethingpng} \caption{By Something} \end{subfigure} \caption{Distribution by something} \label{85thNewOldOverlay} \end{figure} \end{figure}
Special Characters
LaTeX has a plethora of special characters available. Some are provided in the stock installation, whilst others are provided as part of individual packages. The ultimate reference for all symbols is The Comprehensive LaTeX Symbol Guide. I've detailed below a number of symbols that I have had recourse to use on a regular basis.
Symbol | Code |
---|---|
✔ | \checkmark |
\male | |
\female |
Verbatim Text
Including examples of code is (as with many other things) straight-forward in LaTeX, you simply use the verbatim environment and encapsulate your code within the \begin{verbatim}
and \end{verbatim}
. For small sections of in-line code that you wish to include simply use the \verb|your text|
alternative.
A different approach, particularly useful if you wish to use colour in your code, is to use the alltt
tags to encapsulate your code…
- snippet.latex
\begin{alltt} \textcolor{red}{/* Set up the system */} capture log c label drop _all clear set more off version 10.1 set mem 600m ... \end{alltt}
Headers and Footers
LaTeX has sections for headers and footers on each page of a document. The defaults for these are simple and straightforward, but sometimes you may want more fancy headers and footers. Fortunately there is the fancyhdr
package that allows fine tuning and customisation of the headers and footers.
In the header of your document you most load the package, declare that the fancy
pagestyle is to be used and then simply fill in the text that you want in the left (\l
), center (\c
) or right (\r
) of the header (head
) or footer (foot
)
- snippet.latex
\usepackage{fancyhdr} \pagestyle{fancy} \lhead{} \chead{} \rhead{} \lfoot{} \cfoot{} \rfoot{}
ToDo Notes
Very handy when working on documents, they can be in margins…
- snippet.latex
\todo{Must insert something here}
…or if they are longer you may want to place them inline
…
- snippet.latex
\todo[inline]{A really long note reminding me to do something that would look very strange if it was wrapped in the margin}
Text boxes
To place a box around a section of text simply use the following code
- snippet.latex
\ovalbox{% \begin{minipage}{0.9\textwidth} ... \end{minipage}}
Alternative Documents
As well as books, reports and theses LaTeX can be used to generate slides, posters and letters. The sections below detail the packages and approaches I've used to generate these documents.
Slides - Beamer
The Beamer (Beamer - CTAN) package is simple and ideal for creating slide-shows as PDFs.
Columns in Slides
A neat 'trick' to get columns within slides is to define Minipages
within a given slide to contain two sets of text or text and a picture. You can be smart and set the width of the second column based on the width of the first by placing the following new definitions in the preamble…
- snippet.latex
\newlength{\MiniPageLeft} \newlength{\MiniPageRight}
…and then on a given slide using…
Verbatim Sections in Beamer
- snippet.latex
\begin{frame}[fragile]{<SECION TITLE>}{<SUBSECTION TITLE>}
Posters
Posters can be prepared relatively easily using LaTeX, and in my opinion they look a lot nicer than those prepared in alternatives. A poster I created in this manner is here and details of how to do this here.
Letters
Writing letters in LaTeX is very straightforward use the following template and type your letter contents (including tables, images etc.) in-between the \opening{}
and closing{}
tags, then compile your document as you would normally.
- snippet.latex
\documentclass[a4paper]{letter} \address{1st Line of your Address \\ 2nd Line \\ 3rd Line \\ 4th Line \\ etc.} \name{Your Name} \signature{Your title as you would like it to appear at the bottom of the letter} \begin{document} \begin{letter}{1st Line of Recipients Address \\ 2nd Line \\ 3rd Line \\ 4th Line \\ etc. } \opening{Dear Somebody,} \closing{Yours sincerely} \end{letter} \end{document}
Curriculum Vitae
BibTex
Including references in technical reports to work and methods that you cite is crucial to producing self-contained informative papers, reports and posters. Bibliographies and references are handled using BibTex.
Use the BibitNow extension to grab citations of webpages.
Paste these into plain .bib
files (kept under version control in Org repos) and occasionally use ebib and/or Jabref to manage (the later is very useful for finding errors in .bib
files).
A large number of BibTex styles for various journals (primarily focused on biological journals) is available here.
Example
In your document you can include citations in the following manner…
- snippet.latex
This is a citation~\cite{somepaper2025} \bibliographystyle{natbib} \bibliography{sections/references}
- snippet.latex
% sections/references.bib @article{somepaper2025, title={Blah, blah, blah}, author={Important, A.P.}, journal={Journal of Irreproducible Research}, volume={5}, number={3}, pages={5-10}, year={2025}, publisher={Elsevier}, abstract={This is an abstract!}, url={https://www.sciencedirect.com/science/article/abs/pii/S0002390239} }
BibTex Entries
I needed to write an entry by hand and found this useful example which I copy below for reference…
- snippet.latex
@article{article, author = {Peter Adams}, title = {The title of the work}, journal = {The name of the journal}, year = 1993, number = 2, pages = {201-213}, month = 7, note = {An optional note}, volume = 4 } @book{book, author = {Peter Babington}, title = {The title of the work}, publisher = {The name of the publisher}, year = 1993, volume = 4, series = 10, address = {The address}, edition = 3, month = 7, note = {An optional note}, isbn = {3257227892} } @booklet{booklet, title = {The title of the work}, author = {Peter Caxton}, howpublished = {How it was published}, address = {The address of the publisher}, month = 7, year = 1993, note = {An optional note} } @conference{conference, author = {Peter Draper}, title = {The title of the work}, booktitle = {The title of the book}, year = 1993, editor = {The editor}, volume = 4, series = 5, pages = 213, address = {The address of the publisher}, month = 7, organization = {The organization}, publisher = {The publisher}, note = {An optional note} } @inbook{inbook, author = {Peter Eston}, title = {The title of the work}, chapter = 8, pages = {201-213}, publisher = {The name of the publisher}, year = 1993, volume = 4, series = 5, address = {The address of the publisher}, edition = 3, month = 7, note = {An optional note} } @incollection{incollection, author = {Peter Farindon}, title = {The title of the work}, booktitle = {The title of the book}, publisher = {The name of the publisher}, year = 1993, editor = {The editor}, volume = 4, series = 5, chapter = 8, pages = {201-213}, address = {The address of the publisher}, edition = 3, month = 7, note = {An optional note} } @manual{manual, title = {The title of the work}, author = {Peter Gainsford}, organization = {The organization}, address = {The address of the publisher}, edition = 3, month = 7, year = 1993, note = {An optional note} } @mastersthesis{mastersthesis, author = {Peter Harwood}, title = {The title of the work}, school = {The school of the thesis}, year = 1993, address = {The address of the publisher}, month = 7, note = {An optional note} } @misc{misc, author = {Peter Isley}, title = {The title of the work}, howpublished = {How it was published}, month = 7, year = 1993, note = {An optional note} } @phdthesis{phdthesis, author = {Peter Joslin}, title = {The title of the work}, school = {The school of the thesis}, year = 1993, address = {The address of the publisher}, month = 7, note = {An optional note} } @proceedings{proceedings, title = {The title of the work}, year = 1993, editor = {Peter Kidwelly}, volume = 4, series = 5, address = {The address of the publisher}, month = 7, organization = {The organization}, publisher = {The name of the publisher}, note = {An optional note} } @techreport{techreport, author = {Peter Lambert}, title = {The title of the work}, institution = {The institution that published}, year = 1993, number = 2, address = {The address of the publisher}, month = 7, note = {An optional note} } @unpublished{unpublished, author = {Peter Marcheford}, title = {The title of the work}, note = {An optional note}, month = 7, year = 1993 }
Tidying BibTeX
Over time you may find errors creep into your BibTeX file, a useful tool for finding these is JabRef which will report the location of errors on import. Most of the time it thinks its a missing comma (,
) but often it will be the lack of a closing }
within a reference commonly caused by letter/word/text being within a field at the end of a row being encapsulated in {braces}
but then missing a }
to close the field itself. Give it a whirl if you encounter problems and look for these issues.
`
Templates
Below are links to some generic templates that I've written and use regularly myself.
- slides.tex (Makefile required to generate PDF of slides).
Tips & Tricks
Most of my Tips & Tricks are from various blogs such as the brilliant LaTeX Matters but I've collected them here for my own reference (as my bookmarks seem to grow exponentially and I can never find things again!).
Links
Beamer
TeX StackExchange
BibTex
Templates
Errors
latex linux howto