In RMarkdown how to source css file in a chunk? - css

I need to know how to source a CSS file to be applied to my .Rmd report, using a chunk in RMarkdown? Is it possible?
Actually, I would like to make .css file become a parameter.
Something like this below:
---
title: "Untitled"
output:
html_document
---
```{css, echo=FALSE}
source('style.css')
```

You could try using htmltools::includeCSS():
Contents of my_css.css:
p {
color: red;
}
Contents of Rmd:
---
title: "Untitled"
output: html_document
params:
my_css: my_css.css
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
```
```{r}
htmltools::includeCSS(params$my_css)
```
Hello World!
Produces red text:

You can use a css chunk with the code chunk option (which is intended to allow for programmatically inserted code)
---
title: "Untitled"
output: html_document
params:
my_css: my_css.css
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
```
```{css, code = readLines(params$my_css)}
```
Hello World!

Related

Can I remove the [code] button in a rmarkdown html_document while still displaying the code with the [Show/Hide All Code] button

While I like the possibility to show/hide all code that is allowed by the code_folding property of the html_document output in rmarkdown, I do not like that I have to see a Code button everywhere there is some code because it creates "empty line(s)" in the document.
Is there a way to remove this/these Code button(s) with a property or some javascript magic
without altering the Rmd document (ie. not writing some additional code in the document itself)
while still allowing to show/hide all code through the Show/Hide All Code toggle
Here is an example:
---
title: "Test"
date: '`r Sys.Date()`'
output:
html_document:
code_download: true
code_folding: 'hide'
---
# Title 1
chunk 1
```{r, eval = FALSE}
1+1
```
```{r, eval = FALSE}
1+2
```
```{r}
1+3
```
Which renders like this:
You can hide the code-folding buttons with a css rule.
---
title: "Test"
date: '`r Sys.Date()`'
output:
html_document:
css: "style.css"
code_download: true
code_folding: 'hide'
---
The style.css
.code-folding-btn {
display: none;
}

How to change the font color of title, author, date and make it bold in yaml of Rmarkdown?

---
title: "PDF Document"
author: "Sana"
date: "27-05-2022"
---
How to change the font color and style of title, author, date and make it bold or italic in yaml of Rmarkdown?
One way is to use a latex code chunk in the title.
---
title: |
```{=latex}
\textcolor{red}{\textbf{PDF Document}}
```
author: "Sana"
output: pdf_document
---

xaringan set the document title dynamically

In r-markdown is is an option to move the title: out of the main yaml, as described in the R Markdown Cookbook.
But, in a xaringan slide set the new --- seems to conflict with the idea of new slide.
The below code works, but when move line #2, title: "Presentation Ninja" outside the main yaml, and inset it as title: "The name of the dog is r dog_name!", by removing all my <!-- ... --> code in line #17-19, it does not work as expected. I do no longer get a title page. I guess I need to work around the --- in xaringan?
Any help would be appreciated!
---
title: "Presentation Ninja"
subtitle: "⚔<br/>with xaringan"
author: "Yihui Xie"
output:
xaringan::moon_reader:
lib_dir: libs
---
# xaringan set the document title dynamically
```{r, code=xfun::read_utf8('script_with_many_dog_names.R')}
```
The name of the dog is `r dog_name`!
<!-- --- -->
<!-- title: "The name of the dog is `r dog_name`!" -->
<!-- --- -->
Some bullets
- Foo
- Bar
You could use R Markdown parameters :
Create a template file Template.Rmd
---
title: "The name of the dog is `r params$dog_name`"
subtitle: "⚔<br/>with xaringan"
author: "John Doe"
output:
xaringan::moon_reader:
lib_dir: libs
params:
dog_name: NA
---
# xaringan set the document title dynamically
Some bullets
- Foo
- Bar
Render the template after setting dog_name parameter :
source('script_with_many_dog_names.R')
# As an example, but it could be the result of previous script
params <- list(dog_name = 'Charles')
rmarkdown::render('Template.Rmd', output_file = 'presentation.html',
params = params,
envir = new.env(parent = globalenv())
)
You can add your logic directly to the title, that seems to work fine for me:
---
title: "The number is `r round(100 * runif(1), 2)`"
subtitle: "⚔<br/>with xaringan"
author: "Yihui Xie"
output:
xaringan::moon_reader:
lib_dir: libs
---

Do not indent the first paragraph of a new section

I would like to indent every paragraph but the first paragraph of every section in my article. The Rmd file has a pdf_output and it is referencing child files containing the sections of my article.
This is what I have:
---
bibliography: bibliography.bib
header-includes:
- \usepackage{setspace}\doublespacing
- \usepackage{float}
- \setlength{\parskip}{12pt}
#- \setlength{\parindent}{30pt}
fontsize: 12pt
indent: true
output: pdf_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE, warning = FALSE, message = FALSE)
```
```{r 01_Introduction, child = here::here("WriteUp", "Child", "Introduction.Rmd")}
```
\newpage
# References
I would like to indent every paragraph but the first paragraph of every section in my article.
You use indent in the YAML. See this answer.
Here is an example. First the main file:
---
title: "test"
author: "me"
date: "11/3/2020"
output: pdf_document
indent: true
---
```{r, child=c('sec1.Rmd', 'sec2.Rmd')}
```
Here is the sec1.Rmd file:
## My section
Some text.
Some more text.
and here is the sec2.Rmd file:
# That other section
Even more text.
And the last line.
The result is:
Update
It is still indented after I have added:
header-includes:
- \usepackage{setspace}\doublespacing
- \usepackage{float}
- \setlength{\parskip}{12pt}
to the YAML.

R notebook font size

What are my options for changing the font size of dynamically-generated output, when rendering to a PDF? My document currently looks something like this:
---
title: "Foo"
author: "Bar"
date: "`r format(Sys.time(), '%B %e, %Y')`"
output:
pdf_document: default
html_document:
df_print: paged
header-includes: \usepackage{amsmath} \usepackage{color}
---
# Introduction
```{bash echo=FALSE}
perl -le 'print for 1..50'
```
I want to decrease the font size to help more output fit on a single page of the PDF. I'm not currently also outputting to HTML, so a cross-format solution would be cool but not really necessary.
It is as simple as fontsize: 12pt (or of course fontsize: 9pt as we use in pinp)
Modified file:
---
title: "Foo"
author: "Bar"
date: "`r format(Sys.time(), '%B %e, %Y')`"
fontsize: 12pt
output:
pdf_document: default
html_document:
df_print: paged
header-includes: \usepackage{amsmath} \usepackage{color}
---
# Introduction
```{bash echo=FALSE}
perl -le 'print for 1..50'
```
If you want it just for code / result snippets I would suggested you place appropriate \begin{Large} and \end{Large} around it. I have in the past customized the default environments used here --- as I recall knitr more or less follows Sweave here. Just look at the generated .tex file.

Resources