Change location of bibliography in bookdown latex

latex
bookdown

#1

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:

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

My preamble.tex file looks like this:

\usepackage{booktabs}
\usepackage{setspace}
\usepackage{lineno}

% turn off drawing the title
\makeatother
\let\oldmaketitle\maketitle
\AtBeginDocument{\let\maketitle\relax}

% don't remember what this does
\usepackage[section]{placeins}

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

% set up captions
\usepackage[labelfont=bf,labelsep=period]{caption}
\captionsetup{width=\textwidth}


#2

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:

bookdown::pdf_book:
  includes:
    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:

\clearpage

\begin{figure}
  \centering
  \include_graphics{path/to/figure.png}
  \caption{My Caption}
  \label{fig:my-label}
\end{figure}

\clearpage

% 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!