Salvador
Salvador

Reputation: 1565

Clash with package Hyperref

Please consider the small reproducible example below. I am getting an error when trying to use hyperref links. I am getting an error saying that there is a clash, however, I have only called the package one time. I wonder if some one can give a hand on how to resolve this problem. The idea is to make a backref from citations to references and back.

---
title: "References"
output:
  pdf_document: 
    number_sections: yes
    toc: yes
    toc_depth: 4
header-includes:
- \usepackage[backref,colorlinks=true,breaklinks=true,linkcolor=red,citecolor=blue]{hyperref} 
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

## R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.

And this is an instance to cite authors \cite{Mills94}.

\newpage
\addcontentsline{toc}{section}{References} 

\color{black}
\bibliography{bib}
\bibliographystyle{apalike}

This is the reference that I am trying to cite. The file name is bib.bib

@misc{Mills94,
  author = {{Mills, T.J., and R. Fisher}},
  year = {1994},
  title = {{Central Valley Anadromous Sport Fish Annual Run-Size, Harvest, and Population Estimates, 1967 through 1991. Inland Fisheries Technical Report prepared for the California Department of Fish and Game}},
  howpublished = {\url{http://www.fws.gov/}},
  note = {Report}
  }

Upvotes: 1

Views: 601

Answers (2)

With the version 2.19 of rmarkdown and pandoc 3.1, the hyperref package is loaded after the header includes. You can pass options to the package like this:

---
title: "References"
output:
  pdf_document: 
    number_sections: yes
    toc: yes
    toc_depth: 4
header-includes:
  - \PassOptionsToPackage{backref,colorlinks=true,breaklinks=true,linkcolor=red,citecolor=blue}{hyperref}
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

## R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.

And this is an instance to cite authors \cite{Mills94}.

\newpage
\addcontentsline{toc}{section}{References} 

\color{black}
\bibliography{bib}
\bibliographystyle{apalike}

Upvotes: 1

Shafee
Shafee

Reputation: 20097

hyperref latex package is loaded automatically by R-markdown. So when you try to load it again with some options, you run into the risk of clash.

Instead, try with Pandoc variables for latex as below,

---
title: "References"
output:
  pdf_document: 
    number_sections: yes
    toc: yes
    toc_depth: 4
hyperrefoptions:
  - backref
colorlinks: true
linkcolor: red
citecolor: blue
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

## R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.

And this is an instance to cite authors \cite{Mills94}.

\newpage
\addcontentsline{toc}{section}{References} 

\color{black}
\bibliography{bib}
\bibliographystyle{apalike}

page 1


page 2


Upvotes: 1

Related Questions