bookdown figure number formattiing: both sequential numbering and section numbering - r

When using bookdown (single document), if I set both section_numbering = 'yes' and fig_caption = 'yes', the figures are numbered X.2 (where X is the section number). If section_number = 'no', the figures are numbered sequentially (Fig 1, 2 ...), but sections numbers are lost.
Is there a way to get figures numbered sequentially without losing the section numbers? In the example below, I would like to have both the sections figures numbered as 1 and 2.
Thank you.
---
output:
bookdown::html_document2:
fig_caption: yes
number_sections: yes
---
# header 1
Reference example: \#ref(fig:plotcars):
```{r plotcars, fig.cap = "A car plot"}
plot(cars)
```
# header 2
Reference example: \#ref(fig:plotcars2):
```{r plotcars2, fig.cap = "A car plot"}
plot(cars)
```

I just added a new argument global_numbering to the dev version of bookdown. You can test the dev version via
remotes::install_github('rstudio/bookdown')
Example:
---
output:
bookdown::html_document2:
fig_caption: true
number_sections: true
global_numbering: true
---
# header 1
Reference example: \#ref(fig:plotcars):
```{r plotcars, fig.cap = "A car plot"}
plot(cars)
```
# header 2
Reference example: \#ref(fig:plotcars2):
```{r plotcars2, fig.cap = "A car plot"}
plot(cars)
```

Related

R markdown-figures are shown at the bottom of a section instead of in between text where I placed text

I have the following code chunks in R markdown that produce 2 plots with some text in between but the pdf output is just sending all plots to the bottom of the document. How can I make it so the Plot stays in the spot of the text where I want it to be?
---
title: "Evaluation of Income annuities with money-back guarantees"
author: "Long Life Insurance Company's Actuarial Team"
output:
word_document:
toc: yes
pdf_document:
fig_width: 6
fig_height: 3.5
fig_caption: yes
fig_crop: no
toc: yes
number_sections: yes
---
[Text]
{r, echo=FALSE, include=TRUE, messages=FALSE, warnings=FALSE, fig.cap = "EPVs at different ages and interest rates"}
plot(xAges,EPVs[1,],ylim=c(min(EPVs),max(EPVs)),main="EPV of CRIA",xlab=
"Annuitant's age",ylab="EPV",pch=1,cex=0.5,col=2)
legend(60,30, legend=c("r = 2%", "r = 4%","r = 6%", "r = 8%"),col=2:5, pch=c(1,3,8,16), cex=1)
[Text]
{r, echo=FALSE, messages=FALSE, warnings=FALSE,fig.cap = "EPVs at different given interest rates"}
plot(IntRates65,EPVs[1,],ylim=c(min(EPVs),max(EPVs)),main="EPV of all 3 products",xlab=
"Interest rate",ylab="EPV",pch=1,cex=0.8,col=2)
legend(0.08, 16, legend=c("LOIA", "IRIA","CRIA"),col=2:4, pch=c(1,3,8), cex=0.8)
[Text]
Tried code above but it sends all plots to the bottom
I think that simply putting each figure in its own chunk with ``` in the beginning and end, will do the trick, like so
---
title: "Evaluation of Income annuities with money-back guarantees"
author: "Long Life Insurance Company's Actuarial Team"
output:
word_document:
toc: yes
pdf_document:
fig_width: 6
fig_height: 3.5
fig_caption: yes
fig_crop: no
toc: yes
number_sections: yes
---
[Text]
```{r, echo=FALSE, include=TRUE, messages=FALSE, warnings=FALSE, fig.cap = "EPVs at different ages and interest rates"}
barplot(c(2,5))
```
[Text]
```{r, echo=FALSE, messages=FALSE, warnings=FALSE,fig.cap = "EPVs at different given interest rates"}
barplot(c(2,5), main="Main title",
xlab="X axis title",
ylab="Y axis title",
sub="Sub-title")
```
[Text]

Add Table of contents in different position in Rmd pdf

I want to add the table of content in different place instead of the first page in .Rmd pdf. I found the function render_toc() from #Garrick Aden-Buie this is the link https://gist.github.com/gadenbuie/c83e078bf8c81b035e32c3fc0cf04ee8. It is working perfect if I don't use cat() in chunk codes.
I have to use cat() in my .Rmd, is there any way I can add the table of contents any place in .Rmd
You can see the table of contents only test 1, test 2, and test 3, setosa, versicolor, virginica did not include in table of content
---
title: "test"
output:
pdf_document:
number_sections: yes
toc: yes
toc_depth: 4
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
source("render_toc.R")
library(knitr)
```
\newpage
# Table of Contents {#crazy-slug-here}
```{r echo = FALSE}
render_toc("test.Rmd")
```
# Test 1
# Test 2
# Test 3
```{r echo = FALSE, results ='asis'}
library(ggplot2)
for(Species in levels(iris$Species)){
cat('\n#', Species, '\n')
p <- ggplot(iris[iris$Species == Species,], aes(x = Sepal.Length, y = Sepal.Width)) +
geom_point()
print(p)
cat('\n')
}
```
Using the code you provided I'm able to knit the pdf document and get the three plots, one for each Species in iris.
This code, however, produces two TOCs: one of them in the first page which is not desired according to your question. In order to get only the TOC produced by the function render_toc() set toc: no in the YAML header.
---
title: "test"
output:
pdf_document:
number_sections: yes
toc: no
toc_depth: 4
---
This way only one TOC will be generated and placed where render_toc() is.
Edit: After seeing your edit with the desired output, I think the following LaTeX code can help:
\newpage
\thispagestyle{plain}
\mbox{}
\setcounter{tocdepth}{2}
\renewcommand{\contentsname}{Table of Contents}
\tableofcontents
\newpage
\thispagestyle{plain}
\mbox{}
Note: here we are not using render_toc(). The code creates the TOC in the second page of the document. If you need another blank page, just introduce another block of:
\newpage
\thispagestyle{plain}
\mbox{}
These pages may or may not be numbered, which you control via the argument passed to \thispagestyle{}. See here for more details.
Full code:
---
title: "Document title"
output:
pdf_document:
number_sections: yes
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
\newpage
\thispagestyle{plain}
\mbox{}
\setcounter{tocdepth}{2}
\renewcommand{\contentsname}{Table of Contents}
\tableofcontents
\newpage
\thispagestyle{plain}
\mbox{}
# Test 1
## Subsection 1
# Test 2
## Subsection 2
# Test 3
```{r, results='asis', echo=FALSE}
library(ggplot2)
for(Species in levels(iris$Species)){
cat('\n#', Species, '\n')
p <- ggplot(iris[iris$Species == Species,],
aes(x = Sepal.Length, y = Sepal.Width)) +
geom_point()
print(p)
cat("\n")
}
```
This way we end up with a TOC looking like this:

