# How to magically add the file field to each entry in a bib file?

In the context of biblatex its possible to bibstyle=reading, this will produce bibliography including abstract and annotation fields.

As well as file field linking to the file.pdf, the reference. If the bib entry has file set like:

@book{some-book,
author     = {The Author},
title      = {The Book Title},
abstract   = {Bla bla bla},
annotation = {Bla bla bla},
file       = {some-book.pdf}
}


I have file.bib containing many entries, like:

@book{some-book,
author     = {The Author},
title      = {The Book Title},
abstract   = {Bla bla bla},
annotation = {Bla bla bla}
}


Following idea above I should go to each entry and add:

 file       = {some-book.pdf}


That will be a lot of work. Is it possible to automate it some way?

How to add file field to each entry without having to go to each entry and manually add it?

It is easily possible to add arbitrary field contents to entries with Biber's sourcemap features.

With

\documentclass[british]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{babel}
\usepackage{csquotes}

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

\DeclareSourcemap{
\maps[datatype=bibtex,overwrite=false]{
\map{
\step[fieldsource=entrykey, match={.*}, final]
\step[fieldset=file, fieldvalue={\$1.pdf}]
}
}
}

% just to print the 'file' field
\DeclareFieldFormat{file}{file: \path{#1}}
\renewbibmacro{finentry}{\printfield{file}\finentry}

\begin{document}
\cite{sigfridsson,nussbaum,worman,geer}
\printbibliography
\end{document}


You would add to each entry the equivalent of

file = {<entrykey>.pdf},


to all entries that don't already have a file field.

You don't even need RegExes here, you could just say

\DeclareSourcemap{
\maps[datatype=bibtex,overwrite=true]{
\map{
\step[fieldsource=entrykey, final]
\step[fieldset=file, origfieldval, final]
\step[fieldset=file, fieldvalue={.pdf}, append]
}
}
}


which would overwrite an existing file field. The overwrite=true is needed because an append step must formally be able to overwrite a field. You can guard against overwriting a pre-existing file field by adding

      \step[notfield=file, final]


as first step.

But it seems rather pointless to add a field file with the sole contents <entrykey>.pdf to all entries. The information you are adding is a very simple function of information that is already present in the entrykey field, after all.

\documentclass[british]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{babel}
\usepackage{csquotes}

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

\DeclareFieldFormat{fakefile}{file: \path{#1.pdf}}
\renewbibmacro{finentry}{\printfield[fakefile]{entrykey}\finentry}

\begin{document}
\cite{sigfridsson,nussbaum,worman,geer}
\printbibliography
\end{document}


Would produce the same result but without the need to map any field contents.

• Concerning last part of the answer, is the file path provided this manner a link so that I can click and go to that file? – KcFnMi May 5 '19 at 5:17
• @KcFnMi Not in this code, no. \path normally does not link stuff, but instead of \path{XYZ} you could say something like \href{file:XYZ}{\path{XYZ}} and things should be linked (provided you load hyperref). This is independent of the solution you use and would be the same if you just gave file in the .bib file. – moewe May 5 '19 at 5:23
• And should I worry about \Url Error ->\url used in a moving argument? – KcFnMi May 5 '19 at 7:41
• @KcFnMi You should always worry about errors. I get no error from the MWEs though, so I suggest you ask a new question with a short example document that reproduces the error. – moewe May 5 '19 at 7:56
• @KcFnMi In the RegEx the overwrite=false avoids overwriting an existing file field. (The second \step is not allowed to overwrite an existing file field.) The non-RegEx-solution needs overwrite=true to work properly, because the third \step with append formally needs to be able to overwrite the file field. In consequence that means that this solution overwrites an existing file field. – moewe May 5 '19 at 9:42