Reputation: 321
Is there any way to avoid overshooting lines that go beyond the margin of a page if you knit a RMarkdown document to a PDF?
Illustration:
# RMarkdown file
title: "xyz"
author: "John Doe"
date: "23 2 2021"
output:
pdf_document:
latex_engine: xelatex
toc: true
toc_depth: 5
html_document: default
mainfont: Times New Roman
fontsize: 12
text
more text
```{r}
print("This is a very long line illustrating my question that drives me nuts. It should include more text that is supposed line-break if it goes beyond specified margins.)
a <- 10
b <- sqrt(2)
print(paste("This is also a use case that I am looking at right now. I want to print results in the PDF documents of a calcualtion and put it in context.", a, "divided by square root of 2 results in", (a/b))
```
Any way to force line breaks if text goes out of bound? I tried editing YAML headers in the Markdown file, creating preamble files and reference them in the YAML header, trying various latex engines to knit my document as PDF, defining margins for the PDF document, manually splitting long print()
calls to span several lines in the source file and adhering to the max. number of characters per line, but all failed so far. HTML output seems to be just fine.
Would highly appreciate any pointers to already provided answers in the past, as I am sure that I am not the only one having experienced these kind of problems, but unfortunately I am unable to find a fitting solution to my problem.
Upvotes: 2
Views: 2190
Reputation: 26484
A potential solution is to change the width using '--listings' (per https://bookdown.org/yihui/rmarkdown-cookbook/text-width.html), e.g.
Create a file called "preamble.tex" in the directory where you are knitting and include the lines:
\lstset{
breaklines=true
}
Then change the YAML and knit:
> --- title: "xyz" author: "John Doe" date: "23 2 2021" output:
> pdf_document:
> pandoc_args: --listings
> toc: true
> toc_depth: 5
> includes:
> in_header: preamble.tex
> html_document: default
> mainfont: Times New Roman
> fontsize: 12
> ---
>
> ```{r setup, include=FALSE}
> knitr::opts_chunk$set(echo = TRUE)
> options(width = 80)
> ```
>
> text
> more text
> ```{r}
> print("This is a very long line illustrating my
> question that drives me nuts. It should include more text that is
> supposed line-break if it goes beyond specified margins.")
>
> a <- 10 b <- sqrt(2) paste("This is also a use case that I am
> looking at right now. I want to print results in the PDF documents of
> a calcualtion and put it in context.", a, "divided by square root of 2
> results in", a/b, sep = " ")
> ```
>
> ## R Markdown
>
> This is an R Markdown document. Markdown is a simple formatting syntax
> for authoring HTML, PDF, and MS Word documents. For more details on
> using R Markdown see <http://rmarkdown.rstudio.com>.
>
> When you click the **Knit** button a document will be generated that
> includes both content as well as the output of any embedded R code
> chunks within the document. You can embed an R code chunk like this:
>
> ```{r cars}
> summary(cars)
> ```
>
> ## Including Plots
>
> You can also embed plots, for example:
>
> ```{r pressure, echo=FALSE}
> plot(pressure)
> ```
>
> Note that the `echo = FALSE` parameter was added to the code chunk to
> prevent printing of the R code that generated the plot.
Another potential solution (what I usually do) is to output the RMarkdown as html using options(width = 80)
in the {r setup} chunk, then convert the html to pdf with https://wkhtmltopdf.org/. If you're using macOS you can install wkhtmltopdf with homebrew (brew install wkhtmltopdf
). The advantage to this approach is that it maintains any html-specific formatting i.e. you can use themes
Upvotes: 3