# Iterate over all the entries in a bib file

I have a big bib file that contains a lot of entries such that most of them contain a note filed. I would like to automatically iterate over all the entries and for each entry insert into the document something like:

Paper's title / First author (et al. if needed) [x]
Note:
The content of the note field.


where [x] is the entry's number in the list of references that is printed.

If I know the entry's key, then using biblatex this could be something like:

\newcommand{\completecite}[1]{
\citetitle{#1}, \citeauthor{#1} \cite{#1}

\citefield{#1}{note}
}


My problem is how to iterate over all the entries in the bib file?

Probably this can be solved using a combination of lua and biblatex. But my question is whether there's a simple and canonical way to do it? And otherwise, I would be happy to get a hint how to tackle this using lua.

In short, the ultimate goal is to keep track of all the papers and manually added notes in a single bib file and generate a pdf that contains the notes for each entry.

• You could write your own bib driver in biblatex and use \nocite, but for what you want the relatively recent package citeall seems very relevant. – moewe Sep 18 '15 at 7:46
• If I understand correctly biblatex's driver kicks in only when there is a cite command. Isn't it so? I cannot opne the documentation of citeall package. – Dror Sep 18 '15 at 7:50
• If you use \nocite{*} all available .bib entries are "cited" in that they appear in the bibliography. I can open the documentation of the citeall pacakge. – moewe Sep 18 '15 at 7:56

We can effectively use a \printbibliography if we do some black magic.

First we define an universal driver

\newbibmacro*{cite:title}{%
\printtext[bibhyperref]{%
\printfield[citetitle]{labeltitle}}}

\newbibmacro*{cite:shorthand}{%
\printtext[bibhyperref]{\printfield{shorthand}}}

\newbibmacro*{citetitle}{%
\iffieldundef{shorthand}
{\usebibmacro{cite:title}}%
{\usebibmacro{cite:shorthand}}}

\DeclareBibliographyDriver{drorver}{%
\usebibmacro{citetitle}%
\printfield{year}%
\printtext[brackets]{\usebibmacro{cite}}%
\par
\printfield{note}%
}


and a new list format

\defbibenvironment{drorlist}
{\list
{}
{\setlength{\leftmargin}{\bibhang}%
\setlength{\itemindent}{-\leftmargin}%
\setlength{\itemsep}{\bibitemsep}%
\setlength{\parsep}{\bibparsep}}}
{\endlist}
{\item}


And then the magic

\makeatletter
\newcommand{\makealldror}{\def\blx@driver##1{\csuse{blx@bbx@drorver}}}
\makeatother


this makes all entries use the drorver type. The macro blx@driver is used to call the relevant driver for the correct entry type (\blx@bbx@<type>) we make that macro choose \blx@bbx@drorver at all times. (See biblatex2.sty, line 1665 for the real definition of \blx@driver)

And then we use

\begingroup
\makealldror
\printbibliography[env=drorlist]
\endgroup


to typeset a list of the form you want.

MWE

\documentclass{article}
\usepackage[backend=biber,style=numeric]{biblatex}
\usepackage{citeall}
\usepackage{filecontents}
\begin{filecontents}{\jobname.bib}
@article{batty2007iq,
title={IQ in early adulthood and later cancer risk: cohort study of one million Swedish men},
author={Batty, G. David and Wennerstad, Karin Modig and Smith, George Davey and Gunnell, David and Deary, Ian J. and Tynelius, Per and Rasmussen, Finn},
journal={Annals of Oncology},
volume={18},
number={1},
pages={21--28},
year={2007},
publisher={European Society for Medical Oncology},
note = {ipsum ipsum ipsum ipsum},
}

@article{batty2007premorbid,
title={Premorbid (early life) IQ and Later Mortality Risk: Systematic Review},
author={Batty, G. David and Deary, Ian J. and Gottfredson, Linda S.},
journal={Annals of Epidemiology},
volume={17},
number={4},
pages={278--288},
year={2007},
publisher={Elsevier},
note = {Lorem lorem lorem lorem lorem lorem},
}

@article{batty2009iq_early,
title={IQ in Early Adulthood and Mortality By Middle Age: Cohort Study of 1 Million Swedish Men},
author={Batty, G. David and Wennerstad, Karin Modig and Smith, George Davey and Gunnell, David and Deary, Ian J. and Tynelius, Per and Rasmussen, Finn},
journal={Epidemiology},
volume={20},
number={1},
pages={100--109},
year={2009},
publisher={LWW},
note = {In Mexico is een man gearresteerd.},
}

@article{batty2009iq_socio,
title={IQ in Early Adulthood, Socioeconomic Position, and Unintentional Injury Mortality by Middle Age: A Cohort Study of More Than 1 Million Swedish Men},
author={Batty, G. David and Gale, Catharine R. and Tynelius, Per and Deary, Ian J. and Rasmussen, Finn},
journal={American Journal of Epidemiology},
volume={169},
number={5},
pages={606--615},
year={2009},
publisher={Oxford University Press},
note = {Lore ipsum dolor sit amet \begin{itemize}\item Lorem\end{itemize}},
}
\end{filecontents}

\newbibmacro*{cite:title}{%
\printtext[bibhyperref]{%
\printfield[citetitle]{labeltitle}}}

\newbibmacro*{cite:shorthand}{%
\printtext[bibhyperref]{\printfield{shorthand}}}

\newbibmacro*{citetitle}{%
\iffieldundef{shorthand}
{\usebibmacro{cite:title}}%
{\usebibmacro{cite:shorthand}}}

\DeclareBibliographyDriver{drorver}{%
\usebibmacro{citetitle}%
\printfield{year}%
\printtext[brackets]{\usebibmacro{cite}}%
\par
\printfield{note}%
}

\defbibenvironment{drorlist}
{\list
{}
{\setlength{\leftmargin}{\bibhang}%
\setlength{\itemindent}{-\leftmargin}%
\setlength{\itemsep}{\bibitemsep}%
\setlength{\parsep}{\bibparsep}}}
{\endlist}
{\item}

\makeatletter
\newcommand{\makealldror}{\def\blx@driver##1{\csuse{blx@bbx@drorver}}}
\makeatother

\begin{document}
\nocite{*}

\begingroup
\makealldror
\printbibliography[env=drorlist]
\endgroup

\printbibliography
\end{document}


We can use the citeall package for this task.

First we need to define a nice cite macro for the job. It is mostly cobbled together from the standard .cbx files.

\newbibmacro*{cite:title}{%
\printtext[bibhyperref]{%
\printfield[citetitle]{labeltitle}}}

\newbibmacro*{cite:shorthand}{%
\printtext[bibhyperref]{\printfield{shorthand}}}

\newbibmacro*{citetitle}{%
\iffieldundef{shorthand}
{\usebibmacro{cite:title}}%
{\usebibmacro{cite:shorthand}}}

\newbibmacro*{drorcite}{%
\usebibmacro{citetitle}%
\printfield{year}%
\printtext[brackets]{\usebibmacro{cite}}%
\par
\printfield{note}%
}

\DeclareCiteCommand{\drorcite}
{\usebibmacro{prenote}}
{\usebibmacro{drorcite}}
{\multicitedelim}
{\usebibmacro{postnote}}


and then just

\citeall[\drorcite]


and all entries in the .bib file are cited with \drorcite.

MWE

\documentclass{article}
\usepackage[backend=biber,style=numeric]{biblatex}
\usepackage{citeall}
\usepackage{filecontents}
\begin{filecontents}{\jobname.bib}
@article{batty2007iq,
title={IQ in early adulthood and later cancer risk: cohort study of one million Swedish men},
author={Batty, G. David and Wennerstad, Karin Modig and Smith, George Davey and Gunnell, David and Deary, Ian J. and Tynelius, Per and Rasmussen, Finn},
journal={Annals of Oncology},
volume={18},
number={1},
pages={21--28},
year={2007},
publisher={European Society for Medical Oncology},
note = {ipsum ipsum ipsum ipsum},
}

@article{batty2007premorbid,
title={Premorbid (early life) IQ and Later Mortality Risk: Systematic Review},
author={Batty, G. David and Deary, Ian J. and Gottfredson, Linda S.},
journal={Annals of Epidemiology},
volume={17},
number={4},
pages={278--288},
year={2007},
publisher={Elsevier},
note = {Lorem lorem lorem lorem lorem lorem},
}

@article{batty2009iq_early,
title={IQ in Early Adulthood and Mortality By Middle Age: Cohort Study of 1 Million Swedish Men},
author={Batty, G. David and Wennerstad, Karin Modig and Smith, George Davey and Gunnell, David and Deary, Ian J. and Tynelius, Per and Rasmussen, Finn},
journal={Epidemiology},
volume={20},
number={1},
pages={100--109},
year={2009},
publisher={LWW},
note = {In Mexico is een man gearresteerd.},
}

@article{batty2009iq_socio,
title={IQ in Early Adulthood, Socioeconomic Position, and Unintentional Injury Mortality by Middle Age: A Cohort Study of More Than 1 Million Swedish Men},
author={Batty, G. David and Gale, Catharine R. and Tynelius, Per and Deary, Ian J. and Rasmussen, Finn},
journal={American Journal of Epidemiology},
volume={169},
number={5},
pages={606--615},
year={2009},
publisher={Oxford University Press},
note = {Lore ipsum dolor sit amet},
}
\end{filecontents}

\newbibmacro*{cite:title}{%
\printtext[bibhyperref]{%
\printfield[citetitle]{labeltitle}}}

\newbibmacro*{cite:shorthand}{%
\printtext[bibhyperref]{\printfield{shorthand}}}

\newbibmacro*{citetitle}{%
\iffieldundef{shorthand}
{\usebibmacro{cite:title}}%
{\usebibmacro{cite:shorthand}}}

\newbibmacro*{drorcite}{%
\usebibmacro{citetitle}%
\printfield{year}%
\printtext[brackets]{\usebibmacro{cite}}%
\par
\printfield{note}%
}

\DeclareCiteCommand{\drorcite}
{\usebibmacro{prenote}}
{\usebibmacro{drorcite}}
{\multicitedelim}
{\usebibmacro{postnote}}

\begin{document}
\citeall[\drorcite]
\end{document}


• What would be the limitations of the formatting that can be used inside the note fields? Seems like itemize break this approach... – Dror Sep 18 '15 at 8:31
• @Dror That is probably a limitation imposed by how citeall works. If you can live with a document where you can't have a normal bibliography, I could cook something up. – moewe Sep 18 '15 at 8:36
• It seems like all I need is to iterate over the keys and pass them to the command I have in the OP. I actually consider using python for that, but would prefer a self-contained TeXy solution. – Dror Sep 18 '15 at 8:41
• @Dror Try the edit, please. – moewe Sep 18 '15 at 8:53
• Magic indeed! Two more requests: 1) Can you add some comments to the magic so I could maintain it and reuse it? 2) How can I avoid the rendering of "References" twice? – Dror Sep 18 '15 at 9:00

You can use citeall. But if you have entries without note field, you will probably have to define a better \completecite command. Also be aware that citeall was written to test and debug new cite styles and not for "document loops".

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{biblatex}
\usepackage{citeall}
\newcommand{\completecite}[1]{%
\citetitle{#1}, \citeauthor{#1} \cite{#1}
\par
\citefield{#1}{note}\par
}
\begin{document}
\citeall[\completecite]
\end{document}

• +1 Of course. But my toenails curl up when I see definitions such as \completecite here. – moewe Sep 18 '15 at 8:20
• @moewe: For a fast test I would something like \completecite too. – Ulrike Fischer Sep 18 '15 at 8:22
• For fast tests that would be fine. But if you want to have a proper citation command, \DeclareCiteCommand is definitely the right way to go. – moewe Sep 18 '15 at 8:24
• This seems to be a good start. However, having itemize in the note field breaks this approach, while using the command in my OP handles the list properly. Any workaround? – Dror Sep 18 '15 at 8:29