Home > Net >  Wrapping A Line Without Spaces In R-Markdown Output
Wrapping A Line Without Spaces In R-Markdown Output

Time:09-03

Writing the following R-Markdown chunk at the beginning of an R-Markdown file allows me to wrap a line in R-Markdown PDF output, provided that the R package formatR is installed and the line to wrap has a whitespace character. How do I wrap a line if the line does not have a whitespace character?

```{r, echo = FALSE}
library(knitr)
opts_chunk$set(tidy.opts = list(width.cutoff = 27), tidy=TRUE)
```

For example, the following R chunk demonstrates failure to wrap and wrapping after 27 characters.

```{r, eval=TRUE, echo=TRUE, results="show", message=FALSE}
# devtools::install_github("tslever/Tom_Levers_Git_Repository/TomLeversRPackage")
# devtools::install_github( "tslever/Tom_Levers_Git_Repository/TomLeversRPackage")
```

enter image description here

I would like the first command to render as

# devtools::install_github(
"tslever/Tom_Levers_Git_Rep
ository/TomLeversRPackage")

Possibly another question: With the above options, the following R chunk is rendered as one line. How do I prevent this reformatting, and still wrap after 27 characters?

```{r, eval=TRUE, echo=TRUE, results="show", message=FALSE}
test.data.frame <- read.csv(
    file = "C:/Users/Tom/Documents/Tom_Levers_Git_Repository/test.csv"
)
```

enter image description here

CodePudding user response:

HTML output

You can use some custom CSS; here is a minimal RMarkdown example:

---
title: "Untitled"
output: html_document
date: "2022-09-02"
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

```{css, echo=FALSE}
pre {
    width: 30ch;
}
```

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

```{r, eval=TRUE, echo=TRUE, results="show", message=FALSE}
# devtools::install_github("tslever/Tom_Levers_Git_Repository/TomLeversRPackage")
# devtools::install_github( "tslever/Tom_Levers_Git_Repository/TomLeversRPackage")
```

This limits the width of code chunks to 30 characters (width: 30ch) and produces

enter image description here

PDF output

---
title: "Untitled"
output: 
    pdf_document:
        pandoc_args: --listings
        includes:
            in_header: preamble.tex
date: "2022-09-02"
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

```{r, eval=TRUE, echo=TRUE, results="show", message=FALSE}
# devtools::install_github("tslever/Tom_Levers_Git_Repository/TomLeversRPackage")
# devtools::install_github( "tslever/Tom_Levers_Git_Repository/TomLeversRPackage")
```

with the following preamble.tex

\lstset{
    breaklines=true,
    basicstyle=\ttfamily,
    linewidth=30ex
}

This produces

enter image description here

Note that this probably requires some tuning/tweaking of listings parameters. The key here is that you need breaklines=true to enable breaking long lines, and then define a line width with e.g. linewidth=30ex. Note that this won't exactly give you 30 characters, as this is not how widths are specified in LaTeX; so you'll need to adjust the width to suit your aesthetic needs.

CodePudding user response:

Based on Maurits Evers's work,

preamble.tex

\lstset{
    backgroundcolor = \color{lightgray},
    breaklines=true,
    breakindent=0ex,
    basicstyle=\ttfamily,
    linewidth=109ex
}

and Test.Rmd

---
title: "Test"
output: 
    pdf_document:
        pandoc_args: --listings
        includes:
            in_header: preamble.tex
date: "2022-09-02"
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

```{r, eval=TRUE, echo=TRUE, results="show", message=FALSE}
# devtools::install_github("tslever/Tom_Levers_Git_Repository/TomLeversRPackage")
# devtools::install_github( "tslever/Tom_Levers_Git_Repository/TomLeversRPackage")
"<string of a's as long as the immediately above command that I cant enter in SO>"
```

yielded

enter image description here

  • Related