In general the idea is that the bibliography entry shows the bibliographic data of your source. For more specific pinpoint references one usually uses the optional argument of \cite
in the text.
I don't doubt that there are legitimate uses to adding pages to bibliography entries on the fly, but I can't think of a good reason at the moment that would not better be covered by a different approach.
Anyway, here is a hopefully robust implementation of a \nocitepagestoentry[<pages>]{<entrykey>}
. Since we want to be able to add pages anywhere in the document (even after \printbibliography
) we collect the pages we want to inject for each entry key and write them to the .aux
file at the end of the document. This has the added advantage that we can inject the pages field when biblatex
reads the entry data from the .bbl
file so that from then on our injected pages are in the pages
field.
(Because our injected 'pages
' field is not preprocessed by Biber/BibTeX we slightly alter the pages
field format to take care of nice formatting.)
\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}
