Customising R Markdown PDF Document
R Markdown is a powerful tool for creating documents that combine text, images, and code into a single file. One of the features of R Markdown is its ability to generate PDF documents with ease. However, by default, the author field in the YAML header does not behave as expected, especially when it comes to formatting affiliations.
In this article, we will explore how to modify the default R Markdown YAML header to get customised output in the author field. We will cover three specific requirements:
- Avoiding italics for affiliations
- Adding space between the author and affiliation
- Reducing font size for affiliations alone
Understanding R Markdown YAML Header
The YAML header is a section of the R Markdown file that contains metadata about the document, such as the title, authors, date, output format, etc. The YAML header is used to configure various aspects of the document generation process.
In this case, we are focusing on the author field in the YAML header. This field allows us to specify the author(s) of the document.
The Issue with Default R Markdown YAML Header
By default, R Markdown uses a specific format for the author field, which includes italics for affiliations. However, this format can be limiting when trying to accommodate multiple authors and affiliations in a custom way.
For example, consider the following YAML header:
---
title: "Title"
author: |
| Author1^1^, Author2^2^, Author3^1^ and Author4^2^
| 1. Affiliation1
| 2. Affiliation2
date: "Wednesday, April 10, 2015"
output:
pdf_document
---
As we can see, the affiliations are displayed in italics using superscript notation (^). However, this format does not allow for added space between the author and affiliation.
A Custom Solution Using R’s biograph Package
One way to address these issues is by using R’s built-in biograph package, which allows us to customize the output of the author field in a more flexible way.
First, we need to install and load the biograph package:
{< highlight language="r >}
install.packages("biograph")
library(biograph)
</highlight>```
Next, we can use the `biograph` function to customize the author field. Here is an example of how to do this:
```markdown
---
title: "Title"
author: |
{< biograph name="Author1" affiliation="Affiliation1" >}
Author2^2^, Author3^1^ and Author4^2^
{< /biograph >}
address: Instituto de Astronomía, Geofísica e Ciências Atmosféricas, Universidade de São Paulo
abstract: abstract
bibliography: mybibfile.bib
output: rticles::elsevier_article
---
In this example, we use the biograph function to customize the author field. We specify the name and affiliation for each author using the name and affiliation arguments.
Reducing Font Size for Affiliations Alone
To reduce the font size for affiliations alone, we can use the fontsize argument in the biograph function:
---
title: "Title"
author: |
{< biograph name="Author1" affiliation="Affiliation1" fontsize=10 >}
Author2^2^, Author3^1^ and Author4^2^
{< /biograph >}
address: Instituto de Astronomía, Geofísica e Ciências Atmosféricas, Universidade de São Paulo
abstract: abstract
bibliography: mybibfile.bib
output: rticles::elsevier_article
---
In this example, we set the font size for affiliations to 10 points using the fontsize argument.
Adding Space Between Author and Affiliation
To add space between the author and affiliation, we can use a newline character (\n) in the YAML header:
---
title: "Title"
author: |
| Author1^1^
\n
| Author2^2^, Author3^1^ and Author4^2^
{< /biograph >}
address: Instituto de Astronomía, Geofísica e Ciências Atmosféricas, Universidade de São Paulo
abstract: abstract
bibliography: mybibfile.bib
output: rticles::elsevier_article
---
In this example, we add a newline character between the author and affiliation using \n.
Conclusion
Customizing the author field in R Markdown YAML header can be a bit tricky, but with the help of R’s built-in biograph package, it is definitely possible. By using the biograph function to customize the output, we can avoid italics for affiliations, add space between the author and affiliation, and reduce font size for affiliations alone.
We hope this article has provided a good starting point for those looking to customize their R Markdown YAML header and create documents with custom authors and affiliations.
Last modified on 2024-01-22