Rmarkdown with html, how to reference figures? - r-markdown

Using RMarkdown I always generate pdf documents via Rmd -> pandoc -> TeX -> pdflatex -> pdf and accomplish figure references using \\label{something} in the fig.cap as in the follow example:
---
title: "Foo"
author: "Mark Andrews"
date: "10 January 2019"
output: pdf_document
---
See Figure \ref{figfoo} and see Figure \ref{figbar}.
```{r fig1, fig.cap='A figure\\label{figfoo}'}
plot(rnorm(10))
```
```{r fig2, fig.cap='Another figure\\label{figbar}'}
plot(rnorm(10))
```
If I change output: pdf_document to output: html_document, that does not work, understandably because it is relying on LaTeX's cross referencing system.
So how do Figure references work with html_document in RMarkdown?
The following does not work:
---
title: "Foo"
author: "Mark Andrews"
date: "10 January 2019"
output: html_document
---
See Figure \#ref(fig:fig1) and see Figure \#ref(fig:fig2).
```{r fig1, fig.cap='A figure}'}
plot(rnorm(10))
```
```{r fig2, fig.cap='Another figure'}
plot(rnorm(10))
```
But the following does work:
---
title: "Foo"
author: "Mark Andrews"
date: "10 January 2019"
output: bookdown::html_document2
---
See Figure \#ref(fig:fig1) and see Figure \#ref(fig:fig2).
```{r fig1, fig.cap='A figure}'}
plot(rnorm(10))
```
```{r fig2, fig.cap='Another figure'}
plot(rnorm(10))
```
Does that mean that the only way to cross-reference figures when producing html from Rmarkdown is to use output: bookdown::html_document2. That's fine, if so, but am I missing something?

Having heard from Yihui Xie, I think we can take it for granted that yes, the only way to do figure cross references in html_document in rmarkdown is to do
---
output: bookdown::html_document2
---
in the header.

Related

How to print special Latin characters in Markdown pdfs

I need to knit my Markdown script into a pdf file, but when knitted, the pdf file does not show special Latin characters, such as those used in the International Phonetic Alphabet (IPA).
---
title: "MarkdownIPA"
date: '2022-07-06'
output:
pdf_document:
latex_engine: xelatex
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
`Print IPA`
```{r}
'[aɪ.pʰiː.ɛɪ]'
```
Which rends:
How can I fix this?
You'll need to use a font that supports those glyphs. A (IMHO) rather nice one is Source Sans Pro and the monospace variant Source Code Pro.
To use Source Code Pro, you can add the respective LaTeX code directly:
---
title: "MarkdownIPA"
date: '2022-07-06'
output:
pdf_document:
latex_engine: xelatex
header-includes:
- \usepackage{sourcecodepro}
---
Result:
The sourcecodepro package is likely already installed; otherwise you'd have to install it first.

Missing tittle and author

I have a Rmd (see bellow) file and when I execute the file without toc and pandoc I am able to get the title and author, but with them I am not able to see them. How can I get my title and author name to appear with numbered sections and toc?
---
title: "Design"
author: "Jen"
output:
html_document:
toc: true
pandoc_args: [
"--number-sections"
]
---
# plots
You should give rmarkdown full control over the pandoc arguments:
---
title: "Design"
author: "Jen"
output:
html_document:
toc: true
number_sections: true
---
# plots
Source: https://bookdown.org/yihui/rmarkdown/html-document.html#section-numbering

Running code chunk in R-markdown using exam document class

I would like to run a code chunk under a question level in the exam document class, but I keep receiving errors. I am assuming this is because it believes the output from the R-code is Latex code.
---
output: pdf_document
documentclass: exam
header-includes: \usepackage{float}
---
\begin{questions}
\question Answer question...
```{r}
iris%>%
group_by(Species)%>%
summarize(Total=n())
```
\end{questions}
Sometimes \begin{"some environment") ... \end{"some environment") doesn't play well with R chuncks. One work around is to define a new environment.
For example, I defined a file preamble.tex with the following information:
preamble.tex
\usepackage{float}
\newcommand{\bQ}{\begin{questions}}
\newcommand{\eQ}{\end{questions}}
Then, I ran the following.
exam.Rmd
---
documentclass: exam
geometry: margin=.5in
output:
pdf_document:
highlight: haddock
includes:
in_header: preamble.tex
before_body: doc-prefix.tex
after_body: doc-suffix.tex
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
\bQ
## Including Plots
\question You can also embed plots, for example:
```{r pressure, echo=FALSE}
plot(pressure)
```
```{r cars}
summary(cars)
```
\question We can keep the pound signs.
\eQ
Here is the resulting output.
Output
I managed to get something working. It needs some libraries and uses knitr to create the output.
---
output:
pdf_document:
keep_tex: true
documentclass: exam
header-includes: \usepackage{float}
---
```{r setup, include=TRUE,echo=FALSE,message=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(dplyr)
library(knitr)
```
\begin{questions}
\question Answer question...
```{r, echo=F, comment=NA}
summary = iris %>% group_by(Species) %>% summarize(Total=n())
kable(summary, format='latex')
```
\end{questions}
The problem seems to be the inclusion of # in the output from R for processing by Latex. I avoid this by using kable.

How to link a hmtl page to another html in Rmarkdown?

I have page 1 saved in html by Rmarkdown and I want to put a link to the second page of html made by Rmarkdown. How to do this in Rmarkdown?
page1.rmd:
---
title: "page1"
author: "ww"
date: "25 lutego 2017"
output: html_document
---
###working directory
My rmarkdown files are saved here: `r getwd()`
The list of saved files in working directory:
```{r echo=T}
as.list(list.files(getwd()))
```
##checking the link to the next page
[LINK to another html file]("C:/R/rmarkdown/page2.html")
page2.rmd:
---
title: "page2"
author: "ww"
date: "25 lutego 2017"
output: html_document
---
#tytuł
this is the second document
it works!!! :)
Unfortunately it doesn't work :(
The files are in the same directory C/R/rmarkdown

How to I vertically center the title of my rmarkdown pdf document?

I have the following preamble:
---
title: "Luke Klein Dissertation MARKDOWN ROUGH DRAFT"
author: "Luke KLEIN"
date: "`r Sys.Date()`"
output:
pdf_document:
toc: true
---
How do I get my title centered on the cover page?
You probably already finished your dissertation :-) but leaving the solution for future reference as i came here through google.
---
title: |
| \vspace{5cm} \LARGE{Luke Klein Dissertation MARKDOWN ROUGH DRAFT}
author: "Luke KLEIN"
date: "`r Sys.Date()`"
output:
pdf_document:
toc: true
---
Using a combination of vspace and hspace you can put the title anywhere you want.
Other combinations of fonts (both size and type) can be seen here:
https://www.sharelatex.com/learn/Font_sizes,_families,_and_styles?nocdn=true
Late to the party, this worked for me :
---
title: "Luke Klein Dissertation MARKDOWN ROUGH DRAFT"
author: "Luke KLEIN"
date: "`r Sys.Date()`"
output:
pdf_document:
toc: true
header-includes:
- \pretitle{\begin{center} \vspace{5cm}}
---