Biblatex – How to Check if an Entry is Cited Multiple Times

biblatexcitingconditionalscounters

I am trying to find out whether it is currently possible to have a test in Biblatex that tests at the very first time an entry is cited whether the same entry will be cited more than once in the current reference section (i.e. the entire paper, or the chapter in case of a book).

The real world usage scenario is legal citations, namely the Canadian Guide to Uniform Legal Citation, which, while slightly ambiguous, states that "it is not necessary to establish a short form for cases if they are not referred to in any subsequent paragraph" (in practice, most papers I see will only give the short form if the case is cited subsequently, which makes sense as otherwise, given the abundance of cases cited just once in a usual paper, any way to make the footnote shorter is welcome).

So let's say I cite Elders Pastoral Ltd v Bank of New Zealand. The first footnote would then say "Elders Pastoral Ltd v Bank of New Zealand [1989] 2 N.Z.L.R. 180 (C.A.) [Elders]", all subsequent footnotes would say "Elders, supra n. 1". However, I need the printing of the shorthand field to be conditional on the entry to be cited more than once.

I have a vague idea that might be possible by doing something along the following lines.

Here is my current macro to make a footnote for a case:

\newbibmacro*{footcite:jurisdiction}{%
  \global\boolfalse{cbx:loccit}%
  \ifciteseen
    {\ifthenelse{\ifciteibid\AND\NOT\iffirstonpage}
      {\usebibmacro{footcite:ibid}}
      {\usebibmacro{footcite:jurisdiction:note}}}
    {\usebibmacro{footcite:full:jurisdiction}%

And here is the footcite:full:jurisdiction macro

\newbibmacro*{footcite:full:jurisdiction}{%
   \printtext{%
   \bibhypertarget{\thefield{entrykey}:\the\value{instcount}}{%
      \usedriver
        {\DeclareNameAlias{sortname}{default}}
        {jurisdiction}}}%
   \usebibmacro{shorthandintro:jurisdiction}}
   \usebibmacro{footcite:save}}}

So in the footcite:full:jurisdiction, I would make the \usebibmacro{shorthandintro:jurisdiction} dependent on the test for a variable and in the footcite:jurisdiction, I would set the variable in case the \ifciteseen yields true. This would then work after two LaTeX runs.

However, my question would be what type of variable to use and how to to set it, so as to avoid errors (for instance, I think using a boolean and do an \ifbooltrue test would result in errors like "boolean variable cbx:???? not set" errors on the first run).

Any ideas are very welcome.

Best Answer

As per @Seamus suggestion, I edited my original answer, rather than add a new one; my initial answer was not entirely correct, and in case anyone ever has the same problem, my experience might be helpful. While my initial solution would work, it would also give me errors I did not notice initially because TextMate seemed to suppress them. Only when I compiled in the terminal would I see them. So here is how I changed and simplified it.

First, I begin with the footcite:jurisdiction macro:

\newbibmacro*{footcite:jurisdiction}{%
  \global\boolfalse{cbx:loccit}%
  \ifciteseen%
    {\ifthenelse{\ifciteibid\AND\NOT\iffirstonpage}%
          {\usebibmacro{footcite:ibid}}%
          {\usebibmacro{footcite:jurisdiction:note}}}%
    {\usebibmacro{footcite:full:jurisdiction}%
     \usebibmacro{footcite:save}}}

Nothing spectacular here, it is just a variation of the regular footcite macro. Notice that it calls the footcite:jurisdiction:note macro when the case is cited at least once not for the first time and not with ibid (which is exactly the scenario in which I want the shorthand to be printed in the first citation). The footcite:jurisdiction:note macro goes like this:

\newbibmacro*{footcite:jurisdiction:note}{%
 \usebibmacro{jurisdiction:multipletimes}%
 \ifbool{bbx:jurisdiction:germany}%
   {\usebibmacro{court+location:germany}% Case id for German cases
    \setunit{\addspace}%
    \iffieldundef{origtitle}%
      {\printfield{title}}%
      {\printfield{origtitle}}}
   {\iffieldundef{shorthand}% Case id for English-style cases
      {\usebibmacro{casename}}
      {\printfield{shorthand}}}%
 \usebibmacro{seenote}%
 \iffieldundef{postnote}%
  {}%
  {\setunit*{\addcomma\space}}%
}

The jurisdiction:multipletimes macro sets the variable which will be checked to determine whether the shorthand needs to be printed. Because the macro is only called at the second citation at the earliest, but the shorthand needs to be printed at the first citation, it writes to the aux file. The macro goes like this:

\newbibmacro{jurisdiction:multipletimes}{%
  \iffieldundef{shorthand}%
     {}%
     {\if@filesw
       \protected@write\@mainaux{}%
         {\string\csxdef{cbx@\thefield{entrykey}:short:%
          \csuse{cbx@first@\thefield{entrykey}}}{multi}}%
      \fi
      }%
}%

To my understanding, the \if@fileswchecks whether the aux file is writeable which seemed like a good idea to check for. The \protected@write\@mainaux{} writes to the main aux file. The \string writes the following command verbatim instead of expanding it before being written to the aux file. The \csxdef sets the variable that is later used to check whether the entry has been cited multiple times. The next cbx@\thefield{entrykey} is not preceded by a \string because we want itto expand before being written to the aux file to make a unique variable name for each entry. The name of the variable also contains the \csuse{cbx@first@\thefield{entrykey}} part because that part, which expands since it is not preceded by \string, will be unique for a citation within a reference section, such as a chapter. This way, the test will be performed on a per-reference section basis rather than globally.

The footcite:full:jurisdiction no longer contains any reference to the variable used for the test. It goes like this:

\newbibmacro*{footcite:full:jurisdiction}{%
  \printtext{%
    \bibhypertarget{\thefield{entrykey}:\the\value{instcount}}{%
      \usedriver%
        {\DeclareNameAlias{sortname}{default}}%
        {jurisdiction}}}%
  \iffieldundef{postnote}%
    {}%
    {\setunit{\addspace}}}%

The jurisdiction:shortform:intro is, unlike in my original question, now called by the driver for jurisdiction, and thus implicitly by footcite:full:jurisdiction. It goes like this:

\newbibmacro{jurisdiction:shortform:intro}{%
  \ifcsstring{cbx@\thefield{entrykey}:short:%
              \the\value{instcount}}{multi}%
    {\addspace\printtext[brackets]%
      {\printfield{shorthand}%
       \ifcsstring{cbx@\thefield{entrykey}:orig}{orig}%
         {\addcomma\addspace%
          \bibstring{citedto}\addspace\printfield{origtitle}}%
         {}}%
    }%
    {\ifcsstring{cbx@\thefield{entrykey}:orig}{orig}%
      {\addspace\printtext[brackets]%
        {\bibstring{citedto}\addspace\printfield{origtitle}}}%
      {}%
    }%
}%

As long as we are in the same reference section, cbx@\thefield{entrykey}:short:\the\value{instcount} will be the same as cbx@\thefield{entrykey}:short:\csuse{cbx@first@\thefield{entrykey}}, because the general footcite:save macro sets cbx@first@\thefield{entrykey} to \the\value{instcount} at the first full citation within the current reference section.

Notice here that it also contains a test whether the original title needs to be printed, which was not part of my initial question, but the mechanism for doing that is similar. (For anyone interested, the original title test tests whether any citation of the case contains a pinpoint, in which case the reporter cited to, provided by the origtitle field, is given in the first citation of the case).

Related Question