Rmarkdown each chunk on separate pages [duplicate] - r

I wonder if one could simply use LaTeX \newpage command in R markdown v2 in a different way than this:
```{r, results='asis', echo=FALSE}
cat("\\newpage")
```
I produce pdf_output.
If any1 has any idea please do not hesitate to comment :) !
Thanks
I create pdf like this:
---
title: " "
author: " "
date: "2014"
output:
pdf_document:
includes:
in_header: naglowek.tex
highlight: pygments
toc: true
toc_depth: 3
number_sections: true
keep_tex: true
---

Simply \newpage or \pagebreak will work, e.g.
hello world
\newpage
```{r, echo=FALSE}
1+1
```
\pagebreak
```{r, echo=FALSE}
plot(1:10)
```
This solution assumes you are knitting PDF. For HTML, you can achieve a similar effect by adding a tag <P style="page-break-before: always">. Note that you likely won't see a page break in your browser (HTMLs don't have pages per se), but the printing layout will have it.

In the initialization chunk I define a function
pagebreak <- function() {
if(knitr::is_latex_output())
return("\\newpage")
else
return('<div style="page-break-before: always;" />')
}
In the markdown part where I want to insert a page break, I type
`r pagebreak()`

You can make the pagebreak conditional on knitting to PDF. This worked for me.
```{r, results='asis', eval=(opts_knit$get('rmarkdown.pandoc.to') == 'latex')}
cat('\\pagebreak')
```

If you're having problems with \newpage or \pagebreak and floating figures/tables. You need to use \clearpage, as answered here.

Related

How can I customize and style tabset.dropdown in R Markdown?

I want to change the apperance of my dropdown tabset in R Markdown. For example, I want my dropdown to expand on hover and change the overall apperance of the box. Furthermore I want to change font, color, and text-alignment within the dropdown. I have tried a lot of different methods in my .css file without any success. See code and picture below.
This is the dropdown I want to customize
---
output:
html_document:
theme: paper
highlight: tango
number_sections: false
toc: false
toc_float: false
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## Title {.tabset .tabset-fade .tabset-pills}
### Subject1 {.tabset .tabset-dropdown}
#### How can i customize this?
#### How can i customize this?
### Subject2 {.tabset .tabset-dropdown}
#### How can i customize this?
#### How can i customize this?
You need to add styles.css in your r code at the top, and also as a new file in your folder.
R Code
---
output:
html_document:
theme: paper
highlight: tango
number_sections: false
toc: false
toc_float: false
css: styles.css
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## Title {.tabset .tabset-fade .tabset-pills}
### Subject1 {.tabset .tabset-dropdown}
#### How can i customize this?
#### How can i customize this?
### Subject2 {.tabset .tabset-dropdown}
#### How can i customize this?
#### How can i customize this?
CSS code (styles.css):
.nav-tabs > li.active:nth-child(1) > a {
background-color: #E8C120;
}
.nav-tabs > li > a {
background-color: #5882A6;
color: red;
}
OUTPUT:

Cannot render custom blocks with cat()

