使用 natbib 和 bookdown 压缩内联引用

使用 natbib 和 bookdown 压缩内联引用

我正在使用bookdown来生成一本 pdf 书natbib,现在内联引用如下所示:

[118,119,120,121,142]

然而,我希望它们压缩为:

[118 - 121,142]

我在我的中使用以下内容preamble

\usepackage{natbib}
\setcitestyle{square,comma,numbers}

我尝试了以下方法:

  • \biboptions{sort&compress}如上所述preamble这里,这会引发错误undefined control sequence
  • \setcitestyle{square,comma,numbers,sort&compress}如上所述preamble这里,这根本不会改变内联引用,与\PassOptionsToPackage{square,comma,numbers,sort&compress}{natbib}同一篇文章中描述的一样
  • \setcitestyle{numbers,sort&compress}看看是不是有什么事情搞砸了

我使用了 的稍微修改版本plainnat.bst,其中我只更改了articlemisc函数以排除 URL 等。除此之外,它与 完全相似plainnat.bst

在我的 YAML 文件中index.Rmd我指定了以下内容:

date: "`r Sys.Date()`"
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, library.bib]
biblio-style: [plainnatnew.bst]
link-citations: yes

关于如何获取压缩数字有什么想法吗?

工作示例:

.bib文件

@article{OBrien2011,
  title = {Effect of Diet and Gut Dynamics on the Establishment and Persistence of {{Escherichia}} Coli},
  volume = {157},
  issn = {1350-0872},
  number = {5},
  journal = {Microbiology},
  doi = {10.1099/mic.0.047092-0},
  url = {http://mic.microbiologyresearch.org/content/journal/micro/10.1099/mic.0.047092-0},
  author = {O'Brien, Claire L. and Gordon, David M.},
  month = may,
  year = {2011},
  pages = {1375--1384}
}

@article{Fabrega2009,
  title = {Mechanism of Action of and Resistance to Quinolones},
  volume = {2},
  issn = {17517907},
  number = {1},
  journal = {Microbial Biotechnology},
  doi = {10.1111/j.1751-7915.2008.00063.x},
  url = {http://doi.wiley.com/10.1111/j.1751-7915.2008.00063.x},
  author = {F{\`a}brega, Anna and Madurga, Sergi and Giralt, Ernest and Vila, Jordi},
  month = jan,
  year = {2009},
  pages = {40--61}
}

@article{Chung2002,
  title = {Overexpression of the {{Escherichia}} Coli {{sugE}} Gene Confers Resistance to a Narrow Range of Quaternary Ammonium Compounds},
  volume = {184},
  number = {9},
  journal = {Journal of Bacteriology},
  url = {http://www.ncbi.nlm.nih.gov/pubmed/11948170},
  author = {Chung, Y J and Saier Jr., M H},
  year = {2002},
  pages = {2543--2545},
  pmid = {11948170}
}

@article{Dierikx2013a,
  title = {Presence of {{ESBL}}/{{AmpC}} -{{Producing Escherichia}} Coli in the {{Broiler Production Pyramid}}: {{A Descriptive Study}}},
  volume = {8},
  language = {English},
  number = {11},
  journal = {PloS One},
  doi = {ARTN e79005 DOI 10.1371/journal.pone.0079005},
  author = {Dierikx, C M and {van der Goot}, J A and Smith, H E and Kant, A and Mevius, D J},
  year = {2013},
  keywords = {antimicrobial agents,chickens,enterobacteriaceae,epidemiology,humans,impact,poultry,resistance,spectrum-beta-lactamase,transmission},
  pages = {e79005}
}

@article{Blaak2015,
  title = {Distribution, {{Numbers}}, and {{Diversity}} of {{ESBL}}-{{Producing E}}. Coli in the {{Poultry Farm Environment}}},
  volume = {10},,
  number = {8},
  journal = {PloS One},
  doi = {10.1371/journal.pone.0135402},
  url = {http://www.ncbi.nlm.nih.gov/pubmed/26270644},
  author = {Blaak, H and {van Hoek}, A H and Hamidjaja, R A and {van der Plaats}, R Q and {Kerkhof-de Heer}, L and {de Roda Husman}, A M and Schets, F M},
  year = {2015},
  pages = {e0135402},
  pmid = {26270644}
}

@article{Kingsford2010,
  title = {Assembly Complexity of Prokaryotic Genomes Using Short Reads},
  volume = {11},
  issn = {1471-2105},
  number = {1},
  journal = {BMC Bioinformatics},
  doi = {10.1186/1471-2105-11-21},
  url = {https://bmcbioinformatics.biomedcentral.com/articles/10.1186/1471-2105-11-21},
  author = {Kingsford, Carl and Schatz, Michael C. and Pop, Mihai},
  month = dec,
  year = {2010},
  pages = {21}
}

Lorem ipsum [@OBrien2011;Fabrega2009;Chung2002;Dierikx2013a;Kingsford2010]

答案1

尝试

\documentclass[12pt,a4paper]{article}
\usepackage[sort&compress,numbers]{natbib}
\usepackage{filecontents}
\begin{filecontents}{\jobname.bib}
@article{OBrien2011,
  title = {Effect of Diet and Gut Dynamics on the Establishment and Persistence of {{Escherichia}} Coli},
  volume = {157},
  issn = {1350-0872},
  number = {5},
  journal = {Microbiology},
  doi = {10.1099/mic.0.047092-0},
  url = {http://mic.microbiologyresearch.org/content/journal/micro/10.1099/mic.0.047092-0},
  author = {O'Brien, Claire L. and Gordon, David M.},
  month = may,
  year = {2011},
  pages = {1375--1384}
}

@article{Fabrega2009,
  title = {Mechanism of Action of and Resistance to Quinolones},
  volume = {2},
  issn = {17517907},
  number = {1},
  journal = {Microbial Biotechnology},
  doi = {10.1111/j.1751-7915.2008.00063.x},
  url = {http://doi.wiley.com/10.1111/j.1751-7915.2008.00063.x},
  author = {F{\`a}brega, Anna and Madurga, Sergi and Giralt, Ernest and Vila, Jordi},
  month = jan,
  year = {2009},
  pages = {40--61}
}

@article{Chung2002,
  title = {Overexpression of the {{Escherichia}} Coli {{sugE}} Gene Confers Resistance to a Narrow Range of Quaternary Ammonium Compounds},
  volume = {184},
  number = {9},
  journal = {Journal of Bacteriology},
  url = {http://www.ncbi.nlm.nih.gov/pubmed/11948170},
  author = {Chung, Y J and Saier Jr., M H},
  year = {2002},
  pages = {2543--2545},
  pmid = {11948170}
}
\end{filecontents}
\begin{document}

\citep{Chung2002,Fabrega2009,OBrien2011}

\bibliographystyle{plainnat}
\bibliography{\jobname}
\end{document}

您将获得

在此处输入图片描述

答案2

这是一个迟来的答案,但问题似乎是 YAML 中的 \usepackage{natbib} 引入得很晚,而 header-includes 的内容引入得更早。我使用 preamble.tex 中的以下代码规避了这个问题,该代码在 \begin{document} 之前引入了命令。希望有帮助!

\BeforeBeginEnvironment{document}{\setcitestyle{sort&compress,square,comma,numbers,sort&compress}}

相关内容