I am trying to create a custom field in biblatex of type key

\DeclareDatamodelFields[type=field, datatype=key]{myfield} 

Based on my understanding of the documentation

Key fields May hold printable data or localisation keys. A test is performed to determine whether the value of the field is a known localisation key (the localisation keys defined by default are listed in §4.9.2). If so, the localised string is printed. If not, the value is printed as is. A typical example is the type field.

if myfield has a value of editortr and the language is english, I should get editor and translator (or maybe ed. and trans.). But that is not what I see. What am I missing?

\documentclass[english]{article}

\begin{filecontents}{\jobname.bib}
@article{foo,
  myfield = {editortr}
}
\end{filecontents}

\begin{filecontents}{\jobname.dbx}
  \DeclareDatamodelFields[type=field, datatype=key]{myfield}
\end{filecontents}

\usepackage{xpatch}
\usepackage{babel}
\usepackage[datamodel=\jobname]{biblatex}
\addbibresource{\jobname.bib}

\xpatchbibdriver{article}{%
  \printlist{language}%
}{%
  \printlist{language}%
  \printfield{myfield}%
}{}{}

\begin{document}
\nocite{*}
\printbibliography
\end{document}

The log tells me it found english.lbx

Package biblatex Info: Trying to load language 'english'... Package biblatex Info: ... file 'english.lbx' found.

  • I'm not really sure here and don't have much time to look better at the moment. But take a look at biblatex.def, where you'll find, eg. \DeclareFieldFormat{type}{\ifbibstring{#1}{\bibstring{#1}}{#1}} or the bibmacros authorstrg/editorstrg. You'll notice the test is done there, and not by some inner mechanisms. And ifbibstring (or variants) is used for the purpose. – gusbrs Nov 8 at 20:20
  • 1
    Tested: \DeclareFieldFormat{myfield}{\ifbibstring{#1}{\bibstring{#1}}{#1}} gets the job done. – gusbrs Nov 8 at 20:31
  • @gusbrs but then why use key and not literal. The documentation makes it sound like it should work out of the box. – StrongBad Nov 8 at 20:46
  • Good question, perhaps a useful standard? (I'm guessing here). Anyway, in biblatex.def you see how the "effect" is actually implemented, as far as I get. – gusbrs Nov 8 at 22:54
up vote 2 down vote accepted

The quoted passage from the documentation is slightly misleading. It suggests that the backend (BibTeX/Biber) performs the test to determine if the given value is a localisation string and it also suggests that this happens automatically. Both of these are not true

  1. Your average key field is handled exactly like a literal field by Biber and BibTeX. Indeed the data output in the .bbl is exactly the same for literal and key datatypes. (I remember that Biber would remap langid fields from BCP 47 to babel identifiers and and I can still see part of that in the Biber source, but I failed to produce an example where that worked as I expected... Anyway, that is hard-coded behaviour for one field.)

  2. The difference between literal and key fields or list only manifests itself when their format is declared on the biblatex end. Literal types are just printed as-is, whereas key types usually have declarations like

    \DeclareFieldFormat{type}{\ifbibstring{#1}{\bibstring{#1}}{#1}}
    

    Which implements the behaviour from the quoted passage, but on the biblatex side. This has to be done explicitly in the field format. It does not happen automatically even if the field was declared to be of datatype key.

That means that you need

\DeclareFieldFormat{myfield}{\ifbibstring{#1}{\bibstring{#1}}{#1}}

in your example.

I have opened a ticket at https://github.com/plk/biblatex/issues/839 to discuss changes to the documentation, so that the behaviour is clearly documented in the future. Suggestions are very welcome.

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.