Books

The last LaTeX document class covered in the course is book. Books can be thought of as a more complex version of the article class – they are likely to consist of multiple chapter files with one “master” file binding them all together. This class is particularly useful when preparing a longer piece of academic work, such as a PhD thesis. This section will briefly go through an example book structure which you can download here. The book folder should always follow a tree-like structure, summarized below. Each chapter is a separate tex file and all the chapters are stored in the same folder. Similarly, all the image files used in the book are stored in a separate folder, with sub-folders corresponding to each chapter number.

 ----\book_template
    |   header.tex
    |   mybook.tex
    |----\bibliography
    |       references.bib
    |----\chapters
    |       01_introduction.tex
    |       02_body.tex
    |       03_conclusion.tex
    |       titlepage.tex
    |----\images
        |---\01
        |       koala.jpg
        |---\02
        |       lemur.jpg
        |---\03
                sloth.jpg

Main File

The main file, mybook.tex binds everything together using the main document environment. The \include command inserts the book chapters in a specified order. The \input command is used to “copy and paste” the content of a tex file. In this case, it inserts the content of the header.tex file, which contains the book’s preamble. Finally, the \addcontentsline command is used to add the “Bibliography” entry to the table of contents.

\input{header}

\begin{document}

    \include{chapters/titlepage}
    \clearpage
    \tableofcontents

    \include{chapters/01_introduction}
    \include{chapters/02_body}
    \include{chapters/03_conclusion}

    %bibliography
    \cleardoublepage
    \addcontentsline{toc}{chapter}{Bibliography}
    \bibliographystyle{apa}
    \bibliography{bibliography/references}


\end{document}

The Preamble File

The header.tex file contains everything that would normally be put in the document’s preamble, i.e. before the \begin{document} statement. Note that this is done for clarity and is not necessary when using a simple book structure like the one in the example – the preamble could simply be placed at the beginning of the mybook.tex file, like in other document classes. However, with the growing complexity of the book, your preamble is likely to get filled with multiple package references and other code customizing the final output. Because of that, storing the preamble in a separate file is likely to make your life easier and keep the length of the main file to a minimum, so that it gives you a good overview of your book’s structure.

When working with large documents, compiling the entire document while working on a specific chapter only may waste a lot of time. To avoid it, you can use the \includeonly tag in the preamble of the book. It explicitly tells the LaTeX compiler which chapter files to use when you hit the “play” button in TeXStudio. For example, using \includeonly{chapters/01_introduction} will mean that the \include tags in the main file referencing other chapters than 01_introduction.tex will be ignored and their content will not be compiled in the final output.

\documentclass[a4paper,12pt]{book}
\usepackage{graphicx}
\usepackage{natbib}
\usepackage{tabularx}
\usepackage{lipsum}

\author{James Doe}
\title{My PhD Thesis}

%this allows you to compile the document with specified chapters only
%\includeonly{chapters/01_introduction}

Chapter File

The chapter file resembles a normal LaTeX document, except for the fact that it has neither a preamble nor a \begin{document} statement. Instead, it just begins with \chapter{chaptertitle}. All the packages used in the chapters are defined globally in the book’s preamble.

\chapter{Introduction}
\section{First section}
Let's begin with citation of \citet{schadkeetal2007}.\\

\lipsum[1-5]
\begin{figure}[!h]
    \centering
    \includegraphics[width = 0.8\textwidth]{images/01/koala.jpg}
    \caption{Picture of a coala}
\end{figure}
\section{Second section}
\lipsum[1-5]

Listings

Reproducibility of results a key prerequisite of methodological transparency and one of the most important facets of quantitative research in social sciences. Every respectable scholar or analyst should allow his audience to go through his research cycle step-by-step, which involves providing them with the original data and the source-code used for the statistical analysis. Reading someone else’s code might often be a difficult task and providing some comments is likely to make it easier. The listings package allows you to include chunks of code in your documents, which makes it possible to prepare technical reports and appendices explaining your quantitative research.


