BibLaTeX – Adding Custom Field in BibLaTeX for Bibliographies

biberbiblatexbibliographies

I would like to allow for an optional custom field in my .bib entries. I do not need these fields to be part of the bibliography, but would like to be able to access these via

\citefield{<entry-key>}{<custom-field-name>}

The answer is probably somewhere in the references listed below, but my initial attempts to adapt \DeclareSourcemap and \DeclareFieldFormat failed.

References:

Code:

\begin{filecontents}{mybib.bib}
@book{knuth1984texbook,
  title={The texbook},
  author={Knuth, D.E. and Knuth, A.D. and Bibby, D. and American Mathematical Society and Addison-Wesley Publishing Company and Addison-Wesley},
  isbn={9780201134483},
  lccn={85030845},
  series={Computers \& typesetting},
  url={https://books.google.com/books?id=hEYuAQAAIAAJ},
  year={1984},
  publisher={Addison-Wesley},
  myFieldA={Useful Book},
  myFieldB={on Shelf 4},
}
@book{goossens1994latex,
  title={The LaTeX Companion},
  author={Goossens, M. and Mittelbach, F. and Samarin, A.},
  isbn={9780201541991},
  lccn={lc93023150},
  series={Addison-Wesley series on tools and techniques for computer typesetting},
  url={https://books.google.com/books?id=54A3MuBzIrEC},
  year={1994},
  publisher={Addison-Wesley},
  myFieldA={Also Useful Book},
  myFieldB={on Shelf 5},
}
\end{filecontents}

%% -----------------

\documentclass{article}
\usepackage{biblatex}
\addbibresource{mybib.bib}

\begin{document}
    This should print ``Also Useful Book'':
    \citefield{goossens1994latex}{myFieldA}
\end{document}

Best Answer

Provided you only need a few fields, and are willing to sacrifice the markup semantics just a little bit (for the citations), you can get away with using the "custom fields" biblatex provides (the standard styles don't use them, but if you are using a non-standard one, you might have to check if it is really "vacant").

Given you mentioned in the comments that you'd like a custom name for your field, that can be achieved by using a SourceMap to copy the value from your custom field, to one of the available ones in biblatex's data model. This means you can have your custom name in your .bib file, but when actually making the citation you still have to use a field biblatex "knows".

This could go something like:

\begin{filecontents}[overwrite]{mybib.bib}
@book{knuth1984texbook,
  title={The texbook},
  author={Knuth, D.E. and Knuth, A.D. and Bibby, D. and American Mathematical Society and Addison-Wesley Publishing Company and Addison-Wesley},
  isbn={9780201134483},
  lccn={85030845},
  series={Computers \& typesetting},
  url={https://books.google.com/books?id=hEYuAQAAIAAJ},
  year={1984},
  publisher={Addison-Wesley},
  myFieldA={Useful Book},
  myFieldB={on Shelf 4},
}
@book{goossens1994latex,
  title={The LaTeX Companion},
  author={Goossens, M. and Mittelbach, F. and Samarin, A.},
  isbn={9780201541991},
  lccn={lc93023150},
  series={Addison-Wesley series on tools and techniques for computer typesetting},
  url={https://books.google.com/books?id=54A3MuBzIrEC},
  year={1994},
  publisher={Addison-Wesley},
  myFieldA={Also Useful Book},
  myFieldB={on Shelf 5},
}
\end{filecontents}

%% -----------------

\documentclass{article}
\usepackage{biblatex}
\addbibresource{mybib.bib}

\DeclareSourcemap{
  \maps[datatype=bibtex]{
    \map{
      \step[fieldsource=myFieldA]
      \step[fieldset=usera, origfieldval]
      \step[fieldsource=myFieldB]
      \step[fieldset=userb, origfieldval]
    }
  }
}

\begin{document}
This should print ``Also Useful Book'':
\citefield{goossens1994latex}{usera}

This should print ``on Shelf 5'':
\citefield{goossens1994latex}{userb}
\end{document}

enter image description here

The advantage of this approach is that you don't need to extend the data model, so it is simpler. But if you really need to use your custom field name on both sides then, as far as I can tell, the only way is to extend the data model to include your field. In which case, the canonical answer is the one by @moewe at: How can I create entirely new data types with BibLaTeX/Biber?.

Related Question