Home > database >  "sort&compress" and "nocite" do not work for latex-pdf in rmarkdown
"sort&compress" and "nocite" do not work for latex-pdf in rmarkdown

Time:01-13

My pure-latex demo.tex is as follows:

\documentclass{article}

\usepackage[hidelinks]{hyperref}
\usepackage[numbers,super,square,sort&compress]{natbib}

\begin{document}

statistics \cite{anderson2003introduction,efron2004least,hastie2009elements}

\bibliographystyle{unsrtnat}
\nocite{*}
\bibliography{ref}
\addcontentsline{toc}{section}{References}

\end{document} 

and it works well, where ref.bib contains

@Book{anderson2003introduction,
  author    = {Anderson, Theodore Wilbur},
  publisher = {Wiley},
  title     = {An introduction to multivariate statistical analysis},
  year      = {2003},
  address   = {New Jersey},
  edition   = {3},
}
@Article{efron2004least,
  author    = {Efron, Bradley and Hastie, Trevor and Johnstone, Iain and Tibshirani, Robert},
  title     = {Least angle regression},
  journal   = {The Annals of Statistics},
  year      = {2004},
  volume    = {32},
  number    = {2},
  pages     = {407--499},
}
@Book{hastie2009elements,
  author    = {Hastie, Trevor and Tibshirani, Robert and Friedman, Jerome},
  publisher = {Springer},
  title     = {The elements of statistical learning: {Data} mining, inference, and prediction},
  year      = {2009},
  address   = {New York},
  edition   = {2},
}
@Book{fan2020statistical,
  author    = {Fan, Jianqing and Li, Runze and Zhang, Cun-Hui and Zou, Hui},
  publisher = {CRC},
  title     = {Statistical foundations of data science},
  year      = {2020},
  address   = {Boca Raton},
}

I want to translate demo.tex to the rmarkdown file tex2rmd.rmd and I try

---
output: 
  pdf_document:
    keep_tex: yes
    citation_package: natbib
natbiboptions: "numbers,super,square,sort&compress"
# natbiboptions: "numbers,super,square" #test
biblio-style: unsrtnat
nocite: '@*'
bibliography: ref.bib
link-citations: yes
colorlinks: no
---

statistics [@anderson2003introduction; @efron2004least; @hastie2009elements]

# References

which gives errors like

! Missing \endcsname inserted.
<to be read again> 
                   \&
l.351 \newcommand
                 \NAT@aysep{,} \newcommand\NAT@yrsep{,} 

Then I check the tex2rmd.tex file from the keep_tex: yes functionality, and I find the problem happens at

\usepackage[numbers,super,square,sort\&compress]{natbib}

where there is sort\&compress instead of sort&compress. How can I fix this?

Moreover, I use natbiboptions: "numbers,super,square" instead for test purpose and find the nocite: '@*' does't work either (it should show all 4 items other than the cited 3 items). Do I ignore something?

CodePudding user response:

Replacing the & from sort&compress with , solves the issue of sort&compress.

And it seems the nocite YAML key doesn't work when you use natbib or biblatex as the citation package. Because nocite key is used by pandoc-citeproc filter, but when natbib or biblatex is used as citation method (default is bibliography


CodePudding user response:

To avoid all the problems with rmarkdown parsing and give you much more freedom, you can directly use the latex code in rmarkdown:

---
output: 
  pdf_document:
    keep_tex: yes
link-citations: yes
colorlinks: no
header-includes:
  - \usepackage[numbers,super,square,sort&compress]{natbib}
---

\nocite{*}

statistics \cite{anderson2003introduction,efron2004least,hastie2009elements}

# References

\bibliographystyle{unsrtnat}
\bibliography{ref}
  • Related