Here’s a list of a few extra issues you might run into while writing your thesis, and some potential solutions. Most of this is based on my own experience, so your mileage may vary. If I hear of any additional bottlenecks I’d be happy to include them.
While writing the thesis, you might want to output it to a
.docx
file, say if your supervisors prefer to work with
Microsoft Word instead. This is possible, although not all
bookdown
/ RMarkdown features are supported. It will get
you most of the way there, but you might have to edit the Word file a
bit, or inform your supervisors that this is not exactly what the final
version will look like.
To render the whole thesis to one Word file, run the following:
bookdown::render_book("index.Rmd", output_format = "bookdown::word_document2")
You can also render a single chapter (e.g. if you want to send it out for feedback):
# render chapter 2 to Word
bookdown::preview_chapter("02-chapter.Rmd", output_format = "bookdown::word_document2")
In both cases, you should now find a thesis.docx
file in
the output folder (/_book/
) by default.
Some sections of the thesis will have to be in Dutch (i.e., the Dutch summary). In others (e.g., the acknowledgments), you might want to use another language of your choice. To make sure things like hyphenation will work properly, you should not just start typing in a different language, but encase all non-English text within special language tags.
Here’s how you do it:
This paragraph uses the default language (English).
::: {lang=nl}
> Deze paragraaf is in het Nederlands.
:::
This sentence is in English. ['Dieser Satz ist auf Deutsch.']{lang=de} And now back to English.
Note though that to use the proper Dutch hyphenation rules, you’ll
need to install the additional LaTeX package hyphen-dutch
.
This cannot be auto-detected, so you’ll have to add it manually:
tlmgr install hyphen-dutch
Or from R, using tinytex
:
tinytex::tlmgr_install("hyphen-dutch")
Also see this entry in
pandoc
’s documentation and the examples in the
template’s Dutch Summary and Acknowledgments chapters.
To render image/figure files, I’d recommend
knitr::include_graphics()
.
This allows you to include both bitmap and vector versions of the
same figure. Most commonly this would be, a .png
(bitmap)
version of the figure for html
output, and a
.pdf
(vector) figure for the pdf
version of
the thesis. The auto_pdf
option will insert the figure in
vector format for pdf output automatically, or use the specified figure
otherwise (in bitmap). For example:
knitr::include_graphics("figures/my_figure.png", auto_pdf = TRUE)
Simpler tables can be inserted in Markdown, or created
from R data frames using the knitr::kable()
function. More
complex tables (with row/column-spanners, formatting inside the table,
etc.) can be created with R packages such as papaja (for APA style) or kableExtra (for pretty
much anything you might want).
Citations are handled by pandoc (using the
citeproc
extension) by default. The citation style (e.g., MLA, APA,
Chicago, but also according to specific journals) can be customized by
downloading the corresponding .csl
file from the Zotero Style Repository. For
example, to use APA style (through apa.csl
, placed in the
root directory of your thesis), add the following YAML line to
index.Rmd
, underneath whatever output you want to use the
style for (bookdown::pdf_book()
or
bookdown::gitbook()
):
# N.B. the template already contains this line, but it is commented out
pandoc_args: [ "--csl", "apa.csl" ]
If you want to customize the output further, you could consider using
a LaTeX package, such as biblatex instead of
pandoc-citeproc
(Also see my PhD thesis, which used
biblatex
). This will offer you much more flexibility, at
the expense of complexity, and it will only work for pdf outputs. To do
that, add the following line (indented under
bookdown::pdf_book()
) to index.Rmd
instead:
output:
bookdown::pdf_book:
citation_package: biblatex
Sometimes you might want a certain section of the thesis (a
paragraph, a figure, or even a whole chapter) to occur only in the html
(or pdf) version of the thesis, but not in the other. For example, for
the amsterdown
template, the “Preface” chapter
(_00-preface.Rmd
) is only included in the html version, not
in pdf (check out index.Rmd
to see how this is done).
You can use the knitr::is_html_output()
and
knitr::is_latex_output()
functions for this purpose. These
are conditionals that return TRUE
only when the output
format you are rendering to is html/latex.
For example, to include a section of text only in html, you can use some Inline R code:
`r if (knitr::is_html_output()) ' This text will not show when rendering to pdf '`
Or, to only run a code chunk (say, that includes a figure) for the pdf version, you can conditionally evaluate it using chunk options:
```{r include = knitr::is_latex_output()}
knitr::include_graphics("my_fig.pdf") # this figure will be omitted from the html
```
LaTeX puts things like figures and tables—that cannot be broken over multiple pages—inside floating environments, or floats for short. LaTeX is smart about figuring out the best positions for floats, but it’s bound to not always work: once in a while it’s gonna place a figure earlier or later than you’d like.
You can use the fig.pos
chunk option to guide the
placement of figures in floats. See a list of all the placement
specifiers here.
For tables, you’ll have to use a package like kableExtra to achieve
the same.
For example, to try and place a figure “here” (i.e., close to where
you put the command in the source text), try the h
option:
```{r fig.cap='test', out.width='100%', fig.pos='h'}
knitr::include_graphics("my_fig.png")
```
(Note that you may need to also specify some other chunk options
for fig.pos
to become active)
A book-style document usually has the current chapter/section title in the upper portion of the page (the “header”). If the title of a particular chapter is really long, this might cause the title to run off the page in the header.
To fix this, you can use the LaTeX \chaptermark{}
command and put it beneath the title of the chapter (see the example in
Chapter 2 of the template). Specify a shorter title for the chapter
within the curly braces: this short title will then be used for the
headers.
Inline code allows you to include the output of your R code (e.g. statistical tests) within the text of your thesis. It is a lot easier and more reproducible than inserting results by hand!
You can do this using the following syntax:
`r <your R code here>`
.
For example:
`r 3/12*100`% of patients... We included
will read:
We included 25% of patients…
Often the result you’ll you want to insert will already be assigned
to a variable, in which case you can simply call the variable in your
inline code: `r <your variable>`
. Checkout the papaja package for some
awesome functions that will insert statistical results in APA style.
If you want to run code in other languages in a chunk (e.g. Python),
all you need to do is replace r
in the chunk options with
another
language engine:
```{python}
# Now you can write python code
python_number = 20+4
```
If you’re using Python, definitely checkout the reticulate package,
which provides a lot of functionality for using Python in R. For
example, you can access Python variables (like the one we created above)
within your R session using the py$<my variable>
syntax:
library(reticulate)
py$python_number
#> [1] 24
You can also use this principle to insert Inline code:
`r py$python_number`