Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to justify (to both sides) text in R Markdown when knitting in pdf output

I have looked for ways to control the alignment of the text, however I could not find anything for PDF outputs.

There is an existing answer, but related to HTML output only: How to justify the text to both sides when knitting html in rmarkdown.

like image 975
Adela Iliescu Avatar asked Apr 26 '18 11:04

Adela Iliescu


People also ask

How do you Knit a PDF in rmarkdown?

When you press “Knit to PDF” in RStudio, it converts your R Markdown document into LaTeX. Download MiKTeX from here: https://miktex.org/download • Run the installer, and restart your computer. Open an R Markdown file in RStudio, and try knitting to PDF. You may be prompted to install some packages.

How do you justify text in R?

if rendering to HTML, you can use the <p style='text-align:justify;'>your text here</p> tagset.

How do you show output in R Markdown in knitting?

Rendering output Better still, use the “Knit” button in the RStudio IDE to render the file and preview the output with a single click or keyboard shortcut (⇧⌘K).

How do I align text in RMD?

Aligning Text By default, all text in Markdown fields are aligned to the left. You can also align to the right, or center the content by wrapping the text in div tags.


1 Answers

R Markdown should default to using justified text. However, if you only want to export to PDF, we can directly use LaTeX commands within the document.using the standard arguments \centering \raggedright and \raggedleft, as explained here.

Here is a minimal example:

---
output: pdf_document
---

```{r, include = FALSE}
devtools::install_github("coolbutuseless/lipsum")
library(lipsum)
```

**Default**

`r lipsum[1]`

\centering

**Centered Text**

`r lipsum[1]`

\raggedright

**Ragged Right**

`r lipsum[1]`

\raggedleft

**Ragged Left**

`r lipsum[1]`

enter image description here

If you want to revert to justified text, you can use the ragged2e LaTeX package. You will need to load this within the YAML by adding:

---
output: pdf_document
header-includes:
  - \usepackage[document]{ragged2e}
---

\raggedleft

**Ragged Left**

`r lipsum[1]`



\justify

**Revert to Justified**

`r lipsum[1]`

Edit

If you are using the papaja template you need to include all the YAML. Not providing an author, shorttitle or another other field will cause it to crash.

---
title             : "The title"
shorttitle        : "Title"

author: 
  - name          : "First Author"
    affiliation   : "1"
    corresponding : yes    # Define only one corresponding author
    address       : "Postal address"
    email         : "[email protected]"
  - name          : "Ernst-August Doelle"
    affiliation   : "1,2"

affiliation:
  - id            : "1"
    institution   : "Wilhelm-Wundt-University"
  - id            : "2"
    institution   : "Konstanz Business School"

author_note: |
  Add complete departmental affiliations for each author here. Each new line herein must be indented, like this line.

  Enter author note here.

abstract: |
  Enter abstract here. Each new line herein must be indented, like this line.

keywords          : "keywords"
wordcount         : "X"

bibliography      : ["r-references.bib"]

figsintext        : no
figurelist        : no
tablelist         : no
footnotelist      : no
lineno            : yes
mask              : no

class             : "man"
output            : papaja::apa6_pdf
header-includes:
  - \usepackage[document]{ragged2e}
---

```{r load_packages, include = FALSE}

library(lipsum)
```
\justify

**Default**

`r lipsum[1]`

enter image description here

like image 164
Michael Harper Avatar answered Oct 22 '22 01:10

Michael Harper