Rmarkdown beamer ignoring size option - r

I'm trying to create a presentation with Rmarkdown and beamer and I am unable to adjust the size of my code and output. In the past I edited the beamer template to work around this but I was hoping there is a better way.
For example:
---
title: "Reprex"
output:
beamer_presentation
---
## Output one
```{r, size="footnotesize"}
1:50
```
renders identical to:
---
title: "Reprex"
output:
beamer_presentation
---
## Output one
```{r}
1:50
```
Any thoughts on how to fix this?

The size option specifies the "font size for the default LaTeX output" (source). But you are writing rmarkdown which is converted to PDF afterwards.
A workaround:
---
output: beamer_presentation
---
## Output one
Foo.
```{r, echo = FALSE}
knitr::asis_output("\\footnotesize")
1:10 # this will be small
knitr::asis_output("\\normalsize")
11:20 # this not
```
Bar.
Depending on how often this is used it could be work using a helper function or even a chunk hook to print the font size commands.

Related

rmarkdown read code from file and display with highlight

I have two RMarkdown files. main.Rmd which is the main file which is rendered as well as example.Rmd which holds a longer example and is used elsewhere (hence it lives in its own document).
I want to include example.Rmd in the main.Rmd file with code highlighting of its RMarkdown code but the code of example.Rmd does not need to be executed, as if I set eval=FALSE and copied all code into the chunk by hand.
An example MWE is
main.Rmd
---
title: This is main.rmd
output: html_document
---
```{r}
# attempt that doesnt work
cat(readLines("example.Rmd"), sep = "\n")
```
and in example.Rmd
---
title: This is example.rmd
output: html_document
---
```{r}
# code that is not executed but shown in main.Rmd
data <- ...
```
Set eval=FALSE in the example.Rmd file and then include it in main.Rmd using child chunk option.
example.Rmd
---
title: This is example.Rmd
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(eval = FALSE)
```
```{r}
# This is from example.Rmd
x <- rnorm(10)
y <- rnorm(10)
lm(y ~ x)
```
```{r}
# some comments
print("This is from example.Rmd")
```
main.Rmd
---
title: This is main.Rmd
output:
html_document:
highlight: haddock
---
```{r example, child="example.Rmd"}
```
Edit
To show full source-code of the Rmarkdown file, one possible option could be reading that Rmd file and then cat it with chunk option comment="".
Now about the syntax highlighting; there's a chunk option class.output with which it is possible to specify a language name for which pandoc supports syntax highlighting.
You can get the list of language names for which pandoc has syntax highlighting support by running the following,
pandoc --list-highlight-languages
(Note, if you don't have pandoc installed separately, you can also use the pandoc installed with Rstudio itself. Run rmarkdown::pandoc_exec() to get the pandoc executable path)
Now, the file we are trying to include actually contains not just R code, but also markdown and yaml syntaxes. So it's a kind of mixed thing and pandoc has no syntax highlighting support out of the box for this. Still I have chosen c as syntax highlighting language just to show the possibility. (Also tried r, but syntax-highlighting is not so distinctive)
---
title: This is main.Rmd
output:
html_document:
highlight: tango
---
## Rmarkdown
```{r example, echo=FALSE, class.output="c", comment=""}
cat(readLines("example.Rmd"), sep = "\n")
```
But still if you want a specific syntax-highlighting for Rmarkdown, you can actually create one. See here from the pandoc documentation itself and also this answer on SO about this.

Change font and color for figure captions in Rmarkdown in PDF output

I'm wondering how to change font and color specifically for figure captions in Rmarkdown for a PDF output. I know how to do this for word output (creating a separate document), but often figures are submitted to journals as PDFs.
Note that this is not whether you can change the color of text in a PDF which is answered here: Changing the font size of figure captions in RMarkdown pdf output. If I try this within the fig.cap = "fig text here.\\label{...}" I get the following error:
! Undefined control sequence.
<recently read> \cellcolor
This probably comes closest: Changing the font size of figure captions in RMarkdown pdf output, but I went to CTAN and downloaded the package and am not sure whether I have to find my R libraries and drag it to there, or what. Perhaps this is the only/easiest solution, if so, can you advise on how I should proceed with the downloaded package. Haven't done a lot in latex, so it would probably be finding how colors are defined in latex as well?
Thanks much!
James
Here is my header:
output:
pdf_document:
keep_tex: true
html_document: default
header-includes:
\usepackage{placeins}
---
You could try this:
---
title: "Untitled"
author: "bttomio"
date: "6/13/2021"
output: pdf_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r pressure, echo=FALSE, fig.cap="\\textcolor{red}{This is red} and \\textcolor{blue}{this is blue}"}
plot(pressure)
```
-output

Incremental code evaluation in R markdown presentation

I'm looking for a way to evaluate code in a incremental way in R markdown presentation. I don't need to use any specific format - it can be anything that works and is flexible (rpres, ion, revealjs, etc.)
I'll use these presentation in class for my students and would like to make them type code in their R Console first and then compare our outputs.
So far I came up with one solution using revealjs with slide_level header
---
title: "Test presentation"
output: revealjs::revealjs_presentation
slide_level: 2
---
# Level 1 horizontal main slide
Some text
## Level 2 vertical slide with R Code
```{r eval=FALSE}
summary(cars)
```
## Level 2 vertical slide with R output
```{r echo=FALSE}
summary(cars)
```
Anything easier and less time consuming would be great.
I know the question is a bit old but you can use the incremental option for example with ioslides. That way you don't have to change slide but you can print step by step first the text then the code and then the result.
example code:
---
title: "Test presentation"
output:
ioslides_presentation:
incremental: yes
---
## Slide 1
- Some text
- Level 2 just the code
```{r eval=FALSE}
summary(cars)
```
- Level 2 just the output
```{r echo=FALSE}
summary(cars)
```

