Reputation: 1005
I have an RMarkdown file that I want to publish to both HTML and PDF. I use a page break command, \newpage
, in the file to insert a page break in the PDF output. However, in the HTML output, the \newpage
shows up as part of the content.
How do I get knitr/pandoc to omit the \newpage
from the HTML output?
I tried embedding the \newpage
in an HTML comment, but then it had no effect in the PDF output.
Here is the Rmd file.
---
title: 'RMarkdown Test'
author: "Carl Parker"
date: "July 16, 2017"
output:
pdf_document: default
html_document:
keep_md: yes
---
# Page 1 #
\newpage
# Page 2 #
**--- END ---**
Here is the code that builds/renders.
library( "rmarkdown" )
library( "knitr" )
render( "test-1.rmd", output_format = "all" )
# --- END ---
Upvotes: 0
Views: 457
Reputation: 6661
If you want that a code execute only for pdf and not for html, you may use function knitr::pandoc_to()
as follows. You can also use it to write some text only for html or for pdf:
Be careful with \
, it needs to be doubled \\
when called in a paste
or cat
inside a R code.
---
title: "RMarkdown Test"
author: "Carl Parker"
date: "July 16, 2017"
output:
html_document:
keep_md: yes
pdf_document: default
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
# Page 1 #
<!-- use newpage for latex only -->
`r if(knitr:::pandoc_to() == "latex") {paste("\\newpage")}`
<!-- Specific text for html or pdf -->
`r ifelse(knitr:::pandoc_to() == "html", "Text in html output only", "Text in pdf output only")`
# Page 2 #
**--- END ---**
Upvotes: 1