Tx · LaTeX & TikZ

How to Identify an Entry Key in a BibLaTeX Sourcemap

Published By the 9to5Science editorial team

At a glance

  1. 1. Use the special field

    Set fieldsource to entrykey; it lets Biber test an entry’s citation key.

  2. 2. Match the whole key

    Use an anchored regular expression so similar keys are not matched accidentally.

  3. 3. Map a real field

    Use fieldset and fieldvalue to change a bibliography field when the key matches.

  4. 4. Run Biber

    Compile LaTeX, run Biber, then compile LaTeX again to apply the sourcemap.

To apply a BibLaTeX sourcemap to one entry, use Biber’s special entrykey source. Match the citation key, then set a field on that entry. The key itself is an identifier, not a regular bibliography field, so use entrykey as the source and a field such as keywords as the destination.

The example below marks the entry important2024 with the keyword featured. The bibliography filter then prints entries carrying that keyword.

Match an entry key

In a sourcemap step, fieldsource=entrykey makes the entry’s citation key available for matching. To avoid matching keys that merely contain the target text, anchor the regular expression at both ends:

\step[
  fieldsource=entrykey,
  match=\regexp{\Aimportant2024\z},
  fieldset=keywords,
  fieldvalue={featured}
]

Here, \A and \z mark the beginning and end of the string. The step therefore matches important2024, but not old-important2024 or important2024-notes. The fieldset and fieldvalue options assign featured to the entry’s keywords field.

Complete working example

Save this as a .tex file and build it with LaTeX, Biber, then LaTeX twice more if needed. The filecontents* environment writes the sample .bib file when you compile.

\documentclass{article}

\begin{filecontents*}{refs.bib}
@book{important2024,
  author = {Doe, Jane},
  title  = {A Sample Book},
  date   = {2024},
  publisher = {Example Press}
}
@book{other2024,
  author = {Roe, John},
  title  = {Another Sample Book},
  date   = {2024},
  publisher = {Example Press}
}
\end{filecontents*}

\usepackage[backend=biber]{biblatex}
\addbibresource{refs.bib}

\DeclareSourcemap{
  \maps[datatype=bibtex]{
    \map{
      \step[
        fieldsource=entrykey,
        match=\regexp{\Aimportant2024\z},
        fieldset=keywords,
        fieldvalue={featured}
      ]
    }
  }
}

\begin{document}
\nocite{*}
\printbibliography[keyword=featured]
\end{document}

Because the map adds featured only to important2024, the filtered bibliography contains that entry and not other2024. In this example, the entry has no pre-existing keywords. Assigning fieldvalue replaces the destination field’s value; if the entry already has keywords you need to preserve, use an appropriate append operation and check the resulting keyword list.

When to use \perdatasource instead

A citation key identifies an entry; \perdatasource restricts a map by bibliography data source. If the rule should apply to every entry in one .bib file, select the source file rather than testing each key:

\map{
  \perdatasource{special-entries.bib}
  \step[fieldset=keywords, fieldvalue={featured}]
}

Use both mechanisms when the target must be a particular key in a particular file. The data-source name should correspond to the resource as Biber sees it; if a source name does not match as expected, check the Biber log and the resource path.

What this does not do

A sourcemap can use entrykey to decide whether to alter other data, but it is not a way to rename an entry’s citation key. If you need a different key, change the key in the .bib file and update citations that refer to it. The map above leaves important2024 as the key used by \cite{important2024}.

Common mistakes

  • Using key instead of entrykey: Use the special source name entrykey in the sourcemap step.
  • Matching only part of the key: An unanchored expression may match other keys containing the same text. Use \A and \z for an exact match.
  • Expecting a visual change without printing the mapped field: Adding a keyword does not necessarily change how a standard bibliography entry looks. The example makes the change observable by filtering on that keyword.
  • Skipping Biber: Sourcemaps are processed by Biber. A LaTeX-only rerun will not apply a changed map; run Biber and then LaTeX again.
  • Overwriting existing data unintentionally: Setting fieldvalue assigns the destination field. Consider how existing field contents should be handled before applying the map to real entries.

References

  1. Biber: A Backend for BibLaTeX — User Documentation
  2. The BibLaTeX Package — Package Documentation

Spotted an error? Send a correction. Read how we write and check articles in our editorial policy.