我很确定以前一定有人问过这个问题,但我找不到。
在此 MWE 中,我希望 LaTeX Companion 出现在参考书目中,但带有选定的页面(例如“ pp. 155--162
”),而无需在.bib
文件中编辑其条目。类似于\nocite[155-162]{companion}
。
\documentclass{article}
\usepackage[backend=biber]{biblatex}
\addbibresource{biblatex-examples.bib}
\begin{document}
\noindent I'd like to have The \LaTeX\ Companion appear in the bibliography, but with selected pages (e.g. ``pp. 155--162'') without having to edit its entry in the \verb .bib \ file.
\nocite{companion}
\printbibliography
\end{document}
答案1
总体而言,参考书目条目显示来源的书目数据。对于更具体的精确引用,通常使用\cite
文本中的可选参数。
我并不怀疑在参考书目条目中动态添加页面的合法用途,但目前我想不出一个好的理由,而最好用不同的方法来解决这个问题。
无论如何,这是一个希望强大的实现\nocitepagestoentry[<pages>]{<entrykey>}
。由于我们希望能够在文档的任何位置(甚至在之后\printbibliography
)添加页面,因此我们收集要为每个条目键注入的页面,并将它们写入.aux
文档末尾的文件中。这还有一个额外的好处,那就是我们可以在biblatex
从文件中读取条目数据时注入页面字段.bbl
,以便从那时起我们注入的页面就位于pages
字段中。(因为我们注入的“ pages
”字段未经过 Biber/BibTeX 预处理,所以我们稍微改变了pages
字段格式以保持良好的格式。)
\documentclass{article}
\usepackage[backend=biber]{biblatex}
\makeatletter
\newcommand*{\steveblx@pagesentries}{}
% I know it's a bit pointless to declare #1
% optional and then do nothing if it is not
% present, but that way we mirror the argument
% structure of \cite
% [<pages>]{<entrykey>}
\newcommand*{\nocitepagestoentry}[2][]{%
\ifblank{#1}
{}
{\nocite{#2}%
\ifinlist{#2}{\steveblx@pagesentries}
{}
{\listadd{\steveblx@pagesentries}{#2}}%
\ifcsvoid{steveblx@addpages@#2}
{\csdef{steveblx@addpages@#2}{#1}}
{\csappto{steveblx@addpages@#2}{, #1}}}}
% write pages to aux file
\def\steveblx@writepages#1{%
\expandafter\expandafter\expandafter\steveblx@writepages@i
\expandafter\expandafter\expandafter
{\csname steveblx@addpages@#1\endcsname}{#1}}
\def\steveblx@writepages@i#1#2{%
\blx@auxwrite\@mainaux{}
{\string\steveblx@setpages{#2}{\unexpanded{#1}}}}
\AtEndDocument{%
\forlistloop{\steveblx@writepages}{\steveblx@pagesentries}}
% read pages from aux file
\def\steveblx@setpages#1#2{%
\csgdef{steveblx@savedpages@#1}{#2}}
% inject pages into 'pages' field
\def\steveblx@addpagesfield{\blx@bbl@fielddef{pages}}
\AtDataInput{%
\ifundef\abx@field@pages
{\ifcsvoid{steveblx@savedpages@\abx@field@entrykey}
{}
{\expandafter\expandafter\expandafter\steveblx@addpagesfield
\expandafter\expandafter\expandafter
{\csname steveblx@savedpages@\abx@field@entrykey\endcsname}}}
{}}
\makeatother
% we need \mknormrange because the injected pages 'field' is not preformatted
\DeclareFieldFormat{pages}{\mkpageprefix[bookpagination][\mknormrange]{#1}}
\addbibresource{biblatex-examples.bib}
\begin{document}
I'd like to have The \LaTeX\ Companion appear in the bibliography,
but with selected pages (e.g. ``pp. 155--162'')
without having to edit its entry in the \verb|.bib| file.
\nocitepagestoentry[157-162]{companion}
\printbibliography
\end{document}