## Saturday, 12 October 2013

### Writing a PhD Thesis in LaTeX

So, you're starting your history thesis and you're having a look at some other recent ones to get a sense of how they're done. And one thing that strikes you is how awful they look, with that dreary Word Document functionality. But when you look at your big sister's physics thesis, and your boyfriend's computing thesis, they're all beautifully typeset with real ligatures and perfect spacing as if it's just yearning to be hard-bound, gold-embossed and shelved in a mahogany library with busts of Roman Emperors and models of molecules on the cases. And you think, why can't mine look like that? I'm the artist around here: MY thesis should look like a work of art.

And you know why it is: it's because they did it in LaTeX. They just typed it into a text editor, so even when they have an 80,000-word thesis the file is less than a megabite and loads instantly. All their references are handled automatically without fancy commercial software. They can put in references to figures, cross-references, indexes, tables of contents that simply update as they move things around (maybe Word can do these things, I don't know, but it involves advanced training, and at the end of the day will still look like a document typed in Word).

But their thesis is in that Century Schoolbook font, and uses those Harvard references, and your history thesis needs footnotes and primary and secondary sources and to look, well, like a history book. How do you write a history thesis in LaTeX? Well, I thought, there's only one way to find out.

Now, I'm not going to tell you how to install LaTeX. There are lots of sites out there that do that. And unless you are braver and cleverer than me, you won't embark on this unless you have a physics/ computing/ engineering pal getting you started and giving you some tech support. You may also need to install an extra module or two, which I found a bit traumatic, but again, there are instructions out there on how to do this.

So I'm going to assume you've got LaTeX successfully installed and are not frightened by a bit of code. So, let's make a folder on our computer called thesis, put all the following files into it, and write our thesis!

But first, we need to sort those references out. A style called 'verbose' gets us pretty close to what Stirling university requires but it needs to be tweaked. My kind friend Rob Hague did this for me. You need to create a file in your new folder called biblatex.cfg, and put the following mystery code into it:

% To fix sort ordering, add a "sorttitle" (or "sortname") field to the offending entries.

\ProvidesFile{biblatex.cfg}

% Separate units with , rather than .

% Format publisher as (City: Name, year)
\renewbibmacro*{publisher+location+date}{%
\printlist{location}%
\printlist{publisher}%
\usebibmacro{date}%
}\nopunct%
\newunit}

% Remove "In:"
\renewbibmacro{in:}{%
\ifentrytype{article}{}{%
\printtext{\bibstring{in}\space}}}

