title: Extracting a list of LaTeX figure and caption labels from RMarkdown to copy into Overleaf
body: <p>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).</p>
  <p>Here is an example Rmd which knits to a pdf. (The pdf will be appended manually to the main paper.)</p>
  <pre><code>---
  title: &quot;Nice document&quot;
  output: pdf_document
  date: '2022-08-12'
  ---
  
  ```{r}
  library(kableExtra)
  ```
  
  
  ```{r pressure, echo=FALSE, fig.cap=&quot;\\label{fig:pressure} Nice caption.&quot;}
  plot(pressure)
  ```
  ```{r echo=FALSE, fig.cap=&quot;\\label{fig:diffpressure} Better caption.&quot;}
  kable(head(mtcars), longtable = T, booktabs = T, caption = &quot;Cool table&quot;, label = &quot;tab:carssummary&quot;) 
  ```
  
  
  \appendix
  
  \setcounter{figure}{0}
  \setcounter{table}{0}
  \renewcommand{\thefigure}{S\arabic{figure}}
  \renewcommand{\thetable}{S\arabic{table}}
  
  
  ```{r echo=FALSE, fig.cap=&quot;\\label{fig:diffpressure} Better caption.&quot;}
  plot(pressure/3.5)
  ```
  </code></pre>
  <p>I want a extract a character vector from the Rmd that is the following:</p>
  <pre><code>\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}
  </code></pre>
  <p>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).</p>
  <p>How can I extract that LaTeX code from the Rmd?</p>