27 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
27.0.1 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}
27.1 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.
27.2 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.
27.3 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.
27.4 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 27.1 for a list of common parameters:
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 |
27.5 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}
27.6 Inline code
Finally, you can use your code in the middle of your regular text, by using \lstinline|yourcode|, for example:
This makes it easy to reference specific parts of your syntax. It can also be used with a source file, like \lstinputlisting.
27.7 Natbib – Advanced Citations
While citations and references were already covered in part 2 of the course, the natbib package extends BiBTeX’s capabilities and offers more flexibility in customizing LaTeX bibliographies by including new bibliography styles and extending the \cite command. The structure of the bibliography file (.bib) used by natbib remains the same as in BibTeX.
Same as in BiBTeX, the bibliography style is set using the \bibliographystyle command. Useful styles include agsm which sets Harvard-like references (the citation style officially used by the PAIS department), as well as plainnat and abbrvnat (which are similar, but the latter uses first name initials instead of full names). By default, some styles (such as apalike or agsm) use round parentheses, while others (like plainnant) use round brackets. This can be changed by loading the natbib with the round option (\usepackage[round]{natbib}) which overrides the default style parentheses to be round or the square option, which does the opposite. Note that when changing the bibliography style in a file once rendered, you need to delete the .bbl auxiliary file which appears in the same folder as your rendered document.
Natbib also implements multiple versions of the citation command. Rather than using \cite as in BiBTeX, the simplest form of bibliography citation is \citep. \citet is a second alternative, which inserts the author’s surname without parentheses, followed by the publication year enclosed in parentheses. This allows you to cite a resource when using the author’s surname in the sentence, as demonstrated in the example.
\documentclass{article}
\usepackage{natbib}
\bibliographystyle{agsm}
\begin{document}
\citet{schadkeetal2007} conducted an experiment demonstrating that prolonged deliberation about politics amongst like-minded voters results in amplified ideological polarization.
Experiments have shown that prolonged deliberation about politics amongst like-minded voters results in amplified ideological polarization \citep{schadkeetal2007}.
\bibliography{my_bibliography}
\end{document}
Both \citep and \citet have their alternative versions - \citealp and \citealt, which remove the parentheses entirely. The difference between \citealp and \citealt is that the latter separates the author’s name and publication year with a comma. This does not apply to the agsm bibliography style, which uses no comma between author’s surname and year – in this case, \citealp and \citealt are equivalent.
Adding an asterisk * at the end of each natbib’s citation command overrides the default et al. abbreviation commonly used in citations of resources with multiple authors and uses the names of all authors instead (this is not supported in some styles, such as apalike). Using the square brackets allows you to add prefixes and suffixes in the citation. So, when using \citep[option1][option2]{resource}, the option1 will be inserted before and option2 after the citation content (see example). Finally, using the \citetext command you can join multiple natbib citations and add your comments in between them, as shown in the example.
\documentclass{article}
\usepackage{natbib}
\bibliographystyle{agsm}
\begin{document}
Experiments have shown that prolonged deliberation about politics amongst like-minded voters results in amplified ideological polarization \citep*[see][pp.~920]{schadkeetal2007}. This undermines the applicability of theoretical models proposed by proponents of deliberative democracy \citetext{see \citealp{ackermanfishkin2004}, see also \citealp{cohen1989}}.
\bibliography{my_bibliography}
\end{document}
Table 27.2 summarizes the functionality of different citation commands in Natbib. Check out the .tex file used to produce the table – it uses the listings package to cite LaTeX code inside of a LaTeX document.
Command | Result |
---|---|
\citep{cohen1989} | (Cohen, 1989) |
\citet{cohen1989} | Cohen (1989) |
\citep[see][p. 920]{cohen1989} | (see Cohen 1989, p. 920) |
\citealp{cohen1989} | Cohen, 1989 |
\citealt{cohen1989} | Cohen 1989 |
\citet*{schadkeetal2007} | Schadke, Sunstein & Hastie (2007)[1] |