Search code examples
r-markdownbookdown

Issues with different citation packages in bookdown


I am creating a pdf-book using bookdown, with a .bib file that holds my citations. I call the citations with the standard [@citation] used in bookdown. Citing works correctly, but the bibliography doesn't seem to be how I wanted it.

Here are the things that I need to work:

  • The bibliography should be included in the toc, at the end of \mainmatter, but before the \backmatter, which I use to attach pdf's as an appendix, where the appendix header is listed in the toc

  • The in-text citations should be in number format, with possible nested citations, like this: [1,2]

  • Bibliography should be in the order they appear in the text, with the possibility of removing DOI/URLs etc as I see fit

For these things to work so far, I've tried the natbib package, which seem to work fine. However, I do not seem to be able to remove DOI/URLs from specific bibliography entries, such as journal articles. Therefore, I tried using biblatex or the built-in pandoc, but neither seem to work properly (With pandoc the bibliography is gone from the toc, and the bibliography entries are messy. With biblatex, i get multiple errors that the program can't find specific entries in the .bib file, which are there)

This is my YAML with natbib:

site: bookdown::bookdown_site
geometry: "left=4cm,right=3cm,top=3cm,bottom=3cm"
subparagraph: true
output:
  bookdown::pdf_book:
    latex_engine: xelatex
    fig_caption: yes
    toc: false
    citation_package: natbib
    includes:
      before_body: frontpage.tex
      after_body: after_body.tex
      in_header: preamble.tex
fontsize: 11pt
linestretch: 1.2
documentclass: book
bibliography: [packages.bib, libraryzotero.bib]
link-citations: yes
---

And this is in my preamble.tex:

\usepackage{titlesec}
\usepackage{pdfpages}
\titleformat{\chapter}
  {\normalfont\LARGE\bfseries}{\thechapter}{1em}{}  % set title format
\titlespacing*{\chapter}{0pt}{3.5ex plus 1ex minus .2ex}{2.3ex plus .2ex}
\AtBeginDocument{\let\maketitle\relax}
\usepackage{makeidx}
\makeindex
\setcitestyle{numbers,square,comma}
\usepackage{url}
\usepackage[nottoc]{tocbibind}
\usepackage{caption}
\captionsetup[figure]{textfont={small,it}, labelfont={normalsize,bf,it}}  % set figure caption font size and style
\usepackage{graphicx}
\usepackage{float}
\let\origfigure\figure
\let\endorigfigure\endfigure
\renewenvironment{figure}[1][2] {
    \expandafter\origfigure\expandafter[H]
} {
    \endorigfigure
}
\raggedbottom
\usepackage{fancyhdr,blindtext}
\fancyhf{}
\fancyhead[LO]{\slshape \rightmark} %section
\fancyhead[RO]{\thepage}
\fancyhead[RE]{\slshape \leftmark} % chapter
\fancyhead[LE]{\thepage}
\setlength{\headheight}{27.7pt} % as requested by fancyhdr's warning
\renewcommand{\sectionmark}[1]{\markright{\thesection.\ #1}}
\makeatletter
\renewcommand{\chaptermark}[1]{%    % change settings for including headers
  \if@mainmatter
    \markboth{Chapter \thechapter{}: #1}{}%
  \else
    \markboth{#1}{}%
  \fi
}
\makeatother
\usepackage{epigraph}
\setcounter{secnumdepth}{1}
\setcounter{tocdepth}{1}

Where \setcitestyle{numbers, square, comma} is for the in-text references.

Does anyone have any suggestions how to get these things to work with bookdown?


Solution

  • For the natbib based solution you could patch the used bibliography style:

    1. You are not setting any bibliography style. Therefore plainnat.bst is used.
    2. Copy plainnat.bst from your TeX installation (typically TEXMF/bibtex/bst/natbib/) to your working directory with a new name.
    3. Add biblio-style: <new-name> to your YAML header.
    4. Edit your BST file:
      • Find FUNCTION {article}.
      • Within that block remove format.doi output and format.url output.
      • Repeat for other types.