Rmarkdown Table of Contents on page 2 and cross referencing - r

I would like my pdf output to satisfy 3 conditions:
Title page on page 1
Table of contents on page 2
Cross reference figures and tables.
I am able to do 1 and 2 with pdf_document but as explained here, pdf_document2 is required for 3.
How can I build a pdf document that can satisfy these 3 conditions? I would like to avoid setting my document class to a report if possible.
This answer is the closest I have found.
Sample code:
---
title: "Untitled"
output:
pdf_document:
number_sections: true
---
\newpage
\tableofcontents
\listoffigures
\listoftables
\newpage
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## R Markdown
Figure (...) below is a
```{r pressure}
plot(pressure)
```

You just need to add \ref{}
## R Markdown
Figure (\ref{fig:pressure}) below is a ...
```{r pressure}
plot(pressure)
```

Related

Producing a "self contained" flextable to use in latex

I'm need to generate a "bunch" of tables in R that must be included in a Latex document.
I can create very nice tables using flextable... but I am not able to generate ANY format that can then be inserted in (multiple places) in a latex document.
The problem is that any method that I try will add extra "margins" (like in pdf... prints a table to a full page) or has some limitation (like png).
Does any ne has a solution, either using directly an RScript or trough knitting R-Markdown?
(Note: The solution of producing the whole latex doc in RMarkdown is Not feasible.)
Just to present an example. I would like this would produce a Full-Page (with dimensions of the table... not A4) of the table.
Thank you very much for your Help :).
---
title: "Untitled"
output:
pdf_document:
latex_engine: xelatex
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(flextable)
```
```{r}
ft <- flextable(head(airquality))
ft <- autofit(ft)
theme_vader(ft)
```
You could use the standalone class (a bit of extra space is still there, but much less than a full page):
---
documentclass: standalone
classoption: varwidth
output:
pdf_document:
latex_engine: xelatex
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
library(flextable)
```
```{r}
ft <- flextable(head(airquality))
ft <- autofit(ft)
theme_vader(ft)
```

Extracting a list of LaTeX figure and caption labels from RMarkdown to copy into Overleaf