% Title in single quotes
\DeclareFieldFormat[article,incollection]{title}{#1'\isdot}

% Omit pp in articles
\DeclareFieldFormat[article]{pages}{#1}

\DefineBibliographyStrings{english}{%
byeditor = {ed.},
urlseen = {accessed}
}

\endinput

Don't ask me how this works, but it does. Say thanks to Rob @robhague.

Now, you need to do some reading and build a bibliography. Create a file called book.bib and put in each thing you read as an entry, like this:

@unpublished{ ForbesWletters,
keywords = "manuscript",
author = "William Forbes",
title = "Letters of Forbes of Pitsligo",
note = "NLS Acc.4796, Acc.12092"}

@book{ AlisonA1820ii,
keywords = "primary",
author = "Archibald Alison",
title = "Sermons, Chiefly on Particular Occasions",
shorttitle = "Sermons",
publisher = "Archibald Constable",
volume = "2",
year = "1814"}

@article{ BlackJ88,
keywords = "secondary",
author = "Jeremy Black",
title = "The Tory View of British Foreign Policy",
sorttitle = "Tory View of British Foreign Policy",
shorttitle = "Tory View",
journal = "Historical Journal",
number = "31",
year = "1988",
pages = "469-477"}

@incollection{ BurnsR93,
keywords = "secondary",
author = "R.A. Burns",
title = "A Hanoverian Legacy? Diocesan Reform in the Church of England c.1800-1833",
sorttitle = "Hanoverian Legacy? Diocesan Reform in the Church of England c.1800-1833",
shorttitle = "Diocesan Reform",
booktitle = "The Church of England c.1689-c.1833: from Toleration to Tractarianism",
editor = "J. Walsh, C. Haydon and S. Taylor",
pages = "265-282",
publisher = "Cambridge University Press",
year = "1993"}

@phdthesis {GordonG79,
keywords = "secondary",
author = "George Gordon",
title = "The Status Areas of Edinburgh: a historical analysis",
sorttitle = "Status Areas of Edinburgh: a historical analysis",
shorttitle = "Status Areas",
school = "Edinburgh University",
year = "1979"}

@online{ HarrisE13,
keywords = "secondary",
author = "Eleanor M Harris",
title = "Writing a History PhD in LaTeX",
url = "http://eleanormharris.blogspot.co.uk/2013/10/writing-phd-thesis-in-latex.html",
urldate = "2013-10-12"}

I hope that's fairly self-explanatory. Each entry begins with its type, and a unique identifier you'll use to cite it (this can be anything but I find surname, initial, year is handy). The 'shorttitle' field is for when you cite something multiple times: the first footnote you want the full detail, but for subsequent ones you just want 'Black, Tory View, p.473'. The 'sorttitle' is so that, in your bibliography, when you cite five works all by R.A. Burns, it alphabetises them correctly ignoring 'The' and 'A'. Clever eh?

There are all kinds of different entry types and possible fields: there's a complete list at http://www.math.upenn.edu/tex_docs/latex/biblatex/biblatex.pdf.

So, you've read a pile of books and manuscripts and it's time to get writing. You go back to your directory, and create a second file called thesis.tex, and you put this template into it:

\documentclass[a4paper]{book}

% This sets the margins to sensible dimensions

\setlength{\oddsidemargin}{0.25in}
\setlength{\evensidemargin}{0.25in}
\setlength{\topmargin}{0in}
\setlength{\marginparsep}{0pt}
\setlength{\marginparwidth}{0pt}
\setlength{\textwidth}{5.75in}
\setlength{\textheight}{9.75in}
\setlength{\footskip}{0.75in}

\pagestyle{plain}

% This lets you put in pictures.

\usepackage{graphicx}

% this allows you to have table cells with little paragraphs in.

\usepackage{array}
\newcolumntype{L}[1]{>{\raggedright\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}

% This enables the bibliography, tells it to use the 'verbose' style of footnoting, sets the title of the bibliography, and tells it where to find the file (we called it 'book').

\usepackage[style=verbose,natbib=true]{biblatex}
\bibliography{book}

% This one-and-a-half spaces it, which looks much nicer than double-spaced.

\renewcommand{\baselinestretch}{1.5}

% OK I've forgotten what this does. I think it was a failed attempt to get the page numbering of the pdf file to line up with the pages in the document so my clickable contents page wasn't de-synched by the 'front matter'. If you understand that and know how to make it work, I'd love to know!

\usepackage[plainpages=false,pdfpagelabels]{hyperref}

% This means the font looks like a history book not an old science textbook. And it's so much nicer than Times New Roman.

\usepackage{palatino}

% All that preceding stuff is the header. You can ignore it. Now, five, four, three, two, one...

\begin{document}

\begin{titlepage}

\begin{center}

~

\vspace{5cm}

\vspace{2cm}

\vfill

\large Department of History and Politics\\
\large School of Arts and Humanities \\
\large University of Stirling

\vspace{1cm}

\large A thesis submitted for the degree of Doctor of Philosophy

\vspace{1cm}

\end{center}

\end{titlepage}

~

\vspace{10cm}

\begin{center}

\large Put Date of Submission Here

\vspace{3cm}

\large I, Your Name declare that this thesis has been composed by me and that the work which it embodies is my work and has not been included in another thesis.

\end{center}

\clearpage

\section*{Acknowledgements}

\clearpage

\section*{Abstract}

\pagenumbering{alph}

\maketitle

\pagenumbering{roman}

\tableofcontents

\clearpage

\listoffigures

\clearpage

\listoftables

\clearpage

\pagenumbering{arabic}

\clearpage

\nocite{*}

\noindent \textbf{Abbreviations:}\\
NRS National Records of Scotland\\
NLS National Library of Scotland

\end{document}
Good heavens!

Actually, from this point it gets a lot easier, because now you've got all that code set you can completely forget all about it and watch your thesis grow. Here is a little sample:

\chapter{Name of Chapter}

\label{firstparagraph} The chapter headings are formatted as above, while the normal text of the thesis is just like this: normal text. To begin a new paragraph we simply press enter twice.

Look, a new paragraph. We can add a footnote.\footnote{This text will appear as a footnote} However, more often we want to cite our wonderful bibliography.\autocite[83]{GordonG79} Sometimes we want to cite several bibliography items in one footnote.\footnote{\cite{HarrisE13}; \cite[268]{BurnsR93}.} I always miss at least one curly bracket from these footnotes, causing the \LaTeX to break when I compile it.

\section{Futher Excitements}

We may want to divide our chapter into sections which we can do like this. You can divide your section into subsections if you like. We can easily make text \emph{italic} or \textbf{bold}. We may also want to add a figure (Fig.~\ref{fig:tree}). I always miss the final bracket off that figure-citing command. The filename (tree, on the line beginning includegraphics') should correspond to a file (tree.jpg) in your thesis directory, with its file extension omitted.

\begin{figure}[!htbp]
\begin{center}
\includegraphics[width=15cm]{tree}
\end{center}
\caption[A Tree (this is the text that appears in the list of tables)]{A tree. This is the caption that goes under the picture. It's a good place to sneak in a lot of additional information without adding to the wordcount.\footnote{A caption can also have a footnote.}}
\label{fig:tree}
\end{figure}

The `label' command is very handy as it lets you do cross-references. So the label 'tree' in the figure lets us refer to the figure in the text. If we added a new figure before it, it would update itself from Fig.1 to Fig.2. Also did you notice that at the beginning of our chapter there was a label (p.~pageref{firstparagraph})? Oh, look, we've just cross-referenced to it! We can put labels anywhere we want to cross-reference to.

\begin{table}[!htbp]
\begin{center}
\begin{tabular}{| l r r r |}
\hline
\textbf{Region} & \textbf{Women} & \textbf{Men} & \textbf{Total}\\
Highland & 6  & 5 & 11\\
North-east & 6 & 8 & 14\\
Central Belt & 11 & 11 & 22\\
Borders & 7 & 4 & 11\\
Rest of UK & 11 & 5 & 16\\
\textbf{Total} & 41 & 33 & 74\\
\hline
\end{tabular}
\end{center}
\caption[Location of landowning families in Charlotte Chapel]{Location of landowning families in Charlotte Chapel.}
\label{tab:landowners}
\end{table}

We may also want a table. Table~\ref{tab:landowners} shows a dull one from my thesis. The syntax is a bit dazzling but quite simple: separate each cell of the table with an ampersand, and end each line with two backslashes.  The code in the line:

\verb1\begin{tabular}{| l r r r |}1

\noindent means draw a vertical line, then make four columns, one aligned left and three aligned right, then draw another vertical line. (noindent means we aren't really beginning a new paragraph, just carrying on the same one)

\begin{quote}
Sometimes we want to quote a snatch of verse\\
Or an overly long quote which of course is worse,
\end{quote}

\noindent and in this case, too, noindent is handy afterwards.

So let's try making a document. Copy the above section of code, and replace the words 'put your thesis here' in your thesis.tex template. Then run the following commands:

pdflatex thesis.tex
bibtex thesis.aux
pdflatex thesis.tex
pdflatex thesis.tex

You should now have a file called thesis.pdf. Have a look.

Running all those commands is a bit tiresome, especially as sometimes an error generates a corrupt .aux file which you have to delete. The thing to do is create a little file called make.txt containing this:

#!/bin/sh
rm thesis.aux
pdflatex thesis.tex
bibtex thesis.aux
pdflatex thesis.tex
pdflatex thesis.tex

Change the mode of this file to executable (ask your geek friend...), and then instead of typing all those commands you can just type ./make.txt and away it goes. Once you have a good, long thesis with lots of images and footnotes, you can go away and have a coffee at this point while it compiles, and pretend you are a proper computer programmer, except that you are programming beautifully typeset art.

Once my thesis is online I shall link to it here, as a proper sample. But meanwhile, go and have a go!

#### 1 comment:

1. Hi, you have posted such precious and informative article which gave me lot of information. I hope that you will keep it up and we will have more informative and helping news from you. Thanks | Phd Thesis Writing Services