TransWikia.com

Retrieve first BibLatex/BibTex/Biber reference automatically

TeX - LaTeX Asked by Britto on April 12, 2021

Suppose I have a .Bib file with an entry like this:

@InBook{lear,
author = {Shakespeare, William},
bookauthor = {Shakespeare, William},
maintitle = {Collected Works},
booktitle = {Tragedies},
title = {King Lear},
volume = {1},
pages = {53-159},
...

Important: it could be any other reference, because I’ll deal with different Bib files with only one reference each one – they will be generated automatically by another program.

I’m using Biblatex inside Overvleaf platform.

My question is:
Is it possible to retrieve the entryfield (that in the example above is the string lear) and use it to be passed as a parameter?

Example:

defmystring{{*var_entrykey*}}

Where var_entrykey content must be lear (refering to the example above) or whatever other entry depending the bib file I’ll be using (remembering that all the Bib files will have only one reference).

The problem is that I don’t know how to read the entrykey
automatically, i.e. usually we need to know the entrykey to
reference it, that’s the normal use and obvious. But in this case, I
need a hack to retrieve it – and put it in a variable – without
knowing it’s content beforehand.

Warm regards

One Answer

The following only works reliably if you cite exactly one entry in your document. Otherwise there is no way of knowing whose entry key you want. This can be ensured by citing only one entry with cite or nocite, in which case you'd have to know the entry key, so the whole thing here would be pointless, or by making sure that your .bib file only contains a single entry.

We can use AtDataInput to hook into the code that reads the entries from the .bbl file. At that point we can save the entry key of the entry that is currently being processed in a variable. If we know there is only going to be one entry in the document, this gives us the entry key we want. (If there were several entries in the bibliography, those calls would overwrite each other and we would end up with the last entry key in our variable. With a wee bit of work we could obtain only the first entry key. Other tricks could be used as well to determine which key to take, but we'd have to have a way to single out one entry.)

documentclass[british]{article}
usepackage[T1]{fontenc}
usepackage{babel}
usepackage{csquotes}

usepackage[backend=biber, style=authoryear]{biblatex}

newcommand*{theoneentrykey}{}

AtDataInput{%
  xdeftheoneentrykey{strfield{entrykey}}}

begin{filecontents}{jobname.bib}
@InBook{lear,
  author     = {Shakespeare, William},
  bookauthor = {Shakespeare, William},
  maintitle  = {Collected Works},
  booktitle  = {Tragedies},
  title      = {King Lear},
  volume     = {1},
  pages      = {53-159},
}
end{filecontents}
addbibresource{jobname.bib}

begin{document}
nocite{*}

Entry key theoneentrykey

texttt{meaningtheoneentrykey}

printbibliography
end{document}

Entry key lear//macro:->lear

Correct answer by moewe on April 12, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP