# Writing a book with a little help from Emacs and friends

(This article was first published on Omnia sunt Communia! » R-english, and kindly contributed to R-bloggers)

This post provides technical details about the making of my book “Displaying time series, spatial, and space-time data with R”, a fully reproducible book including hundreds of code chunks and graphics. Hopefully this post will help others when writing a reproducible document.

# Tools

Among the tools that can create reproducible documents with R, I decided to use these gems of open-source software:

# Each chapter is a separate orgmode file

I use a different orgmode file for each chapter. Each file begins with a simple header. The first line uses #+PROPERTY to specify buffer-wide header arguments (it sets session to R). The second line uses #+OPTIONS to define export settings (^:nil disables TeX-like syntax for sub- and superscripts).

#+PROPERTY:  header-args :session *R*
#+OPTIONS: ^:nil

When I finished the code and figures in a chapter, but I need to make minor changes and corrections in the text, I need to disable code evaluation. Then the first line of the header is

#+PROPERTY:  header-args :session *R* :eval no-export

An example of an orgmode file follows. You have to export it with C-c C-e C-b l l. With C-c C-e the export dispatcher is started and l l chooses the LaTeX exporter and produces a file. C-b sets body-only on, and the LaTeX file will only include the body without headers.

The content of the file shows rendered by GitHub. Click here for the raw content.

The exported LaTeX file is here:

# A LaTeX file for each Part

Each of these exported files is a children of a LaTeX file with a very simple structure. It starts with \part and includes several \chapter. Under each \chapter you will find an \input statement that imports the LaTeX file produced with our orgmode file into its parent.

# A master LaTeX file for the book

Finally, there is a master file that includes the LaTeX preamble and several \include commands to import the previous parent files. It is important to note that the parent LaTeX files include three lines at the end.

%%% Local Variables:
%%% TeX-master: "main.tex"
%%% End:

This code is used by AUCTeX to work with a multifile structure and run commands on the master file.

The master file loads the memoir class, a powerful class extremely useful for designing books. It offers an extensive manual with more than 500 pages containing also examples for the design of a book and of a thesis.

The code blocks are highlighted with the listings package with this configuration:

\lstset{
%keywordstyle=\color{Blue},
% stringstyle=\color{OliveGreen},
basicstyle=\ttfamily\small,
columns=fullflexible,
breaklines=true,
linewidth=\textwidth,
backgroundcolor=\color{gray!10},
basewidth={0.5em,0.4em},
%  frame=single,
literate={á}{{\'a}}1 {ñ}{{\~n}}1 {é}{{\'e}}1 {ó}{{\'o}}1 {º}{{\textordmasculine}}1
}

This is the master file: