Search code examples
rpdflatexr-markdownbiblatex

Compiling latex document in R Markdown


I am trying to compile a latex document in R markdown using R studio. These are the header options -

---
header-includes:
   - \usepackage{float}
   - \usepackage{wrapfig}
   - \usepackage{graphicx}
   - \usepackage{lipsum}
   - \usepackage[fontsize=12pt]{scrextend}
   - \usepackage{fontspec}
   - \newcommand*{\bibfont}{\footnotesize}
   - \PassOptionsToPackage{dvipsnames}{xcolor} 
   - \usepackage[dvipsnames]{xcolor}
   - \usepackage{etoolbox}
   - \defbibheading{bibliography}[\textcolor{Blue}\refname]{}
   - \usepackage{blindtext}
   - \usepackage{needspace}
   - \usepackage{mwe}
output: 
  pdf_document:
    latex_engine: xelatex
    fig_caption: true
    citation_package: biblatex
bibliography: Gibbs.bib
mainfont: ArialMT
geometry: margin=0.70in
---

And this is the error I get -

! Undefined control sequence.
<recently read> \defbibheading 

Any help? Thanks!


Solution

  • The problem is that while rmarkdown will automatically load the biblatex package due to your citation_package: biblatex option, it will do so only after it has already processed your header includes. This means you can't directly use a macro like \defbibheading{} from the biblatex package in your header-includes.

    As a workaround, you could try

    ---
    header-includes:
       - \usepackage{float}
       - \usepackage{wrapfig}
       - \usepackage{lipsum}
       - \usepackage[fontsize=12pt]{scrextend}
       - \usepackage{fontspec}
       - \PassOptionsToPackage{dvipsnames}{xcolor} 
       - \usepackage[dvipsnames]{xcolor}
       - \usepackage{etoolbox}
       - \AtBeginDocument{\renewcommand*{\bibfont}{\footnotesize}\defbibheading{bibliography}[\textcolor{blue}{\refname}]{}}
       - \usepackage{blindtext}
       - \usepackage{needspace}
       - \usepackage{mwe}
    output: 
      pdf_document:
        keep_tex: true
        latex_engine: xelatex
        fig_caption: true
        citation_package: biblatex
    bibliography: Gibbs.bib
    mainfont: ArialMT
    geometry: margin=0.70in
    ---
    
    test