“排序和压缩”和“nocite”不适用于rmarkdown中的latex-pdf

t40tm48m  于 2023-01-15  发布在  其他
关注(0)|答案(2)|浏览(327)

我的纯乳胶demo.tex如下:

\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}

它运行良好,其中ref.bib包含

@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},
}

我想将demo.tex转换为rmarkdown文件tex2rmd.rmd,并尝试

---
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

这会产生如下错误

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

然后,我从keep_tex: yes功能检查tex2rmd.tex文件,发现问题发生在

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

其中存在sort\&compress而不是sort&compress。如何修复此问题?
此外,我使用natbiboptions: "numbers,super,square"代替测试目的,发现nocite: '@*'也不工作(它应该显示所有4项,而不是引用的3项)。我忽略了什么吗?

n9vozmp4

n9vozmp41#

sort&compress中的&替换为,解决了sort&compress的问题。
当你使用natbibbiblatex作为引用包时,nocite YAML键似乎不起作用,因为nocite键被pandoc-citeproc过滤器使用,但当natbibbiblatex作为引用方法时(默认为citeproc),pandoc-citeproc不起作用,nocite也不起作用。
在这种情况下,您需要使用原始LaTeX命令\nocite(如下所述)

---
output: 
  pdf_document:
    keep_tex: yes
    citation_package: natbib
natbiboptions: "numbers,super,square,sort,compress"
biblio-style: unsrtnat
bibliography: ref.bib
link-citations: yes
colorlinks: no
---

\nocite{*}

statistics [@anderson2003introduction; @efron2004least; @hastie2009elements]

# References

lymnna71

lymnna712#

为了避免rmarkdown解析的所有问题并给予您更多的自由,您可以直接在rmarkdown中使用latex代码:

---
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}

相关问题