LaTeX

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…

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

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

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

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

\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

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.

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

Longtables

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…

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

\setlength{\LTcapwidth}{\linewidth}

Rotating Text Within Cells

This is simple to achieve with the rotating package.

\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}
 
 
==== Special Characters ====
 
[[http://www.latex-project.org/|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 [[http://www.ctan.org/tex-archive/info/symbols/comprehensive/symbols-a4.pdf|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...
 
<code 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)

\usepackage{fancyhdr}
\pagestyle{fancy}
\lhead{}
\chead{}
\rhead{}
\lfoot{}
\cfoot{}
\rfoot{}

Text boxes

To place a box around a section of text simply use the following code

\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

The Prosper 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…

\newlength{\MiniPageLeft}
\newlength{\MiniPageRight}

…and then on a given slide using…

\begin{slide}{Title}
\setlength{\MiniPageLeft}{0.5\textwidth}
\setlength{\MiniPageRight}{\textwidth}\addtolength{\MiniPageRight}{-\MiniPageLeft}
\begin{minipage}[t]{\MiniPageLeft}
 
\end{minipage}\hspace{0.5cm}%
\begin{minipage}[t]{\MiniPageRight}
 
\end{minipage}
\end{slide}

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.

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

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.

I use two methods for managing my BibTex references. I keep and manage a local copy on my system using the excellent Pybliographer although this is now essentially just a regularly downloaded/updated version of the references I keep at CiteULike. My references at CiteULike can be viewed here (General, mainly genetics) and here (specifically related to cancer and epidemiology).

A large number of BibTex styles for various journals (primarily focused on biological journals) is available here.

Templates

Below are links to some generic templates that I've written and use regularly myself.

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!).

Templates

latex/latex.txt · Last modified: 2016/01/29 12:50 by neil
CC Attribution-Noncommercial 4.0 International
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0