A Basic Example

Like every other package, listings needs to be loaded in the preamble of the document. The first example shows the minimal structure of a document using listings. \lstset{language=R} is used to determine which language will you use as the input (in this case we use R). Then, the lstlisting environment is used to include code in the text. Note, that in the output, a different text style is used for function names, such as function, sqrt, sum, comments and variables to make the code more readable. 

\documentclass{article}
\usepackage{listings}
\lstset{language=R}
\begin{document}
The following function is used to return L2 vector norm:
\begin{lstlisting}
norm <- function(x){
#calculate norm:
sqrt(sum(x^2)) 
}
\end{lstlisting}
\end{document}

Formatting Listings

To make the code you include in your document more readable, you can add additional formatting to the lstlisting environment by setting different parameters through lstset. The example below shows some most important things that can be changed – you can check out the Listings package documentation for an exhaustive list.

\lstset{language=R,
basicstyle=\ttfamily, %style used for listings
numbers=left, %code line numbers
keywordstyle=\color{red}, %set color of keywords to red
commentstyle=\color{blue}\itshape,
stringstyle=\color{magenta}, %set comment color to green
tabsize = 2, %set tabulation style
emph={function}, %emphasize the words you want
emphstyle=\color{red}\underbar, %choose emphasis style
showstringspaces = false, %without this, symbol will replace spaces in strings
deletekeywords = {se,set,data,model},
morekeywords={ggplot,FALSE,TRUE}
}
The following function is used to return L2 vector norm:
\begin{lstlisting}
norm <- function(x){
#calculate norm:
sqrt(sum(x^2)) 
print("Task completed")
}
\end{lstlisting}

See Table 3 for a list of common parameters:

Table 3: Formatting of Listings
Parameter Description
language Which language you are using. This determines the default keywords to be highlighted.
basicstyle The basic style used for listing. The teletype (\ttfamily) font is a good choice. You can also alter the font size used for the listing.
numbers Location of line numbers
keywordstyle Formatting of language keywords. You can use \color{color} (this requires using color or xcolor packages). Other examples include \itshape for italics, \underbar for underscore and \bfseries for the bold font.
commentstyle Style of the comments.
strinstyle Style of the strings
tabsize Tabulation size
emph, emphstyle This allows you to select a separate style for some words that you want to emphasize in the code
showstringspaces Determines how spaces are shown in strings in the code
morekeywords Allows you to specify additional keywords you want to highlight using the keywordstyle
deletekeywords Allows you to specify keywords you don’t want to be highlighted

Using Source Files

Copying and pasting your code might be effortful, especially if you make any changes to it after you put it in your document. Instead, you can use \lstinputlisting[lines]{filename} tag, which allows you to input code directly from the source file you used in the analysis (for example an R script). Additionally, you can use the option brackets [] to specify the lines you want to insert. For example \lstinputlisting[firstline = 2, lastline = 10]{script.R} will input lines 2 to 10 from file script.R (provided that the file is in the same location as the .tex file).

This code was used to produce the table and plot for one of the Beamer slides in the previous part of the course:

\lstinputlisting{graph_table.R}

\bigskip

\textbf{This code generates some toy data:}
\lstinputlisting[firstline = 5, lastline = 7]{graph_table.R}

\bigskip

\textbf{This code runs the regression model and gets the LaTeX output:}
\lstinputlisting[firstline = 10, lastline = 12]{graph_table.R}

\bigskip

\textbf{This code is used for plotting:}
\lstinputlisting[firstline = 14, lastline = 18]{graph_table.R}

Inline code

Finally, you can use your code in the middle of your regular text, by using \lstinline|yourcode|, for example:

I used \lstinline|apply(mydf, 1, function(x) sum(x))| to get a row-wise sum of the \texttt{mydf}.

This makes it easy to reference specific parts of your syntax. It can also be used with a source file, like \lstinputlisting.