# Extract entry keys from bib file and create a file for each entry key

In the context of ideas mentioned at How to embed a review in biblatex? one should create a file for each entry key in the bib file. These files will be named to refer to each entry key, names will be like

bibannotation-entrykey1.tex
bibannotation-entrykey2.tex
bibannotation-entrykey3.tex


If bib file contains many entries might be interesting to automate it. How? I checked biber --help but doesn't seem to be something for this purpose there.

• What do you want to automate? The creation of those files? Since the contents of those files have to be created manually anyway and biblatex automatically skips over non-existing files, I don't really see a point in creating the files automatically. – moewe May 4 '19 at 9:28
• I was thinking in automate creation of files but after some more thinking I'm realizing this might not be a good thing, as you pointed. – KcFnMi May 5 '19 at 5:22

Biber certainly does not create these files for you automatically. Your best bet is probably a different programming language like Python or Perl with a library that can read .bib files (https://github.com/sciunto-org/python-bibtexparser, https://pypi.org/project/biblib/, https://metacpan.org/pod/Text::BibTeX, ...).

For example the following Perl script

#!/usr/bin/perl

use strict;
use warnings;
use Text::BibTeX;

my ($bibfilename) = @ARGV; unless (defined$bibfilename) {
die "Please provide a file name";
}

if (-f $bibfilename) { print "Processing${bibfilename}...\n";
} else {
die "File ${bibfilename} not found"; } my$bibfile = Text::BibTeX::File->new($bibfilename); my$entry;

while ($entry = Text::BibTeX::Entry->new($bibfile))
{
next unless $entry->parse_ok; next unless$entry->metatype eq BTE_REGULAR;
my $key =$entry->key;
my $file = "bibannotation-${key}.tex";
unless (-e $file) { open TMPFILE, '>',$file and close TMPFILE
or die "File error with $file:$!";
}
}


when saved as bibannotations.pl and run as

perl bibannotations.pl <mybibfile.bib>


will produce empty files of the form bibannotation-<entrykey>.tex for all entries in the .bib file <mybibfile.bib>.

Technically, you don't even need a different programming language, you can have TeX sort this out. Just load all relevant .bib files in the following file and compile it with LaTeX, Biber, LaTeX.

\documentclass{article}
\usepackage[style=authoryear, backend=biber]{biblatex}

\nocite{*}

\newwrite\filehandle
\AtDataInput{%
\IfFileExists{bibannotation-\thefield{entrykey}.tex}
{}
{\immediate\openout\filehandle=bibannotation-\thefield{entrykey}.tex
\immediate\closeout\filehandle}%
}

\usepackage{filecontents}
\begin{filecontents}{\jobname.bib}
@book{appleby,
author  = {Humphrey Appleby},
title   = {On the Importance of the Civil Service},
date    = {1980},
}
\end{filecontents}


I believe, however, that there is little point in creating these files automatically. After all, you'll have to provide the contents of those files manually anyway. At least for the purposes of the loadfiles option discussed in How to embed a review in biblatex? biblatex will just skip over non-existing files meaning that there is no point in having an empty file present.
• @KcFnMi Yes, but as the second example shows you don't even need Lua. LaTeX is enough (as long as your entrykeys are well-behaved and don't contain any characters weird for TeX or for your file system: If you stick to ASCII only and avoid /, , . and : you are probably fine.) – moewe May 6 '19 at 10:16