0

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.

1

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}


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

\addentrysubtyperesource{\jobname-one}
\addentrysubtyperesource{\jobname-two}


\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}

Appleby, Humphrey (1980). On the Importance of the Civil Service. file: ref/kcfnmifilenames-one/appleby.pdf.//Sigfridsson, Emma (1998). ‘Chemistry’. In: Journal of Chemisty 48.2, pp. 135–164. file: ref/kcfnmifilenames-two/sigfridsson.pdf.

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.