1

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}
3
  • 1
    If you don't need many, your job can be much simplified by using "custom" fields biblatex already provides, e.g. user[a-f] (see section 2.2.4 of the manual). If you can live with the default names, just use them, if you need your own names, it would be a matter of making a copy with a sourcemap.
    – gusbrs
    May 5, 2022 at 21:14
  • 1
    @gusbrs I only need a few, but to improve readability I would prefer a custom name if it is not too difficult. May 5, 2022 at 21:24
  • See also tex.stackexchange.com/q/163303/35864.
    – moewe
    May 6, 2022 at 5:49

1 Answer 1

5

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?.

2
  • 1
    Thanks, that seems to work. I don't like the \citefield{goossens1994latex}{usera} in the code, but a simple fix for that is \newcommand*{\myFieldAMappedKey}{usera} and then \citefield{goossens1994latex}{\myFieldAMappedKey}. May 5, 2022 at 21:53
  • 1
    @PeterGrill \citefield is a bit of a kludge anyway. If you intend to cite this particular field more often, you'll probably define a dedicated cite command with \DeclareCiteCommand and can then give the user-level command a more semantic name hiding the usera name in that command definition. See tex.stackexchange.com/q/207591/35864
    – moewe
    May 6, 2022 at 5:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .