knitr: Using subscript with fig.cap in Markdown - r

How do I go about including subscript in a figure caption when using {r fig.cap=""}?
Mock .Rmd file:
---
output:
pdf_document:
fig_caption: yes
---
```{r setup, include = FALSE}
library(knitr)
```{r fig.cap = "CO2 Concentration", echo = FALSE}
plot(rnorm(100))
I would like to get the 2 in CO2 in subscript. Is fig.cap the best way, or is something better?

You could use standard Markdown syntax
---
output:
pdf_document:
fig_caption: yes
---
This is ^superscript^
This is ~subscript~
```{r fig.cap = 'CO~2~ Concentration', echo = FALSE}
plot(rnorm(100))
```

Related

R chunk inside LaTeX in an rmarkdown document

I am trying to get an R chunk run inside LaTeX code in the following rmarkdown document:
---
title: "Untitled"
output: pdf_document
date: '2022-05-20'
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, results = "hide")
```
\begin{enumerate}
\item
{
```{r}
x <- 2+3
x
```
}
\end{enumerate}
The output is:
But I want:
Could you please help me?
EDIT: I want the R chunk to be both evaluated (but result hidden) and its code shown. I have meanwhile found this solution, but maybe there is a simpler one: https://tex.stackexchange.com/questions/210003/how-can-i-nest-a-code-chunk-within-an-enumerate-environment-when-using-r-markdow
You need this? See here https://bookdown.org/yihui/rmarkdown-cookbook/raw-latex.html
---
title: "Untitled"
output: pdf_document
date: '2022-05-20'
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, results = "hide")
```
```{=latex}
\begin{tabular}{ll}
x <- 2+3\\
x\\
\end{tabular}
```
In any of the following examples, you can substitute a double return instead of \\.
If you are wanting to just show those expressions then do not put it in a code chunk:
---
title: "Untitled"
output: pdf_document
date: '2022-05-20'
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, results = "hide")
```
\begin{enumerate}
\item
{
x <- 2 + 3\\x
}
\end{enumerate}
This will produce:
Otherwise, if you want to evaluate an inline expression use backticks and r:
---
title: "Untitled"
output: pdf_document
date: '2022-05-20'
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, results = "hide")
```
\begin{enumerate}
\item
{
`r x <- 2 + 3; x`
}
\end{enumerate}
Which produces:
Lastly, you can of course combine these two concepts to show the expression and evaluate the value by doing this:
x <- 2 + 3\\
`r x <- 2 + 3; x`
If your expression is more complex, I would recommend having the code chunk outside of your LaTeX for evaluation.
Update
For simpler expressions you could do something like:
```{r, include = F}
exprsn <- "x <- 2 + 3"
```
\begin{enumerate}
\item
{
`r exprsn`\\
`r eval(parse(text = exprsn)); x`
}
\end{enumerate}
Meanwhile, I found this: How can I nest a code chunk within an enumerate environment when using R Markdown?, which inspires the following solution:
---
title: "Untitled"
output:
pdf_document:
highlight: monochrome
date: '2022-05-20'
header-includes:
- \newcommand{\benum}{\begin{enumerate}}
- \newcommand{\eenum}{\end{enumerate}}
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, results = "hide")
```
\benum
\item
```{r}
x <- 2+3
x
```
\eenum

RMarkdown is not referencing tables

Somehow my RMarkdown document is not crossreferencing tables or figures. Here is a stripped down version of my document.
---
title: "Test"
author: "Me"
date: "01/04/2022"
output: bookdown::pdf_document2
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
var1<-sample(LETTERS)
tab1<-table(var1)
My table is in Table \#ref{tab:tab1}
library(knitr)
kable(tab1, caption="my table")
AS we see in Figure \#ref{fig:plot1}
plot(seq(1,10,1))
You should call your tab1 in the code chunk like this {r tab1}. And use () instead of {} for your #ref. In that case it reference to your figures and tables. You can use the following code:
---
title: "Test"
author: "Me"
date: "01/04/2022"
output: bookdown::pdf_document2
---
My table is in Table \#ref(tab:tab1)
```{r tab1, echo =FALSE}
var1<-sample(LETTERS)
tab1<-table(var1)
library(knitr)
kable(tab1, caption="my table")
```
\newpage
AS we see in Figure \#ref(fig:plot1)
```{r plot1, fig.cap ="plot", echo=FALSE}
par(mar = c(4, 4, .2, .1))
plot(seq(1,10,1))
```
Output:
As you can see on the image, when you click on 1 in will go to your table.

About the hyperlink in RMARKDOWN

In rmarkdown, we can create a catalog using 'toc: yes',using the hyperlink, i can link to subtitle.
Now, i want to link subtitle to catalog ? (in the final html, i with to click subtitle,then back to top of the file)
---
title:'my markdown file'
output:
html_document:
toc: yes
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## subtitle 1 :R Markdown
```{r cars}
summary(cars)
```
## subtitle 2:Including Plots
```{r pressure, echo=FALSE}
plot(pressure)
```
You may enclose subheader in an HTML tag <a>.
---
title: 'my markdown file'
output:
html_document:
toc: yes
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## subtitle 1 :R Markdown
```{r cars}
summary(cars)
as.matrix(rnorm(100))
```
## subtitle 2:Including Plots
```{r pressure, echo=FALSE}
plot(pressure)
```

How to Create Full Width Figures When Using the "twocolumn" "classoption" for Knitr to PDF?

How can one create a full width figure when using the twocolumn class option in knitr / R / RMarkdown / LaTex?
Based on the Knitr documentation, I've tried two approaches. Nothing short of editing the .tex file has worked for me so far.
This:
---
output: pdf_document
classoption: twocolumn
header-includes:
- \usepackage{lipsum}
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
\lipsum[1-2]
```{r fig.env = "figure*", fig.cap = "Test"}
plot(runif(10))
```
\lipsum[3-5]
```{r fig.fullwidth = T}
plot(runif(10))
```
Results in this:
Yihui has fixed this in the development version of knitr. Yihui's response:
The option fig.env = 'figure*' should be respected now (in the dev version of knitr). But the plot will float to a new page. I guess that is a LaTeX issue orthogonal to knitr. Thanks!
Don't forget that you must include a caption for this to work.
---
output: pdf_document
classoption: twocolumn
header-includes:
- \usepackage{lipsum}
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
\lipsum[1-3]
```{r fig.env = "figure*"}
plot(runif(10))
```
\lipsum[2]
```{r fig.env = "figure*", fig.cap = ""}
plot(runif(10))
```
\lipsum[2]

How to include DiagrammeR/mermaid flowchart in a Rmarkdown file

I have an R markdown file:
---
title: "Untitled"
author: "Me"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## R Markdown
This is an R Markdown document.
As well as a DiagrammeR/mermaid chart:
graph LR
A-->B
How can I add the chart in the R-markdown?
Actually it is trivial:
---
title: "Untitled"
author: "Me"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## R Markdown
This is an R Markdown document.
```{r}
library(DiagrammeR)
mermaid("
graph LR
A-->B
")
```

Resources