Reputation: 1165
I would like to force a linebreak in a long headline in an R markdown file knitted with knitr to both PDF and HTML output. At best, referencing of the headline should still work.
The following solutions proposed for related issues did not solve the issue: \\
, \n
, <br>
, <br>
(the latter two work for HTML only).
---
title: "TEST"
author: "Tester"
output:
pdf_document: default
html_document:
df_print: paged
---
# Fancy long headline in R Markdown
\newpage
# Not working:
## Fancy long headline in \\ R Markdown
# Work in HTML only:
## Fancy long headline in <br> R Markdown
## Fancy long headline in <br /> R Markdown
<!-- ## Fancy long headline in \n R Markdown -->
<!-- => ` \n` throws error in LaTex -->
\newpage
# Reference the headline
Reference the [fancy long headline in r markdown]
or similarly in bookdown [fancy-long-headline-in-r-markdown]
or via [myref](fancy-long-headline-in-r-markdown).
Any help is greatly appreciated.
Upvotes: 1
Views: 323
Reputation: 44977
If you want something that will work in PDF but don't care about HTML, define a new LaTeX command and use that. For example,
\newcommand{\Linebreak}{\\}
## Fancy long headline in \Linebreak R Markdown
If you want it to work in both, this should do it:
\newcommand{\Linebreak}{\\}
```{r include = FALSE}
Linebreak <- function()
if (knitr::opts_knit$get("rmarkdown.pandoc.to") == "html")
" <BR> " else " \\Linebreak "
```
## Fancy long headline in `r Linebreak()` R Markdown
It's not too hard to get the reference to work in PDF, but appears to be tricky in HTML; I'll leave that to you or someone else to work out.
Upvotes: 2
Reputation: 1577
Actually, you were almost there. You need to put the Latex Code in $...$
. Using this code
---
title: "TEST"
author: "Tester"
output:
pdf_document: default
html_document:
df_print: paged
---
# Fancy long headline in R Markdown
\newpage
# Working!
## Fancy long headline in $\\$ <br> R Markdown
gives
and
Upvotes: 2