# How do I get the name of the bib file to set as the vale of a bib field?

Slightly changing topic from How to magically add the file field to each entry in a bib file?.

I have the following:

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


And in the bib:

@string { subtype = "thesubtype" }


Name of bib file is thesubtype.bib.

Instead of (manually) setting the value of subtype inside the bib I would like to set it programmatically on the DeclareSourcemap. Which might be a good approach to that?

I guess one approach might be to through in some python. And I heard lualatex brings lua language, curious if that can be used here.

The name of the .bib file is not accessible for use in source maps or other operations. But the name is known at least at a very early stage when Biber processes \perdatasource (I presume it is forgotten later on). If you think access to the file name would be a useful feature for the general public, please specify what exactly you want (Do you want the file name of an entry to basically act as a normal .bib field? Do you want to be able to refer to the file name just in a source map?) and open a feature request at https://github.com/plk/biber/issues.

In the meantime, here is a work-around that uses \perdatasource and encompasses \addbibresource to keep redundancy at a minimum. The solution exploits that \perdatasource allows us to get a glimpse of the .bib file name. Since you need one such \perdatasource map for every .bib file in the document, so it seems natural to combine the source map with \addbibresource directly.

Note that \addentrysubtyperesource takes the .bib file name without the file extension (unlike \addbibresource which needs the extension).

Note further that there are several source maps in the example and their order matters. The \addentrysubtyperesource calls should come before any other source map constructs.

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

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

\DeclareSourcemap{
\maps[datatype=bibtex,overwrite=true]{
\map{
\perdatasource{#1.bib}
\step[fieldset=entrysubtype, fieldvalue={#1}]
}
}
}
}

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

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

\usepackage{filecontents}
\begin{filecontents}{\jobname-one.bib}
@book{appleby,
author  = {Humphrey Appleby},
title   = {On the Importance of the Civil Service},
date    = {1980},
}
\end{filecontents}
\begin{filecontents}{\jobname-two.bib}
@article{sigfridsson,
author  = {Emma Sigfridsson},
title   = {Chemistry},
date    = {1998},
journal = {Journal of Chemisty},
volume  = {48},
number  = {2},
pages   = {135-164},
}
\end{filecontents}

\begin{document}
\cite{sigfridsson,appleby}
\printbibliography
\end{document}