0

I'm pretty sure this must've been asked before, but I couldn't find it.

In this MWE, 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 .bib file. Something like \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}

screenshot of output

1 Answer 1

1

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}

Michel Goossens, Frank Mittelbach, and Alexander Samarin. The LaTeX Companion. 1st ed. Reading, Mass.: Addison-Wesley, 1994, pp. 157–162. 528 pp.

5
  • Thank you for your answer! Yes, usually I'd go for the optional argument in \cite, but this work (or, more precisely, part thereof) that I'm citing is relevant to the a whole chapter, but not at a specific place. Since t I'd reuse the same bibliography entry over and over again (e.g. citing a different chapter of some handbook in each chapter of my document), I thought maybe I could avoid having to define a separate entry in the .bib file for each chapter. Your answer certainly helps me in doing that, but if you believe another approach is more appropriate, I'd be glad to hear your advice.
    – steve
    May 26, 2020 at 20:06
  • 1
    @steve If the part of the work you want to reference is a particular chapter of a book, I would look into @inbook 'child' entries. You would have to add a new entry to your .bib file (which you say you don't want), but you could keep duplication of data at a minimum by using crossref = {companion}, in the @inbook. Other than that I would avoid injecting random page numbers into the bibliography unless your readers are aware that you want to point them to something. I don't know what kind of document you are writing. Adding page numbers might be OK and expected in a reading list. ...
    – moewe
    May 27, 2020 at 6:40
  • ... But may go unnoticed in a normal bibliography in a maths paper, say. I'd find it much more useful as a reader to be pointed to relevant sections of a book in a short discussion/mention in the text. But this is all context-, field- and culture-dependent, so without knowing what you are actually doing I don't think I can give a useful suggestion. ...
    – moewe
    May 27, 2020 at 6:44
  • ... And even if I knew what kind of document you have in mind and why you don't want to explicitly cite the work in your document itself (where you could easily give page references), I don't know if I could give you an answer that respects the culture in your field.
    – moewe
    May 27, 2020 at 6:45
  • Thank you so much for your comments! And for pointing me to crossref, which I didn't know about, and will probably be what I end up using.
    – steve
    May 27, 2020 at 22:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.