I am working with coauthors. We want to produce all figures and tables (including an appendix) in a single RMarkdown document but write the paper jointly in Overleaf (excellent for simultaneous editing, which we don't need for the statistical code).
Here is an example Rmd which knits to a pdf. (The pdf will be appended manually to the main paper.)
---
title: "Nice document"
output: pdf_document
date: '2022-08-12'
---
```{r}
library(kableExtra)
```
```{r pressure, echo=FALSE, fig.cap="\\label{fig:pressure} Nice caption."}
plot(pressure)
```
```{r echo=FALSE, fig.cap="\\label{fig:diffpressure} Better caption."}
kable(head(mtcars), longtable = T, booktabs = T, caption = "Cool table", label = "tab:carssummary")
```
\appendix
\setcounter{figure}{0}
\setcounter{table}{0}
\renewcommand{\thefigure}{S\arabic{figure}}
\renewcommand{\thetable}{S\arabic{table}}
```{r echo=FALSE, fig.cap="\\label{fig:diffpressure} Better caption."}
plot(pressure/3.5)
```
I want a extract a character vector from the Rmd that is the following:
\begin{figure}
\caption{empty}
\label{fig:pressure}
\end{figure}
\begin{table}
\caption{empty}
\label{tab:carssummary}
\end{table}
\appendix
\setcounter{figure}{0}
\setcounter{table}{0}
\renewcommand{\thefigure}{S\arabic{figure}}
\renewcommand{\thetable}{S\arabic{table}}
\begin{figure}
\caption{empty}
\label{fig:pressure2}
\end{figure}
I will then copy and paste this to the bottom of the Overleaf doc, enabling us to do the automated cross referencing with minimal hassle (and easy updating if and when the analysis output changes).
How can I extract that LaTeX code from the Rmd?
Assuming your markdown document is called test.rmd and you included the keep_tex: true option to the header
---
title: "Nice document"
output:
pdf_document:
keep_tex: true
date: '2022-08-12'
---
You can upload the test.aux file to overleaf and then include the cross-refs in your overleaf document with the xr-hyper package:
\documentclass{article}
\usepackage{xr-hyper}
\externaldocument{test}
\usepackage{hyperref}
\begin{document}
some cross-ref: \ref{fig:diffpressure}
\end{document}

header on first page and others

a header is created for pages 2+ with fancyhdr but how can you make the same header also appear on the first page?
here is the rmarkdown:
---
title: "Untitled"
classoption: landscape
output:
pdf_document:
number_sections: false
dev: pdf
keep_tex: false
toc: yes
header-includes:
- \usepackage{fancyhdr}
- \pagestyle{fancy}
- \fancyhead[C]{center text}
- \fancyhead[R]{right text}
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## R Markdown
This is an R Markdown document. 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. You can embed an R code chunk like this:
```{r cars}
summary(cars)
```
## Including Plots
You can also embed plots, for example:
```{r pressure, echo=FALSE}
plot(pressure)
```
Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.
Include \thispagestyle{fancy} in the beginning of your document (e.g. just after the yaml header) to make the first page fancy.
Edit to address your comment
If you want to have different center text in the first page you can have use if when you define \fancyhead
\fancyhead[C]{\ifnum\value{page}>1 center text \else \fi}

R Markdown : \#ref() not working

I'm having a lot of trouble getting basic references to work in R Markdown. To reduce complexity from my original project, I've decided to use the bookdown example code, but I'm experiencing the same problem. Here's a link to the intro exmample code: https://github.com/rstudio/bookdown-demo/blob/master/01-intro.Rmd
When I use Knitr to HTML or PDF the file is being generated fine but the references are not working, instead the file will just containt "#ref(example)". Here is an image to show better the output (my emphasis added in red):
Direct link to image: https://i.imgur.com/2yxB5h3.png
Here is a minimal example:
---
title: "Minimal"
output:
pdf_document:
fig_caption: yes
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
Here is a reference to the plot below \#ref(fig:minGraph)
```{r minGraph, echo=FALSE, fig.cap="\\label{fig:minGraph}test"}
plot(x=1)
```
With the output appearing as such:
https://i.imgur.com/J3UECqn.png
If you want make use of the bookdown extensions in a normal rmarkdown document you can use bookdown::html_document2 and bookdown::pdf_document2 instead of rmarkdown::html_document and rmarkdown::pdf_document. Example:
---
title: "Minimal"
output:
bookdown::html_document2:
fig_caption: yes
bookdown::pdf_document2:
fig_caption: yes
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
Here is a reference to the plot below \#ref(fig:minGraph)
```{r minGraph, echo=FALSE, fig.cap="test"}
plot(x=1)
```
Looks like I was getting my syntax confused by reading the bookdown guide while using just R markdown. Thanks to Ralf for pointing me in the this direction. The correct minimal code would be like so:
---
title: "Minimal"
output:
pdf_document:
fig_caption: yes
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
Here is a reference to the plot below \ref{fig:minGraph}
```{r minGraph, echo=FALSE, fig.cap="\\label{fig:minGraph}test"}
plot(x=1)
```

output markdown in r code chunk

I have a R markdown file that I want to output rmarkdown from the script itself. For example, I would have the following simple code in an Rmd file.
---
title: "test"
author: "johndoe"
date: "September 5, 2015"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r cars}
paste("## This is a Heading in Code")
summary(cars)
```
I want "This is a Heading in Code" to render in rmarkdown. There is a solution in an R script to generate markdown as per http://rmarkdown.rstudio.com/r_notebook_format.html. But I am trying to figure out how to do this in a Rmarkdown file. Any help appreciated. Thanks.
Why build the header markup (either in markdown or HTML) manually? Try inline R expressions or some helper functions in pander (to generate markdown programatically):
---
title: "test"
author: "johndoe"
date: "September 5, 2015"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## `r 'This is a Heading in Code'`
```{r title, results='asis'}
library(pander)
pandoc.header("This is a Heading in Code", level = 2)
```
```{r cars, results='asis'}
summary(cars)
```
I searched for a good answer for this for some time after using cat("## Heading") inside results='asis' code chucks. I have seen many people dissatisfied by the results='asis' setting in the code chunk because it sets all results of the code chunk to not be wrapped in a code markup block. We have many cases when we want to output the heading along with results that should be wrapped in markup (e.g. a kable table that renders to a html table).
Here is the solution I found by simply specifying the "asis" attribute to the text object with knitr::asis_output and keeping the code chunk in the default 'markup' setting.
---
title: "test"
author: "johndoe"
date: "September 5, 2015"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r cars}
knitr::asis_output("## This is a Heading in Code")
summary(cars)
knitr::kable(summary(cars))
```
Unfortunately, at the current time knitr::asis_output only works in top-level R expressions, and it will not work when it is called inside another expression, such as a for-loop.

Resources