Search code examples
line-breaksbookdownpdflatex

In pdf document created with bookdown codelines are too long


I am trying to write my thesis with bookdown, via RStudio, since I am not so familiar with Latex. I want to add the code I generated for the project in the appendix but the lines don't break so the code always runs over the page.

I tried so much over a long period that I don't know anymore what I tried or what maybe interferes. I tried to change the size of in the code chunk, but it didn't do anything (so here: {r app, eval = FALSE, size = "tiny"} )

Another idea was to change the rotation of the paper but I couldn't manage that either, maybe I wrote it on the wrong page?

I would appreciate it so much if anyone can help me. Thank you!

My index file looks like this:

---
title: "Thesis"
site: "bookdown::bookdown_site"
documentclass: book
output:
bookdown::pdf_book:
keep_tex: true
extra_dependencies: ["float"]
citation_package: biblatex
toc: true
toc_depth: 3
includes:
in_header:
- \usepackage{pdfpages}
bibliography: [file.bib]
link-citations: yes
geometry: "left=4cm, right=3cm, top=2.5cm, bottom=2.5cm"
---

And the head of the appendix document with an example of code that ist way too long:
# Appendix: Codes {.unnumbered #appendix}
\markboth{APPENDIX}{}

## app.R {-}
```{r app, eval = FALSE} 
  # Inputs ------------------------------------------------------------------

  ## Transform excel input data
  input_data <- reactive({
    req(input$file)
    
    # functions used here are generated in 02_functions.r
    headers  <- read_excel(input$file$datapath, col_names = FALSE, n_max = 2) %>% tibble::as_tibble() %>% mutate(...3 = NA)
    data     <- read_excel(input$file$datapath, col_names = FALSE, skip = 2)  %>% tibble::as_tibble()
    return(data)
  })

```

This leads to something like this:

enter image description here

I tried:

  • to change the code size in the code chunk options
  • to change the rotation of the page
  • to use \usepackage{fvextra}
  • to use \DefineVerbatimEnvironment{Highlighting}{Verbatim}{breaklines,commandchars=\{}}
  • to use \lstset{breaklines=true} in the in_header section of the index.Rmd
  • to use an extra tex file with %\usepackage{fvextra} %\DefineVerbatimEnvironment{Highlighting}{Verbatim}{breaklines,commandchars=\{}}

Solution

  • ---
    title: "Thesis"
    site: "bookdown::bookdown_site"
    documentclass: book
    output:
      bookdown::pdf_book:
        keep_tex: true
    extra_dependencies: ["float"]
    citation_package: biblatex
    toc: true
    toc_depth: 3
    header-includes:
    - \usepackage{pdfpages}
    - \usepackage{fvextra}
    - \DefineVerbatimEnvironment{Highlighting}{Verbatim}{commandchars=\\\{\},breaklines=true, breakanywhere=true}
    link-citations: yes
    geometry: "left=4cm, right=3cm, top=2.5cm, bottom=2.5cm"
    ---
    
    And the head of the appendix document with an example of code that ist way too long:
    # Appendix: Codes {.unnumbered #appendix}
    \markboth{APPENDIX}{}
    
    ## app.R {-}
    ```{r app, eval = FALSE} 
      # Inputs ------------------------------------------------------------------
    
      ## Transform excel input data
      input_data <- reactive({
        req(input$file)
        
        # functions used here are generated in 02_functions.r
        headers  <- read_excel(input$file$datapath, col_names = FALSE, n_max = 2) %>% tibble::as_tibble() %>% mutate(...3 = NA)
        data     <- read_excel(input$file$datapath, col_names = FALSE, skip = 2)  %>% tibble::as_tibble()
        return(data)
      })
    
    ```
    

    enter image description here