Home > Software design >  Extracting a list of LaTeX figure and caption labels from RMarkdown to copy into Overleaf
Extracting a list of LaTeX figure and caption labels from RMarkdown to copy into Overleaf

Time:08-13

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?

CodePudding user response:

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}

enter image description here

  • Related