Rich
Rich

Reputation: 141

How to fit console output in pdf generated by Rmarkdown?

enter image description hereenter image description here

I am trying to print str(df) in pdf output of rmarkdown. How do I fit the wide output in pdf?

Upvotes: 1

Views: 1031

Answers (1)

Quinten
Quinten

Reputation: 41533

For PDF output is a bit trickier to wrap lines. One option is using the LaTeX package listings. I will use mtcars dataset as example. Your Rmarkdown should enable the package via the Pandoc argument --listings like this:

---
title: "Test"
output:
  pdf_document:
    pandoc_args: --listings
    includes:
      in_header: preamble.tex
---

```{r, echo=FALSE, warning=FALSE, message=FALSE}
options(width = 300)
str(mtcars)
```

In preamble.tex, you set an option of the package in a different file:

\lstset{
  breaklines=true
  basicstyle=\ttfamily
}

This is the output:

enter image description here

For more info check this link: https://bookdown.org/yihui/rmarkdown-cookbook/text-width.html

Upvotes: 1

Related Questions