Some BibLaTeX Tricks to remember

For creating beautiful PDFs and slides, I am a big believer of using LaTeX whenever possible. Sometimes one has to use Word, but for everything else LaTeX!

For writing documents, that is another matter.

When working with citations, LaTeX is bundled with a powerful tool called BibTeX that supports structured representation of bibliographic entries in a custom format that allows one to easily change citation style and reference style.

Now, BibTeX is rather outdated and the community has produced a tidy replacement called BibLaTeX that uses biber as a backend rather than BibTeX. BibLaTeX is the swiss-army knife of bibliographic control in your document. I won’t go into too much detail about all the things that BibLaTeX can do, but highlight some new tricks I have learned recently.

Capitalisation of Dutch Surnames are Context Dependent.

My surname is partly Dutch and has a prefix. The rules for Dutch surnames are:

  1. Prefixes are ignored when sorting.
  2. Prefixes are capitalised when just the surname is presented, lower case otherwise.
  3. Surnames can contain spaces.

The rules for most Flemish surnames are:

  1. Prefixes are not ignored when sorting;
  2. Prefixes are always capitalsied, unless the surname is of Dutch origin;
  3. Surnames can contain spaces;

It would be great if our citation styles and engines can do this automatically when using author-year citations. Unfortunately, the citation and references styles do not account for all of this automatically but BibLaTeX is prefix aware. Thus:

\usepackage[
    ...
    useprefix=true
  ]{biblatex}

Will enable BibLaTeX to consider the prefix when: printing, sorting, generating, and formatting things. The example given in the documentation (for Ludwig van Beethoven) is:

  • When true: Cited as van Beethoven; alphabetised as Van Beethoven, Ludwig
  • When false: Cited as Beethoven; alphabetised as Beethoven, Ludwig van

More so there are commands \Cite{}, where the first letter is capitalised, that will capitalise the first prefix of the first name in the first entry of the citation.

Borrowing from an existing source we can get a good approximation for handling Dutch-style surnames in our documents.

We first have to load BibLaTex as:

\usepackage[
    ...
    useprefix=false
  ]{biblatex}

Sorting of entries is enabled at load time, so we want to ignore prefixes then.

To stop forced capitalisation by BibLaTex we do:

\renewbibmacro{begentry}{\midsentence}

We then enable useprefix and ensure that names are always capitalised within the document body:

\makeatletter
\AtBeginDocument{%
  \toggletrue{blx@useprefix}
  \renewcommand*{\mkbibnameprefix}[1]{\MakeCapital{#1}}
}
\makeatother

We then disable useprefix and reset how names are capitalised for the reference list.

\makeatletter
\AtBeginBibliography{%
  \togglefalse{blx@useprefix}
  \renewcommand*{\mkbibnameprefix}[1]{#1}
}
\makeatother

The idea being that with author-year citations we will get the correct capitalistion when citing and in the references and it be ‘context’ dependent.

The downside is that if we were to use \fullcite{} then the surnames will be incorrectly capitalised. So we can provide some toggles to set and reset how names are capitalised.

\newcommand{\BibSetPrefixUpper}
{
\renewcommand*{\mkbibnameprefix}[1]{\MakeCapital{##1}}
}
\newcommand{\BibSetPrefixLower}
{
\renewcommand*{\mkbibnameprefix}[1]{##1}
}

Alternatively we could redefine how \fullcite{} works to ensure that names are correctly capitalised.

Hyper Full Cites.

Speaking of FullCite when providing references in line, and one is not using \printbibliography DOIs and URLs take up space. Borrowing from an existing source we can make those fullcites clickable.

\DeclareCiteCommand{\fullciteHyper}
  {\usebibmacro{prenote}}
  {\iffieldundef{doi}
    {\iffieldundef{url}
      {\usedriver
       {\DeclareNameAlias{sortname}{default}}
       {\thefield{entrytype}}}
      {\href{\thefield{url}}
        {\usedriver
          {\DeclareNameAlias{sortname}{default}}
          {\thefield{entrytype}}}}}
    {\href{http://dx.doi.org/\thefield{doi}}{\usedriver
     {\DeclareNameAlias{sortname}{default}}
     {\thefield{entrytype}}}}}
  {\multicitedelim}
  {\usebibmacro{postnote}}

Bold names

When writing CVs or application documents, it is a good idea to highlight where you are in the author list. Borrowing from an existing source we can do this easily. Previous iterations of this required you to know the Hash of your name (it is how Biber works underneath), but this way is much cleaner.

It works by using a command to track which names are to be bold, lists the names to an external file and highlights them by referencing the file. We provide commands to set and unset the names to be highlighted.

First we create the file to store hashes, and count their occurrence.

\makeatletter
\def\nhblx@bibfile@name{\jobname -nhblx.bib}
\newwrite\nhblx@bibfile
\immediate\openout\nhblx@bibfile=\nhblx@bibfile@name

\immediate\write\nhblx@bibfile{%
  @comment{Auto-generated file}\blx@nl}

\newcounter{nhblx@name}
\setcounter{nhblx@name}{0}

\newcommand*{\nhblx@writenametobib}[1]{%
  \stepcounter{nhblx@name}%
  \edef\nhblx@tmp@nocite{%
    \noexpand\AfterPreamble{%
      \noexpand\setbox0\noexpand\vbox{%
        \noexpand\nhblx@getmethehash{nhblx@name@\the\value{nhblx@name}}}}%
  }%
  \nhblx@tmp@nocite
  \immediate\write\nhblx@bibfile{%
    @misc{nhblx@name@\the\value{nhblx@name}, author = {\unexpanded{#1}}, %
      options = {dataonly=true},}%
  }%
}

\AtEndDocument{%
  \closeout\nhblx@bibfile}

Add the file to the document for processing in the next pass:

\addbibresource{\nhblx@bibfile@name}

We then provide internal commands to store and capture the hashes.

\newcommand*{\nhblx@boldhashes}{}
\DeclareNameFormat{nhblx@hashextract}{%
  \xifinlist{\thefield{hash}}{\nhblx@boldhashes}
    {}
    {\listxadd{\nhblx@boldhashes}{\thefield{hash}}}}

\DeclareCiteCommand{\nhblx@getmethehash}
  {}
  {\printnames[nhblx@hashextract][1-999]{author}}
  {}
  {}

We now provide core commands to set and reset the tracked names:

\newcommand*{\addboldnames}{\forcsvlist\nhblx@writenametobib}
\newcommand*{\resetboldnames}{\def\nhblx@boldhashes{}}

They are used as follows:

\addboldnames{{Sigfridsson, Emma},{Vizedom, Monika B.}}
\resetboldnames{}

Notice you can provide a list of names!

We now provide the infrastructure to embolden the names in the document,

\newcommand*{\ifhashinboldlist}{%
  \xifinlist{\thefield{hash}}{\nhblx@boldhashes}}
\makeatother

\newcommand*{\mkboldifhashinlist}[1]{%
  \ifhashinboldlist
    {\mkbibbold{#1}}
    {#1}}

\DeclareNameWrapperFormat{boldifhashinlist}{%
  \renewcommand*{\mkbibcompletename}{\mkboldifhashinlist}%
  #1}

\DeclareNameWrapperAlias{sortname}{default}
\DeclareNameWrapperAlias{default}{boldifhashinlist}

Filter Bibliography by Name

We can even use the previous tip to support filtering by name.

See https://tex.stackexchange.com/a/560399 for more.