Change location of bibliography in bookdown latex



I'm writing a manuscript using bookdown and submitting only the PDF output. I would like the order of the sections to be body, references, tables, then figures. Right now I'm getting it the references at the end after the figures. The "body" in my case is made of 5 .Rmd files (one for each section of the MS, like intro, methods, etc.) and the figures and tables are generated in two separate .Rmd files.

Does anyone know how I can set the location of the bibliography to be somewhere other than the end of a document in bookdown? I can turn off the bibliography in the index.Rmd YAML header, and add \usepackage{natbib} to the preamble.tex and \bibliography{cites.bib} where I want it. But then that "breaks" some of the bookdown shortcuts (like using @name-year instead of \cite{name-year} for in-text citations). So I'd prefer a more "bookdown" based fix rather than a LaTeX fix.

Thank you!

My YAML header of index.Rmd looks like this:

site: bookdown::bookdown_site
documentclass: article
classoption: leqno
bibliography: [cites.bib]
biblio-style: apalike
link-citations: yes
fontsize: 12pt

My _output.yml file looks like this:

    in_header: preamble.tex
  latex_engine: pdflatex
  citation_package: natbib
  keep_tex: yes
  toc: false

My preamble.tex file looks like this:


% turn off drawing the title

% don't remember what this does

% set up line spacing and paragraph spacing
\setlength{\parskip}{0pt plus 0pt minus 0pt}

% set up captions


I was able to figure this out by placing my figures and tables in the after_body section. The bibliography gets placed immediately after the body text and now the tables and figures show up after it. I altered my _output.yml file to look like this:

    in_header: preamble.tex
    after_body: [tables.tex,figures.tex]
  latex_engine: pdflatex
  citation_package: natbib
  keep_tex: yes
  toc: false

Where tables.tex is generated by a R script that looks something like this:

knitr::kable(df, "latex") %>%
  kableExtra::kable_styling() %>%
  writeLines(con = "tables.tex")

And figures.tex looks something like this:


  \caption{My Caption}


% more figures below

This is something of a workaround and has a distinct "non-bookdowny" feel to it, but it works for me. It is a bit of a pain because if I change the table, I have to re-create the tables.tex document. I've written a shell script to get around this problem which builds the tables.tex document and builds the book using bookdown::render_book().

If anyone has a better solution, I'd be excited to hear it!