How do I include a footnote in a figure caption? - r-markdown

I am using R Markdown to generate both HTML and PDF versions of the same document. Some figure captions have footnotes referencing the source of the figure. The following code generates figure captions with footnotes in the HTML.
{r, echo=FALSE, out.width="75%", fig.align = "center", fig.show='hold', fig.cap="My figure caption. ^[footnote text.]"}
knitr::include_graphics("My figure.png")
However, when I try to generate the PDF it returns the following error message:
! Missing $ inserted.
<inserted text>
$
I have been unable to determine how $ $ is used in this context. Can anyone help me?

Related

How do I knit to two formats simulateously with Quarto?

Is it possible to create multiple output formats from a Quarto/R Markdown document at once by specifying them in the YAML header?
My guess did not work:
---
title: Stacked Area chart with Annotations
format:
- html
- gfm
---
Try this way (and suppose file name is format_test.qmd)
format_test.qmd
---
title: "Untitled"
format:
html: default
gfm: default
---
## Quarto
Quarto enables you to weave together content and executable code
into a finished document. To learn more about Quarto
see <https://quarto.org>.
## Running Code
When you click the **Render** button a document will
be generated that includes both content and the output
of embedded code. You can embed code like this:
```{r}
1 + 1
```
Then from console run this following code to generate both html and github flavoured markdown (gfm) file at once.
# install.packages("quarto")
quarto::quarto_render("format_test.qmd", output_format = "all")
Or if you prefer using the terminal (or if you are a non R-useR) you can run the following command in the terminal,
quarto render format_test.qmd --to gfm,html
Be careful to specify multiple formats without putting any space in between i.e. type them as gfm,html.
And running the above command in the terminal does not require you to specify format yaml option in your document. So, for
format_test.qmd
---
title: "Untitled"
---
## Quarto
Quarto enables you to weave together content and executable code
into a finished document. To learn more about Quarto
see <https://quarto.org>.
quarto render format_test.qmd --to pdf,html,gfm
will generate three files in HTML, pdf, and markdown format.

Ordering of code chunk outputs in Rmd/HTML reports

I have a summary figure that I want to insert at the top of my HTML report generated using knitr/rmarkdown. But the inputs of this figure depend on outputs generated from downstream code chunks and child documents. Is there a way to do this without having to put the summary figure at the end of the document?
I have tried ref.label='last' option in the top level code chunk (with eval=FALSE) to add a duplicate code chunk in the end of the document {r last, eval=TRUE}, but has not worked for me.
Any suggestions?
You can insert HTML anywhere into your document referencing the figure by the name knitr will use for it.
To figure out the name, temporarily change the YAML header to something like this:
output:
html_document:
self_contained: FALSE
This won't embed figures in the output, they'll be kept in a separate directory. Open the output in an editor, or view source in a browser, and look for the line that displays the figure. In my test example below, it was being displayed by this code:
<p><img src="Untitled2_files/figure-html/rnormplot-1.png" width="672" /></p>
I copied that line into the document, and removed the self_contained setting, and got this source code:
---
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
This is the figure before it was generated:
<p><img src="Untitled2_files/figure-html/rnormplot-1.png" width="672" /></p>
And here it is being generated:
```{r rnormplot}
x <- rnorm(100)
plot(x)
```
One key is to name the code chunk that generates the plot (mine is named rnormplot) because that helps to make sure the figure name is independent of the rest of the document.

R Markdown - HTML Output in Powerpoint

