rr-markdownrstudiobibtexbibliography

Positioning of bibliographic references in R markdown


I want my references to be placed exactly where I want them to be, I am using in yaml the bibliography command: where I have my citations in bibtex format, and loaded the csl apa format. However, the references come out at the end and I want them to come out where I have the title #References.

Here is my yaml

output:
  pdf_document: null
  latex_engine: xelatex
  word_document: default
toc: false
keep_tex: true
number_sections: false
mainfont: Times New Roman
fontsize: 12pt
header-includes:
- \usepackage{fancyhdr}
- \usepackage{booktabs}
- \usepackage{tabu}
- \pagestyle{fancy}
- \usepackage{titling}
- "\\definecolor{yellow-orange}{rgb}{1, 0.64, 0}"
- "\\pretitle{\\begin{flushleft}\\fontsize{40}{180}\\selectfont\\textcolor{yellow-orange}}"
- \posttitle{\end{flushleft}}
- \usepackage{xcolor}
- \usepackage{tcolorbox}
- \tcbuselibrary{skins}
- \usepackage{setspace}
- \setstretch{1.5}
- \usepackage{lmodern}
- \usepackage[T1]{fontenc}

always_allow_html: true

editor_options:

  markdown: null

  wrap: 72

bibliography: bibliografia.bib

csl: apa.csl

reference_section_title: "Referencias Bibliográficas"

Solution

  • A relevant section in the [Pandoc User's Guide}(https://pandoc.org/MANUAL.html#placement-of-the-bibliography):

    If the style calls for a list of works cited, it will be placed in a div with id refs, if one exists:

    ::: {#refs}
    :::

    Otherwise, it will be placed at the end of the document.

    Therefore your R Markdown could look something like

    ---
    title: "Control Bibliography Placement"
    output: pdf_document
    date: "2024-05-06"
    csl: "apa.csl"
    bibliography: "mybib.bib"
    ---
    
    ```{r setup, include=FALSE}
    knitr::opts_chunk$set(echo = TRUE)
    ```
    
    # Before
    
    This content comes before the bibliography.
    
    # My Bibliography Header
    
    ::: {#refs}
    :::
    
    # After
    
    This content comes after the bibliography.
    

    Note, setting reference-section-title only seems to work when not moving the bibliography location. Instead you can place a header before the refs div block as in this example.