Multiple chunks in a tab

I want chunks of Example1 and Example2 in a single tab and Example3 in another tab. The code below is showing everything in one tab. In my desired output, some chunks in sectionA tab and some in sectionB tab.
---
title: "R Notebook"
output:
html_document:
df_print: paged
code_folding: show
theme: united
highlight: tango
toc: true
toc_float: true
toc_depth: 3 # upto three depths of headings (specified by #, ## and ###)
---
```{r, echo=FALSE}
library(rmarkdown) #used for syntax highlighting in this document
```
## Sections {.tabset}
### Section A
# Example1
```{r, eval=FALSE}
plot(cars)
```
<details>
<summary>Click for Output</summary>
```{r, echo=FALSE, eval=TRUE}
plot(cars)
```
</details>
### Section B
# Example2
```{r}
quantile(mtcars$mpg, probs = c(0.99))
```
## {-}
# Example3
```{r table}
knitr::kable(mtcars[1:5,, 1:5], caption = "A table caption")
``````
If I understand correctly, you want something along these lines? Once you start a tabbed section, you can't willy-nilly throw around subsections but should follow specific hierarchy. If you have a tabbed section of level ##, everything under level ### will become a tab. Anything deeper than that will become a title in the said tab.
## Sections {.tabset}
### Section A
#### Example1
```{r, eval=FALSE}
plot(cars)
```
<details>
<summary>Click for Output</summary>
```{r, echo=FALSE, eval=TRUE}
plot(cars)
```
</details>
#### Example2
```{r}
quantile(mtcars$mpg, probs = c(0.99))
```
### Section B
#### Example3
```{r table}
knitr::kable(mtcars[1:5,, 1:5], caption = "A table caption")
``````

hiding entire pages in an r markdown document

I have the r markdown document below. I would like to hide page 2 if parameter "P" is not equal to A.
So the result would be if parameter P != A then only 3 pages are produced.
Is this possible somehow?
---
title: "Untitled"
output:
pdf_document:
toc: yes
params:
P: A
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
\newpage
## PAGE2
this is text for page 2
this is text for page 2
```{r cars}
summary(cars)
```
\newpage
## PAGE3
this is text for page 3
```{r pressure, echo=FALSE}
plot(pressure)
```
\newpage
## PAGE4
this is text for page 4
You could comment the section out based on P value. Here is a bit of a hackish way to do it: surround the section with \iffalse/\fi. Note that the R code inside the section still needs to be valid for this to work (it will be compiled by knitr, but ignored by latex).
```{r, echo=FALSE, results='asis'}
if(params$P != "A")
cat("\\iffalse")
```
## PAGE2
this is text for page 2
this is text for page 2
```{r cars}
summary(cars)
```
\newpage
```{r, echo=FALSE, results='asis'}
if(params$P != "A")
cat("\\fi")
```

How to number table with chapter number

I inclue and cross-referencing a table in a Rmd file, and I want to add chapter number with the table title and referencing number, like "TABLE 1.1 test", but not "TABLE 1 test". I render the file using bookdown::word_document2. Here is a minimal example:
---
title: test
output:
bookdown::word_document2:
fig_caption: yes
toc: yes
---
```{r}
library(knitr)
```
# Heading 1
I'm cross-referencing a table table:\#ref(tab:test).
```{r test}
test <- data.frame(a = 1:6, b = 2:7)
kable(test, caption = "test")
```
Rmarkdown/Bookdown is really an amazing tool #Yihui, thanks!

Resources