I try to get Highouts Output in my Powerpoint presentation, but the suggestion does not work .This feature is still pretty new and I was not able to find something valueable in the docu.
---
title: "Untitled"
output:
beamer_presentation: default
powerpoint_presentation: default
ioslides_presentation: default
always_allow_html: yes
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
library(highcharter)
```
### R Markdown
This is an R Markdown presentation. Markdown is a simple formatting
syntax for authoring HTML, PDF, and MS Word documents. For more
details on using R Markdown see <http://rmarkdown.rstudio.com>.
When you click the **Knit** button a document will be generated that
includes both content as well as the output of any embedded R code
chunks within the document.
### Slide with Bullets
- Bullet 1
- Bullet 2
- Bullet 3
### Slide with R Output
```{r cars, echo = TRUE}
highchart() %>%
hc_add_series(data = mtcars, type = "bar", hcaes(y = cyl))
```
### Slide with Plot
```{r pressure}
plot(pressure)
```
Produces:
Functions that produce HTML output found in document targeting pptx output.
Please change the output type of this document to HTML. Alternatively, you can allow
HTML output in non-HTML formats by adding this option to the YAML front-matter of
your rmarkdown file:
always_allow_html: yes
Note however that the HTML output will not be visible in non-HTML formats.
I added the always_allow_html: yes part to the top, but it still does not work. Can anyone help me?

Add markdown code chunk to R Markdown document

I would like to write an R Markdown document which presents code examples of how to write an R Markdown document. For example, I want to show in the document how to render text as bold.
`**this is bold**` will render 'this is bold' with bold text, i.e. **this is bold**
That works fine, the ` render the text within as code. However, I can't figure out how to get a code chunk to display properly. e.g.
```{markdown, eval=FALSE}
```{r}
x = rnorm(1)
```
```
This doesn't work because markdown isn't a supported language. I can't enclose the r code block in ` because I need that symbol to mark the beginning of the code chunk and it only works inline.
I can do some hoop jumping by actually using R
```{r, echo="FALSE"}
o = "```{r}\nx=sample(1)\n```\n"
cat(o)
```
which renders as
## ```{r}
## x=sample(1)
## ```
But this is more complicated for me writing the document and the code it generates doesn't allow for simple copy/paste.
Is there a native way to render as code the markdown necessary to create the R code block?
I found the bookdown book on Rmarkdown which has examples of R code chunks being rendered via Rmarkdown. The book source is available on GitHub and the relevant chapter is located here.
The chunk below works as desired. I can't explain why the `r ''` is required at the beginning of the block, but it is.
````markdown
`r ''````{r}
x = sample(1)
```
````
If you don't put {r} after the three backticks, rmarkdown will just pass the block on to markdown and it almost works. For example, this
```
```{r}
x = rnorm(1)
```
```
displays as
It's not quite right; the braces around the r have been removed. (I think r-markdown did that, not markdown.) I don't know if there's an option to force them in, but they will appear if there's a non-letter ahead of the r. You can put in a space, or (if you can figure out how) a zero width space, which R will display using "\u200B".
You can use the experimental knitrhooks package, which adds the chunk option chunk_head to enable us to keep the header. The package is in development on GitHub here.
To install the package, use the command devtools::install_github("nathaneastwood/knitrhooks"). Once installed, you can use the chunk hook by loading the package and then calling the function chunk_head() in your header.
Here is an example:
---
title: "Untitled"
output: pdf_document
---
```{r, include = FALSE}
library(knitrhooks)
chunk_head()
```
```{r, chunk_head = TRUE, echo = TRUE}
x <- sample(1)
```
Unfortunately, this will not display syntax highlighting, but as discussed within this issue, this is a limitation within how knitr processes the file.

get rid of captions when using texreg in Rmarkdown

How do you suppress captions for a texreg table? I'm using Rmarkdown to generate a LaTeX file. Here's a simple example:
```{r, echo=FALSE, message=FALSE, results="asis"}
library(texreg)
data <- data.frame(a=c(1,2,3,4), b=c(6,3,4,4))
texreg(lm(a~b, data=data), caption="", custom.note="", float.pos="h!")
```
The table I get has a caption on the bottom that says "Table 1:". How do I get rid of it? Thanks.
In the YAML section where LaTeX packages can be included, add the caption package:
header-includes:
- \usepackage{caption}
Then at the beginning of the RMarkdown document body add:
\captionsetup[table]{labelformat=empty}
This removes the caption labels for all tables.
In case you have an external .tex file for additional formatting (i. e. I made a file for including a logo in the header of each page as described here) you should include \usepackage{caption} (just this line) in the external .tex file, not in the yaml header of the RMarkdown document.