rr-markdownmarkdown

How to Make a Line Break in rmarkdown Header?


I am trying to insert a line break inside the header paragraph in rmarkdown.

---
title: "Untitled"
output: word_document
---

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

## `r paste0("1. Attempt to set line break here ->  \n new line with value: ", someValue)`

## `r paste0("2. Attempt Line Feed, U+000A here ->  ",intToUtf8(strtoi("0x000A"))," new line with value: ", someValue)`

## `r paste0("3. Attempt Vertical Tab, U+000B here ->  ",intToUtf8(strtoi("0x000B"))," new line with value: ", someValue)`

## `r paste0("4. Attempt Form Feed, U+000C here ->  ",intToUtf8(strtoi("0x000C"))," new line with value: ", someValue)`

## `r paste0("5.  Carriage Return, U+000D here ->  ",intToUtf8(strtoi("0x000D"))," new line with value: ", someValue)`

## `r paste0("6. Next Line, U+0085 here ->  ",intToUtf8(strtoi("0x0085"))," new line with value: ", someValue)`

## `r paste0("7. Line Separator, U+2028 here ->  ",intToUtf8(strtoi("0x2028"))," new line with value: ", someValue)`

## `r paste0("8.  Paragraph Separator, U+2029 here ->  ",intToUtf8(strtoi("0x2029"))," new line with value: ", someValue)`

## `r paste0("9.  Breaks ->  <br> new line with value: ", someValue)`

## `r paste0("|\n  |10. Pipes break here ->   \n  | new line with value: ", someValue)`

## `r paste0("|\n  <center>11. Center tag here ->   </center>\n  <center>new line with value: ", someValue, "</center>")`

## Unsuitable <br>  
## as giving two separate paragraps and wrong TOC

This code is returning wrong output.

enter image description here

Unfortunately I failed to get any useful information from here as well.

So I am stuck. Please, can anyone help me to set the line break from inside the R code?

Appended

upon @MrFlick comment.

Here is the desired output - a multiline (single) paragraph with user defined positions of line breaks (equivalent of Word's SHIFT + ENTER).

enter image description here


Solution

  • You may use officedown

    ---
    title: "Line Break Solutions"
    output: word_document
    ---
    ```{r setup, include=FALSE}
    knitr::opts_chunk$set(echo = TRUE)
    library(officedown)
    ```
    ## This is my header `r officer::run_linebreak()` with a line break
    

    out

    Alternative 1

    If you run above code, then inspect the underlying XML structure of the resulting word, it gives

    <?xml version="1.0" encoding="UTF-8"?>
    <w:document
        xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"
        xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math"
        xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"
        xmlns:o="urn:schemas-microsoft-com:office:office"
        xmlns:v="urn:schemas-microsoft-com:vml"
        xmlns:w10="urn:schemas-microsoft-com:office:word"
        xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main"
        xmlns:pic="http://schemas.openxmlformats.org/drawingml/2006/picture"
        xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing">
        <w:body>
            <w:p>
                <w:pPr>
                    <w:pStyle w:val="Title" />
                </w:pPr>
                <w:r>
                    <w:t xml:space="preserve">Line</w:t>
                </w:r>
                <w:r>
                    <w:t xml:space="preserve"></w:t>
                </w:r>
                <w:r>
                    <w:t xml:space="preserve">Break</w:t>
                </w:r>
                <w:r>
                    <w:t xml:space="preserve"></w:t>
                </w:r>
                <w:r>
                    <w:t xml:space="preserve">Solutions</w:t>
                </w:r>
            </w:p>
            <w:bookmarkStart w:id="20" w:name="this-is-my-header-with-a-line-break" />
            <w:p>
                <w:pPr>
                    <w:pStyle w:val="Heading2" />
                </w:pPr>
                <w:r>
                    <w:t xml:space="preserve">This is my header</w:t>
                </w:r>
                <w:r>
                    <w:t xml:space="preserve"></w:t>
                </w:r>
                <w:r>
                    <w:br w:type="textWrapping"/>
                </w:r>
                <w:r>
                    <w:t xml:space="preserve"></w:t>
                </w:r>
                <w:r>
                    <w:t xml:space="preserve">with a line break</w:t>
                </w:r>
            </w:p>
            <w:bookmarkEnd w:id="20" />
            <w:sectPr />
        </w:body>
    </w:document>
    

    So copying the relevant part and parsing it within a openxml chunk also works without the need for library(officedown)

    ---
    title: "Linebreak Solutions"
    author: "Tim G"
    output: word_document
    ---
    
    ```{=openxml}
    <w:bookmarkStart w:id="20" w:name="this-is-my-header-with-a-line-break" />
    <w:p>
    <w:pPr>
    <w:pStyle w:val="Heading2" />
    </w:pPr>
    <w:r>
    <w:t xml:space="preserve">This is my header</w:t>
    </w:r>
    <w:r>
    <w:br w:type="textWrapping"/>
    </w:r>
    <w:r>
    <w:t xml:space="preserve">with a line break</w:t>
    </w:r>
    </w:p>
    <w:bookmarkEnd w:id="20" />
    ```
    

    and gives:

    out2

    which you can then try to mold into a function:

    ---
    title: "Linebreak Solutions"
    output: word_document
    ---
    
    ```{r, results='asis', echo=FALSE}
    linebreak_header <- function(id, bookmark_name, header_text, style = "Heading1") {
      
      strings <- strsplit(header_text, "<br>")[[1]]
      txts <- paste0(lapply(
        strings,
        \(x) paste0(
          '<w:r>\n<w:t xml:space="preserve">',
          x,
          ifelse(match(x, strings) != length(strings), '</w:t>\n</w:r>\n<w:r>\n<w:br w:type="textWrapping"/>\n</w:r>\n', '</w:t>\n</w:r>\n')
        )
      ) |> unlist(), collapse = "\n")
      
      cat(
        paste0(
        '```{=openxml}\n<w:bookmarkStart w:id="', id, '" w:name="', bookmark_name, '" />\n<w:p>\n<w:pPr>\n<w:pStyle w:val="', style, '" />\n</w:pPr>\n',txts, '</w:p>\n<w:bookmarkEnd w:id="', id, '" />\n```\n'
        )
      )
    }
    
    someValue <- 1
    linebreak_header(
      id = 20,
      bookmark_name = "Test",
      header_text = paste0(
        "Attempt to set line break here <br> new line <br> and another: ",
        someValue
      ),
      style = "Heading2"
    )
    ```
    

    finally