Code in Columns in RMarkdown presentation

Often when I include r code in a rmarkdown pdf presentation, I want to use the space of the whole slide and therefore want to present the code and the output side-by-side.
In pure LaTex I would go for \begin{columns}...\end{columns} and then include the code/output manually using lstlistings or some other code-highlighting library. But that proves tedious if I exceed a couple of code examples.
Now I want to use RMarkdown for the presentations and would like to achieve a similar result.
However, the following code throws an error:
## This is slide 1
\begin{columns}[t]
\begin{column}{0.5\textwidth}
```{r, eval=F}
plot(1:10)
```
\end{column}
\begin{column}{0.5\textwidth}
```{r, echo=F}
plot(1:10)
```
\end{column}
\end{columns}
Leaving out the knitr code-chunks and replacing them with text works.
I am aware that it has something to do with the pandoc engine (see here), but wanted to ask if anybody has found a way around this issue.
Well, I may should have looked with a wider focus.
Here is a solution that works for Python, but can easily be adapted to Rmarkdown: https://stackoverflow.com/a/26069751/3048453
I ended up with this code:
in header.tex
\newcommand{\columnsbegin}{\begin{columns}}
\newcommand{\columnsend}{\end{columns}}
in presentation.Rmd
---
title: "Two-column codes in RMarkdown"
author: "My Name"
date: "February 4, 2017"
output:
beamer_presentation:
includes:
in_header: header.tex
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## Testslide with Columns
\columnsbegin
\column{.5\textwidth}
```{r, eval=F}
plot(mtcars[, 1:3])
```
\column{.5\textwidth}
```{r, echo=F}
plot(mtcars[, 1:3])
```
\columnsend
Which results in this

knitr plots, labels, and captions within one chunk - .Rmd files

Jallen produced a solution for producing knitr plots, labels, and captions within one chunk - for Rnw files.
knitr plots, labels, and captions within one chunk
This works nicely for .Rnw but I can't make it work for .Rmd, don't see what is going wrong...
---
output:
pdf_document:
fig_caption: yes
fig_crop: no
---
```{r startup,echo=FALSE,results='hide',message=FALSE,tidy=FALSE,warning=FALSE,fig.keep='all',comment=NA}
require(knitr)
require(ggplot2)
opts_knit$set(progress = F, verbose = F)
opts_chunk$set(comment=NA,
tidy=FALSE,
warning=FALSE,
message=FALSE,
echo=FALSE,
dpi=600,
fig.width=6.75, fig.height=4, # Default figure widths
dev=c("pdf",'tiff'),
dev.args=list(pdf=list(NULL),tiff=list(compression='lzw')),
error=FALSE)
```
```{r plotloop,results='asis'}
for(x in seq(1,20)){
x1<-data.frame(x=seq(1,10),y=seq(1,10))
plt<-ggplot(data=x1,aes(x,y))+geom_point()
figLabel=paste('Figure',x,sep='')
capt<-paste('Caption for fig.',x)
cat(knit(text=(paste("```{r ",figLabel,",fig.pos='h',fig.cap='",capt,"'}\nplt\n```",sep=''))))
cat('\\newpage')
plot.knit function in knitr plots, labels, and captions within one chunk can be modified to account for the syntax of markdown as opposed to latex. plot.knit.md becomes
plot.knit.md<-function(chunkLabel,#text for chunk label which is also used for figure file name
capt,#text for caption
plt,
...)
{
cat(knit(text=knit_expand(text="```{r, {{chunkLabel}},eval=TRUE,fig.cap='{{capt}}',echo=FALSE}\nplt\n```"),
quiet=TRUE))
}
This works in the following markdown doc.
---
title: "plot.knit.md demo"
author: "Joel Allen"
date: "04/23/2015"
output:
html_document: default
pdf_document:
fig_caption: yes
---
This is an R Markdown document to demonstrate the generation of self-contained code chunks in a markdown file. It is particularly useful for situations where multiple plots are generated in a single code chunk allowing for dynamic label and caption support.
This example draws on
http://stackoverflow.com/questions/21685885/knitr-plots-labels-and-captions-within-one-chunk
in response to
https://stackoverflow.com/questions/27443019/knitr-plots-labels-and-captions-within-one-chunk-rmd-files
Items to note:
#. output pdf_document fig_caption option must be set to yes
#. plot.knit has been modified to plot.knit.md to account for the different syntax needed.
```{r, echo=FALSE,results='asis'}
plot.knit.md<-function(chunkLabel,#text for chunk label which is also used for figure file name
capt,#text for caption
plt,
...)
{
cat(knit(text=knit_expand(text="```{r, {{chunkLabel}},eval=TRUE,fig.cap='{{capt}}',echo=FALSE}\nplt\n```"),
quiet=TRUE))
}
require(ggplot2)
cars.p<-ggplot(cars,aes(x=speed,y=dist))+
geom_point()
plot.knit.md(chunkLabel="carsPlot",capt="this is a caption for the cars plot",plt=cars.p)
```
One thing to figure out though is the attachment of labels...

Resources