As explained in this chapter of the R Markdown Cookbook, it is possible to make custom blocks in R Markdown files with this syntax (here, to center some text):
:::{.center data-latex=""}
Hello
:::
However, using cat() to render this sort of block (as explained in this chapter) does not work.
Full example:
---
output: rmarkdown::pdf_document
---
<!-- WORKS -->
:::{.center data-latex=""}
Hello
:::
<!-- DOES NOT WORK -->
```{r, results='asis', echo=FALSE}
print(
'
:::{.center data-latex=""}\n
Hello\n
:::
'
)
```
Why is the second part not rendering correctly? How to solve that?
There are two issues:
You use print() instead of cat() (even if you refer to cat() in your post
You put spaces at the beginning of each line which prevents the custom block to be rendered.
If you prefer the lines to be indented (as I do) then I would suggest make each line a separate string as I do in the second example. This also has the advantage that you could add the line breaks via the sep argument instead of having to add them manually.
Reproducible example:
---
output:
rmarkdown::pdf_document:
keep_md: true
---
<!-- WORKS -->
:::{.center data-latex=""}
Hello
:::
<!-- WORKS TOO -->
```{r, results='asis', echo=FALSE}
cat('
:::{.center data-latex=""}\n
Hello\n
:::
', sep = "\n"
)
```
<!-- WORKS TOO -->
```{r, results='asis', echo=FALSE}
cat(':::{.center data-latex=""}',
'Hello',
':::', sep = "\n"
)
```

R Pagedown resume/cv lowercase letters for title

I am working on creating a resume in R using Pagedown. Currently, the default is to have your name in all capital letters at the top of the first page (e.g. JANE DOE). However, this doesn't look great with my name and I am wondering if there is a way to edit just the title line to have mixed case (Jane Doe). Thanks!
It is possible to remove the uppercase text transformation with CSS like that:
---
title: "Lijia Yu's resume"
author: Lijia Yu
date: "`r Sys.Date()`"
output:
pagedown::html_resume:
# set it to true for a self-contained HTML page but it'll take longer to render
self_contained: false
# uncomment this line to produce HTML and PDF in RStudio:
#knit: pagedown::chrome_print
---
```{css, echo=FALSE}
#title h1 {
text-transform: unset;
}
```
Aside
================================================================================
...

Rendering problem cross-reference equation in r markdown HTML

I usually reference equations in rmd using \label{} and \eqref{} combination. (I know \#ref, but this seems only works in bookdown::pdf_document or bookdown::html_document) For example,
---
title: "Untitled"
author: "Blended"
date: '2019 3 14 '
output:
html_document: default
pdf_document: default
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(comment = "#>")
```
\begin{equation} \label{eq:test}
Y_i = \beta_0 + \beta_1 x_i + \epsilon_i
\end{equation}
Equation $\eqref{eq:test}$ works in PDF, but does not works in HTML.
This works well in pdf document.
However, when rendering html, it gives (???), not (1):
I think this is related to this issue: Support LaTeX environments in Markdown -> HTML conversion, i.e. MathJax occurs the error.
But I cannot see any solution of this.
Is it possible to use \eqref{eq:} normally in html document?
Add the following script at the beginning of your document body:
<script type="text/x-mathjax-config">
MathJax.Hub.Config({
TeX: { equationNumbers: { autoNumber: "AMS" } }
});
</script>
It configures MathJax to automatically number equations. More details here.

Code chunk font size in Rmarkdown with knitr and latex

In knitr, the size option works fine in a .Rnw file, the following code generates:
\documentclass{article}
\begin{document}
<<chunk1, size="huge">>=
summary(mtcars)
#
\end{document}
However, I can't get it to work in Rmarkdown. The following code does not change the font size, as it did in .rnw file. The same thing happens when trying to set options with opts_chunk$set(size="huge").
Is this the expected behavior? How does one change the chunk code font size? (I mean using knitr options, not by adding \huge before the code)
---
title: "Untitled"
output: pdf_document
---
```{r, size="huge"}
summary(mtcars)
```
I am using RStudio Version 0.98.987, knitr 1.6 and rmarkdown 0.2.68.
Picking up the idea to alter a knitr hook we can do the following:
def.chunk.hook <- knitr::knit_hooks$get("chunk")
knitr::knit_hooks$set(chunk = function(x, options) {
x <- def.chunk.hook(x, options)
ifelse(options$size != "normalsize", paste0("\n \\", options$size,"\n\n", x, "\n\n \\normalsize"), x)
})
This snippet modifies the default chunk hook. It simply checks if the chunk option size is not equal to its default (normalsize) and if so, prepends the value of options$size to the output of the code chunk (including the source!) and appends \\normalsize in order to switch back.
So if you would add size="tiny" to a chunk, then all the output generated by this chunk will be printed that way.
All you have to do is to include this snippet at the beginning of your document.
\tiny
```{r}
summary(mtcars)
```
\normalsize
available options for size in descending order are:
Huge > huge > LARGE > Large > large > normalsize > small > footnotesize > scriptsize > tiny
Per this Gist, you have to define the font size using css:
<style type="text/css">
body, td {
font-size: 14px;
}
code.r{
font-size: 20px;
}
pre {
font-size: 20px
}
</style>
code.r will control the font size for R code echoed from the code chunk, while pre will apply to any R results output from the code.
A complete working .Rmd file might look like:
---
title: "FontTest"
author: "Thomas Hopper"
date: "January 13,2016"
output: html_document
---
<style type="text/css">
body, td {
font-size: 14px;
}
code.r{
font-size: 20px;
}
pre {
font-size: 20px
}
</style>
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## R Markdown
This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.
When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
```{r cars}
summary(cars)
```
The resulting html renders as:
You can define you own document format by exporting something based on the following function from your package my_package:
my_report <- function(...) {
fmt <- rmarkdown::pdf_document(...)
fmt$knitr$knit_hooks$size = function(before, options, envir) {
if (before) return(paste0("\n \\", options$size, "\n\n"))
else return("\n\n \\normalsize \n")
}
return(fmt)
}
This will define a knitr chunk hook size that will put the appropriate latex command before the chunk, and \normalsize after the chunk.
Anyway, with the following R markdown you can check if it's working:
---
output: my_package::my_report
---
Test text for comparison
```{r}
print(1)
```
The next code chunk has `size = 'tiny'` in the chunk options.
```{r, size = 'tiny'}
print(1)
```
I get the following result from `markdown::render():
See also the issue I opened on github:
https://github.com/yihui/knitr/issues/1296
Following up on #Martin Schmelzer's output, here's a solution in the case you want to change the code and output default font size for the whole document, but not the size of the text.
def.chunk.hook <- knitr::knit_hooks$get("chunk")
knitr::knit_hooks$set(chunk = function(x, options) {
x <- def.chunk.hook(x, options)
paste0("\n \\", "size_of_the_code_and_output","\n\n", x, "\n\n \\size_of_the_text")
})
For instance,
---
output: pdf_document
---
```{r setup, include=FALSE}
def.chunk.hook <- knitr::knit_hooks$get("chunk")
knitr::knit_hooks$set(chunk = function(x, options) {
x <- def.chunk.hook(x, options)
paste0("\n \\", "footnotesize","\n\n", x, "\n\n \\Huge")
})
```
# Section 1
```{r}
summary(cars)
```
Text.
# Section 2
```{r}
print(1)
```
This works for every chunks.
gives this:

Resources