1

tl;dr: How can I -- in a .tex file I'm writing -- add/edit the shorthand for an included bibitem which is defined in a (separate) .bib file?


Motivation

I have a large, central .bib file which contains lots of works by different authors. I use this as a bibresource in my .tex files with a fairly standard bibliography and citing style and that's normally fine. I use biblatex.

I'm a student and occasionally I write works where I cite many works, predominantly from one (normally famous) author, and hand them in to an expert on that author. In these cases, it's common practice in the field to not use the long-form cite style but to use a two or three character shorthand. Many of these works I might want to cite in other writing using the standard long-form style.

Biblatex can handle this with the shorthand field. So far in these situations I've had to adjust the bib file just for this one essay, compile, hand in, then revert the bib file again. This is not ideal. I can't set the shorthand once and leave it, because then it would be used in all of my writing including that where it isn't appropriate.

So I want to be able to set the shorthand for a given bibitem (from the included bib resource file) on a per-tex-file basis.

Notes on usage

I really value being able to use one citation command. I can just \autocite everything and forget about it, the formatting will be taken care of for me. So even if there is a verbose ('ignore shorthand') \cite command that I could use in some writing to avoid the shorthands, I would rather not use it if possible.

I like the idea of using the shorthand functionality rather than a hack or other trick which produces similar behaviour. In a larger paper I could use this to print a list of abbreviations, and I can include them programatically in the bibliography. This is functionality worth keeping.

So, here's an example of the sort of thing I want to be able to do:

...
\addbibresource{~/path/to/bib/file.bib}

\setbibitemvalue{
  shorthand=AB
}{someBibKey101}

...
2
2

Here's the solution I came up with.

Thanks to Ivan for pointing me to this question, which took me to section 4.5.3 of the biblatex documentation: 'Dynamic Modification of Data'. This answer is very specific to my use case, but that section includes everything one might need to expand it to other, similar cases.

\usepackages{biblatex}
\usepackage{pgfkeys}

\pgfkeys{/shorthands/.unknown/.code=
    \DeclareSourcemap{
      \maps[datatype=bibtex,overwrite=true]{
        \map{
          \step[fieldsource=entrykey,
              match=\regexp^\pgfkeyscurrentname$,
            fieldset=shorthand,
            fieldvalue={#1}
            ]
        }
      }
    }
}

\newcommand{\shorthands}[1]{\pgfkeys{/shorthands/.cd,#1}}

This defines a command \shorthands which takes a list of key=value pairs. For each one, it passes the key to pgfkeys, which realises that the key is undefined (I don't define any keys at all, so all keys will be undefined). pgfkeys then runs the DeclareSourcemap code, where \pgfkeyscurrentname is the key and #1 is the corresponding value. The code will set the shorthand field to the desired value, if it didn't have a value already.

To use it, just pass a list of bibKey=shorthand to \shorthands:

\shorthands{
  Wittgenstein-PhilInv=PI,
  Wittgenstein-Trac=TLP
}

I'm sure my solution could be improved. Perhaps it would be possible to put all the different shorthands inside a single DeclareSourcemap, which would probably save on compilation time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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