This post was last updated on 24 April 2021.
Overview
This post is an aide-memoire for my future me. Throughout the recent months/years I have subsequently moved my writing to RMarkdown. With the advent of bookdown and blogdown various things have become smoother over time. Nonetheless, I find myself going back to my past projects to install the same (or similar) set of packages supporting the writing, copy the YAML, etc.
This post shoud save me from going back to past work, but have a centralised place for paste-and-copy the very first steps of all my projects.
Over time, I want to develop this post into a recipe.
When I speak about writing, I predominantly speak about
- research papers for conferences
- technical reports - longer forms of papers ranging from book chapters to longer research reports.
- blogging with blogdown - that might actually go elsewhere.
For research papers and technical reports, bookdown is the master package to augment RMarkdown with out-of-the-box cross-referencing for graphics, tables, and equations.
RMarkdown
RMarkdown is pretty cool and it is too straight forward to add more here.
Technical Reports
bookdown
has overcome a lot of the knitr/RMarkdown shortcomings in terms of cross-referencing.
For any type of writing the ‘long formats’ are actually my default go-to.
In particular, set YAML output to bookdown:: … :html_document2
, pdf_document2
, and word_document2
For specific confernce paper outputs use {rticles} and call the output format as follows:
---
...
output:
bookdown::pdf_book:
base_format: rticles::plos_article
---
Installation
obviously RMarkdown and bookdown. For RMarkdown open a new file in RMarkdown in RStudio. If not installed, this will result in the (automatic) installation of the required packages.
install.packages("bookdown)
Dependent on the study complement with tidyverse, lubridate, ggplot2
YAML - Document Set-Up
This is my minimal standard YAML
---
title: "Security Management System"
author: "RQ"
date: "19. March 2019"
output:
bookdown::pdf_document2 # or word_document2
bibliography: [chapter-SecMS-bib.bib]
---
Rendering to pdf works as a charm. Ditto for MS Word (if needed).
Tables
For a start knitr::kable()
possibly augmented with kableExtra
features gets you far.
I recently started using flextable
as it nicely prints in pdf AND Word!
Flextable (c.f. below) seamlessly blends in with bookdown referencing and the knitr code-chunk tab.cap
which eases the pain of remembering different processes.
Cross-Referencing Headers
To cross-reference a header you need to
- (ideally) assign a header id
- add the cross-reference as appropriate
Headers are normally assigned an id “hyphening” the header text. For example, the header # Overview of Report
will be turned into the id overview-of-report
.
To allow for more control, assign an id within curly braces {} and a hashtag just before the chosen id. This id reference is added at the end of the header, i.e. # Overview of Report {#my-header-id}
.
Cross-reference to the id with the standard reference pointer: \@ref(my-header-id)
.
Including and Cross-Referencing Graphics or Tables
There are 3 parts to using a graphic or table
- including the graphic or creating the table
- definition of the caption
- cross-referencing the graphic
The following is a paste-and-copy skeleton.
Note that with a recent knitr update the chunk option tab.cap has been introduced, thus replace fig.cap
with tab.cap
to get the desired result for tables.
(ref:cool-caption) This is my cool caption text
```{r cool-caption, fig.cap="(ref:cool-caption)", fig.height=4, echo=FALSE}
knitr::include_graphics("figures/my-cool-figure.png")
```
Lorem ipsum blablabla ... c.f. Fig. \@ref(fig:cool-caption))
Conceptually, the r-chunk label can be different which in turn requires the \@ref()
part to link to the different label.
To avoid confusion or frustration, simply use the same label and refernce pointer.
Note that the caption reference placeholder is (ref: ...)
, while the reference to the figure is \@ref(fig: ...)
:)
For tables this can look like the following:
(ref:cool-table) Add a cool table caption here
```{r cool-table, tab.cap="(ref:cool-table)"}
knitr::kable(my-cool-table-df)
```
Table \ref@(tab:cool-table) lalala ... alala
Equations and cross-referencing equations
Inline equations can be placed between single dollar signs $, e.g. \(A^2=A*A\). An equation environment can be inserted with double $$ dollar signs.
However, the cross-referencing for equations and associated numbering works only reliable for pdf output when the equation is properly placed with an LATEX environment.
You may consider editing the equation in $$
to get a formula preview.
Once completed, replace the $$
with \begin{equation}
and \end{equation}
.
The equation block will then look like the following:
\begin{equation}
(\#eq:addTXOT)
avgAddTXOT =\frac{\sum_{f_i}{ actualTXOT_i - refTXOT_{(stand,rwy)} }}{\sum_{f_i}{i}}
\end{equation}
see \@ref(eq:addTXOT)
The rticles packages offers a nice extension to bookdown::pdf_document by providing pdf/LATEX templates for a extensive set of journals/conferences.
For the IEEE rticle format the bookdown equation reference threw an error as the control command requires the amsmath
package installed.
This is now solved for the rticles IEEE format (c.f. rticles issue #289).
If you run into similar problems (older rticles package or another base format, c.f. rticles issue #289, related question on SO) expand your YAML to include the extra_dependencies
pointer:
...
output:
bookdown::pdf_book:
base_format: rticles::ieee_article
extra_dependencies: amsmath
...
Bibs
It depends a bit on the paper/report and workflow. Conceptually, a bibtex file entails the references to the cited material. Bibtex as such has a multitude of entries for all sort of things ranging from books to webpages. Bibtex is not straightforward, but within certain limits one can easily handle the bib by hand. For example, I copy the bibtex entry for a reference from scholar.google (or other sources, e.g. IEEE).
!!! If you do bibtex by hand, with the exception of the last entry, check for the comma at the end of each value line!
For more sophisticated work and bibs the use of a Reference Manager Tool is strongly recommended. Zotero is a great tool. If offers some nice functionality. Amongst others, one can automatically update a (sub-)collection (read: biblio for your paper). Storing the (sub-)collection in the respecive project folder/repo offers a great workflow for managing references while you go and keep and maintain them in one application.
My aforementioned doing-it-by-hand-approach is not completely off. If hooked up with Zotero, Zotero detects a changed repo and will also read in the changed information. If not hooked up with Zotero, the resulting project bib can be readily read in and stored.
Thus, the benefit of going for the Zotero-subcollection approach is that one does not need to check for the respective bibtex formats for a reference, and there is no double work for references already stored in the (master) collection.
Working with MS Word
Let’s face it. We live in a world where people around us will continue to use Microsoft Word no matter what. Thus, from time to time, you will have to revert to this output format.
RMarkdown supports a few nice gimmicks that also works with MS Word. One of the nicer things is the (re-)use of a specific Word template. Such a template file can be set as an option in the YAML header:
---
title: "My word document"
output:
word_document:
reference_docx: my-template-file-name.docx
---