我正在编写一份大型文档,其中包含许多论文,其参考书目结构如下:
/ch1
--paper1.tex
--paper1.bib
--paper1-extra.bib
/ch2
--paper2.tex
--paper2.bib
...
document.tex
我想要做的是在每个 paper*.tex 源中动态地包含 bib 源(类似于addbibresource
),而不是将这样的命令放入根 document.tex 文件中:
\bibliography{ch1/paper1,paper1-extra,ch2/paper1,...}
我的构建脚本是这样的:
pdflatex document
bibtex document
pdflatex document
pdflatex document
有没有办法使用 bibtex 后端来完成它?
答案1
该命令\bibliography
有两个作用:它的参数告诉 BibTeX 在哪里寻找参考文献,并且打印 BibTeX 生成的参考书目。
可以将这两项工作拆分为两个新命令。在下面的示例中,我们定义了一个\printbibliography
仅打印参考书目的命令和一个\addbibfile
可在文档的任何位置多次使用的命令,用于将.bib
文件列表添加到 BibTeX 应查看的文件列表中。的语法\addbibfile
与的参数的语法相同\bibliography
:您可以给出一个不带扩展名的逗号分隔的文件名列表.bib
。给定的文件名将\addbibfile
被收集并写入.aux
文档末尾的 BibTeX 中。
MWE(假设有一个简单的参考书目设置,并且没有任何\input
s 和\include
s,因为它们对于此处提供的代码实际上并不重要)
\documentclass[british]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{babel}
\usepackage{csquotes}
\usepackage{hyperref}
% for list manipulation
\usepackage{etoolbox}
\makeatletter
% redefine \bibliography to only print references
% this is one part of \bibliography's job
\newcommand*{\printbibliography}{%
\@input@{\jobname.bbl}}
% add .bib file to list of searched files
% the argument to this command is a comma-separated
% list of .bib file names without file extension
\newcommand*{\addbibfile}{%
\forcsvlist{\addbibfile@i}}
\newcommand*{\addbibfile@list}{}
\newcommand*{\addbibfile@i}[1]{%
\ifinlist{#1}{\addbibfile@list}
{}
{\listgadd{\addbibfile@list}{#1}}}
% helper macro to convert internal etoolbox list to csv
\newcommand*{\listtocsv}[2]{%
\def#2{}%
\forlistloop{\listtocsv@i{#2}}{#1}}
\newcommand*{\listtocsv@i}[2]{%
\ifdefvoid{#1}
{}
{\appto#1{,}}
\appto#1{#2}}
% write csv-list of .bib files to .aux
\newcommand*{\bibdata@write}[1]{%
\if@filesw
\immediate\write\@auxout{\string\bibdata{\zap@space#1 \@empty}}%
\fi}
\newcommand*{\addbibfile@list@csv}{}
\AtEndDocument{%
\listtocsv{\addbibfile@list}{\addbibfile@list@csv}%
\ifdefvoid{\addbibfile@list}
{}
{\expandafter\bibdata@write\expandafter{\addbibfile@list@csv}}}
\makeatother
\usepackage{filecontents}
\begin{filecontents}{\jobname-one.bib}
@article{sigfridsson,
author = {Sigfridsson, Emma and Ryde, Ulf},
title = {Comparison of methods for deriving atomic charges from the
electrostatic potential and moments},
journal = {Journal of Computational Chemistry},
year = 1998,
volume = 19,
number = 4,
pages = {377-395},
doi = {10.1002/(SICI)1096-987X(199803)19:4<377::AID-JCC1>3.0.CO;2-P},
}
@book{nussbaum,
author = {Nussbaum, Martha},
title = {Aristotle's \enquote{De Motu Animalium}},
year = 1978,
publisher = {Princeton University Press},
location = {Princeton},
}
\end{filecontents}
\begin{filecontents}{\jobname-two.bib}
@phdthesis{geer,
author = {de Geer, Ingrid},
title = {Earl, Saint, Bishop, Skald~-- and Music:
The {Orkney Earldom} of the Twelfth Century. {A} Musicological
Study},
school = {Uppsala Universitet},
year = 1985,
address = {Uppsala},
}
@book{worman,
author = {Worman, Nancy},
title = {The Cast of Character},
year = 2002,
publisher = {University of Texas Press},
address = {Austin},
}
\end{filecontents}
\begin{document}
\cite{sigfridsson,nussbaum,geer,worman}
\addbibfile{\jobname-one}
\addbibfile{\jobname-two}
\bibliographystyle{plainurl}
\printbibliography
\end{document}
请注意,此解决方案仅生成一个与正常相同的全局书目\bibliography
。对于与和朋友的拆分书目,必须进行额外的工作multibib
。