knitr defines the argument fig.cap as
fig.cap: (NULL; character) figure caption to be used in a figure
environment in LaTeX (in \caption{}); if NULL or NA, it will be
ignored, otherwise a figure environment will be used for the plots in
the chunk (output in \begin{figure} and \end{figure})
However, for HTML output the following works:
---
title: "Caption Test"
author: "Some Author"
date: "February 18, 2016"
output: html_document
---
```{r}
library(ggplot2)
```
```{r, fig.cap = c("This is caption 1", "This is caption 2")}
## Plot 1
qplot(carat, price, data = diamonds)
## Plot 2
qplot(carat, depth, data = diamonds)
```
meaning, each figures gets its correct caption defined in the code chunk argument fig.cap = c("Caption 1", "Caption 2")
However, it is challenging to keep track of captions -especially if long- when they are placed inside the chunk options. Besides creating two separate chunks for each figure with captions inserted outside of the chunk, are there other options?
You can set eval.after="fig.cap" so that figure captions are evaluated after the chunk is run. That way, you can define your captions inside the chunk.
---
title: "Caption Test"
author: "Some Author"
date: "February 18, 2016"
output: html_document
---
```{r}
library(ggplot2)
library(knitr)
opts_knit$set(eval.after = 'fig.cap')
```
```{r, fig.cap = cap}
## Plot 1
qplot(carat, price, data = diamonds)
cap <- "This is caption 1"
## Plot 2
qplot(carat, depth, data = diamonds)
cap <- c(cap, "This is caption 2")
```
Related
I want to change the numbering of the tables in the RMarkdown document so that all tables in the appendix have an "A-" in front of the number, thus: "Table A-2".
Only in the appendix. Otherwise with normal numbering ("Table 1").
However, I am not really getting anywhere.
Here is my reproducible example:\
---
title: "This is my title"
date: "`r Sys.setlocale(locale = 'English') ; format(Sys.time(), '%B %d, %Y')`"
output: pdf_document
---
```{r echo = F, message = F, warning = F}
library(tidyverse)
library(knitr)
``` #The hash mark must be removed!
# Results
```{r echo = F, message = F, warning = F}
tribble(~column1, ~column2,
"value1", 2,
"value2", 5
)%>%
kable(booktabs=T, caption = "This is the caption of the first table")
```
# Appendix
```{r echo = F, message = F, warning = F}
tribble(~column1, ~column2,
"value1", 6,
"value2", 8
)%>%
kable(booktabs=T, caption = "This is the caption of the second table")
```
This is really a LaTeX question, and I found the answer here.
You add these LaTeX lines after your Appendix title:
\setcounter{table}{0}
\renewcommand{\thetable}{A\arabic{table}}
I would like do add an icon to the text in one of the slides.
I've tried a few things but the end result is unstable. The icon are being printed in the html file, but If I add a slide or some text the the icons dissapear. Even if I revert what I've done with Ctrl+Z to previous working code the icons don't get printed...
I might be missing something, but the bookdown documentation didn't have anything on the subject...
---
title: 'Title'
author: "Author"
date: '`r format(Sys.time(), "%d %B, %Y")`'
output:
ioslides_presentation:
self_contained: true
incremental: false
---
```{r knitr_init, echo=FALSE, message=FALSE, warning=FALSE, cache=FALSE}
## Global options
library(knitr)
library(shiny)
opts_chunk$set(
cache = FALSE,
prompt = FALSE,
tidy = FALSE,
comment = NA,
message = FALSE,
warning = FALSE
)
library(tidyverse)
library(plotly)
```
## S1
`r shiny::tags$i(class = "fa fa-arrow-down",style = "color: rgb(0,166,90)")`
item 3
`r shiny::tags$i(class = "fa fa-arrow-down",style = "color: rgb(0,166,90)")`
item 2
## S2
```{r, echo=F}
data.frame(a=1:10, b=1:10) %>%
plot_ly(x=~a,y=~b)
```
Writing the icons as <i class="fa fa-arrow-down" style="color: rgb(0,166,90)"></i> didn't seem to work either.
One option is to use the package icon (more information here).
---
title: 'Title'
author: "Author"
date: '`r format(Sys.time(), "%d %B, %Y")`'
output:
ioslides_presentation:
self_contained: true
incremental: false
---
```{r knitr_init, echo=FALSE, message=FALSE, warning=FALSE, cache=FALSE}
## Global options
library(knitr)
library(shiny)
opts_chunk$set(
cache = FALSE,
prompt = FALSE,
tidy = FALSE,
comment = NA,
message = FALSE,
warning = FALSE,
echo=FALSE
)
library(tidyverse)
library(plotly)
library(icon)
```
## S1
```{r icon-style1}
icon_style(fontawesome("arrow-down", style = "solid"), scale = 2, fill = "#00A65A")
```
item 3
```{r icon-style2}
icon_style(fontawesome("arrow-down", style = "solid"), scale = 2, fill = "#00A65A")
```
item 2
## S2
```{r, echo=F}
data.frame(a=1:10, b=1:10) %>%
plot_ly(x=~a,y=~b)
```
-output
I just added two code chunks, one for each icon. Additionally, in your first r chunk, I added echo=FALSE to the opts_chunk$set and library(icon). In order to achieve the RGB color you chose, the HEX code was needed (#00A65A).
In a larger report compiled with bookdown I used several kableExtra tables which included LaTex commands (e.g., to add italics, bullets to create a listing, and manually-added footnotes within the table).
When I copy the tables in a LaTex beamer presentation generated with rmarkdown::beamer_presentation, compilation unfortunately fails.
How to twist the kableExtra tables to include the LaTex commands?
MWE
---
title: "MWE"
subtitle: "Beamer presentation with R-markdown"
author: "Donald Duck"
institute: "some place"
date: "`r format(Sys.time(), '%B %d, %Y')`"
output:
bookdown::pdf_book:
base_format: rmarkdown::beamer_presentation
theme: "THEMENAME"
latex_engine: xelatex
toc: false
slide_level: 2
---
(ref:footnote-a) Text for footnote a
(ref:footnote-b) Text for footnote b
\renewcommand{\arraystretch}{1.3} <!-- increase line spacing for the table -->
```{r table-wLatex, echo=FALSE, fig.align="center", message=FALSE, warning=FALSE, out.width='66%'}
library(kableExtra)
library(dplyr)
# table with manually added footnotes within table
df <- data.frame(
col1 = c("Category 1", "Category 2"),
col2 = c(
"foo and \\emph{special foo}$^{a}$",
"bar and \n $\\boldsymbol{\\cdot}$ \\emph{random bar}$^{a}$\n $\\boldsymbol{\\cdot}$ \\emph{special bar}$^{b}$")
)
# header: add column names
names(df) <- c("Categories", "Description")
df %>%
mutate_all(linebreak) %>% # required for linebreaks to work
kable(
"latex",
# escape = FALSE, # needed to be able to include latex commands
booktabs=TRUE,
align = "l",
caption = "Caption Table with LaTex" # short caption for LoT
) %>%
kableExtra::footnote(
alphabet = c(
"(ref:footnote-a)",
"(ref:footnote-b)"
),
threeparttable = TRUE, # important! Else footnote runs beyond the table
footnote_as_chunk = TRUE, title_format = c("italic", "underline")
) %>%
column_spec(1, width = "11em") %>% # fix width column 1
column_spec(2, width = "27em") # fix width column 2
```
\renewcommand{\arraystretch}{1} <!-- reset row height/line spacing -->
Several LaTex packages had to be loaded for kableExtra table such that LaTex code can well be included inside.
To fix the column width, column_spec(1, width = "3cm") is better specified in cm instead of em (for the previous specification, table ran beyond the slide).
---
title: "LaTex code inside a kable table in an rmarkdown::beamer_presentation"
output:
bookdown::pdf_book:
base_format: rmarkdown::beamer_presentation
latex_engine: xelatex
toc: false
slide_level: 2
header-includes:
- \usepackage{booktabs}
- \usepackage{longtable}
- \usepackage{array}
- \usepackage{multirow}
- \usepackage{wrapfig}
- \usepackage{float}
- \usepackage{colortbl}
- \usepackage{pdflscape}
- \usepackage{tabu}
- \usepackage{threeparttable}
- \usepackage{threeparttablex}
- \usepackage[normalem]{ulem}
- \usepackage{makecell}
# - \usepackage[usenames,dvipsnames]{xcolor} # clashes, as loaded by markdown anyway
---
(ref:footnote-a) Text for footnote a
(ref:footnote-b) Text for footnote b
\renewcommand{\arraystretch}{1.3} <!-- increase line spacing for the table -->
```{r table-wLatex, echo=FALSE, fig.align="center", message=FALSE, warning=FALSE, out.width='30%'}
library(kableExtra)
library(dplyr)
# table with manually added footnotes within table
df <- data.frame(
col1 = c("Category 1", "Category 2"),
col2 = c(
"foo and \\emph{special foo}$^{a}$",
"bar and \n $\\boldsymbol{\\cdot}$ \\emph{random bar}$^{a}$\n $\\boldsymbol{\\cdot}$ \\emph{special bar}$^{b}$")
)
# header: add column names
names(df) <- c("Categories", "Description")
df %>%
mutate_all(linebreak) %>% # required for linebreaks to work
kable(
"latex",
escape = FALSE, # needed to be able to include latex commands
booktabs=TRUE,
align = "l",
caption = "Caption Table with LaTex" # short caption for LoT
) %>%
kableExtra::footnote(
alphabet = c(
"(ref:footnote-a)",
"(ref:footnote-b)"
),
threeparttable = TRUE, # important! Else footnote runs beyond the table
footnote_as_chunk = TRUE, title_format = c("italic", "underline")
) %>%
kable_styling( # for wide tables: scale them down to fit
full_width = F,
latex_options = c(
"scale_down"
)) %>%
column_spec(1, width = "3cm") %>% # fix width column 1
column_spec(2, width = "5cm") # fix width column 2
```
\renewcommand{\arraystretch}{1} <!-- reset row height/line spacing -->
I'm trying to render a pander table in RMarkdown that includes superscript. Here is my current RMarkdown code:
```
---
title: "My Table"
output: html_document
---
```{r packages, include = FALSE}
library(tidyverse)
library(pander)
```
```{r my_table}
my_table <-
data.frame(
c("Sector 1",
"Sector 2",
"Sector 3"),
c("100%",
"29%",
"NA"))
pander(my_table,
col.names = c("Sector", "% Coverage"),
split.cells = 40,
keep.line.breaks = TRUE)
```
And here is the knit result:
I would like to add a superscripted letter "c" to the "NA" cell of the table, yielding an output like this:
You are looking to use the notation $$ NA^c $$, but this only works outside of the R chunk at the moment.
The section of the bookdown manual on generating figures demonstrates a case where include_graphics() can be given a vector of paths of length > 1, producing a number of subplots with a single caption:
However, when I try this in my fork of thesisdown, in the PDF output I get the figure caption (and, judging by the spacing, the entire figure environment) repeated for each subplot. Here is a reproducible example:
---
output: bookdown::pdf_document2
toc: false
---
```{r, echo = FALSE}
for(i in 1:3){
jpeg(filename = paste0("temp_", i, ".jpg"), width = 600, height = 250)
plot(cars)
title(main = i)
dev.off()
}
```
```{r fig.cap = "Caption", out.width="100%", fig.ncol = 1, echo = FALSE}
knitr::include_graphics(paste0("temp_", 1:3, ".jpg"))
```
I was hoping more for the five images stacked, with a single caption at the bottom. This also appears to be breaking the figure cross-referencing, as each plot has its own figure number and cross-references to the chunk render as ??.
Getting subfigures requires a few additional settings to be set in the chunk header.
fig.subcap is a list of the captions for subfigures
fig.ncol: the number of columns of subfigures
out.width: the output width of the figures. You will normally set this 100% divided by the number of sub columns.
Subfigures are built using the subfig package. You can either include this within your LaTeX bookdown template, or alternative you can added it to the YAML as follows:
Here is an example:
---
output: bookdown::pdf_document2
toc: false
header-includes:
- \usepackage{subfig}
---
```{r, echo = FALSE}
for(i in 1:3){
jpeg(filename = paste0("temp_", i, ".jpg"), width = 600, height = 250)
plot(cars)
title(main = i)
dev.off()
}
```
```{r fig.cap = "Caption", out.width="100%", fig.ncol = 1, echo = FALSE, fig.subcap= c("First", "Second", "Third")}
knitr::include_graphics(paste0("temp_", 1:3, ".jpg"))
```