[Tex/LaTex] BibLaTeX \DeclareCiteCommand: How to check shorthand and citeseen and choose accordingly

biblatexciting

This question is closely related to Biblatex: Autocite dependent on shorthand but since that question asks about autocite, I decided to open a new question.

I am trying to to declare a citation command \mycite, which behaves like \footcite for bib-entries without a shorthand field and for bib-entries with shorthand field when they are cited for the first time. If a bib-entry with shorthand field is cited for the second time \mycite should behave like \parencite

Here is a MWE:

\documentclass{article}

\usepackage{filecontents}
\begin{filecontents}{\jobname.bib}
    @book{Book1, author={Author 1}, title={Title 1}, shorthand={Shorthand1}}
    @book{Book2, author={Author 2}, title={Title 2}}
\end{filecontents}

\usepackage[style=verbose,backend=biber]{biblatex}
\addbibresource{\jobname.bib}

\DeclareCiteCommand{\mycite}[\mkbibfootnote]
  {\usebibmacro{prenote}}
  {\usebibmacro{citeindex}%
   \usebibmacro{cite}}
  {\multicitedelim}
  {\usebibmacro{cite:postnote}}

\begin{document}
Citing an entry with defined shorthand field for the first time \mycite{Book1}.

Citing an entry with undefined shorthand field \mycite{Book2}.

Citing an entry with defined shorthand field for the second time \mycite{Book1}.
\end{document}

This gives the following output:

enter image description here
enter image description here

I would like \mycite to output instead:

enter image description here
enter image description here

Sadly the following declaration of \mycite does not work:

\DeclareCiteCommand{\mycite}[\iffieldundef{shorthand}\mkbibfootnote\mkbibparens]
  {\usebibmacro{prenote}}
  {\usebibmacro{citeindex}%
   \usebibmacro{cite}}
  {\multicitedelim}
  {\usebibmacro{cite:postnote}}

So what I tried instead, was to repurpose the <precode> and <postcode> fields by copy pasting the definitions of \mkbibparens and others, which seems to work for simple tasks like using square or rounded brackets depending on the shorthand field:

\makeatletter
\DeclareCiteCommand{\mycite}[]
  {\iffieldundef{shorthand}{%
    \begingroup
    \blx@blxinit
    \blx@setsfcodes
    \bibopenparen
    \usebibmacro{prenote}}{%
    \begingroup
    \blx@blxinit
    \blx@setsfcodes
    \bibopenbracket
    \usebibmacro{prenote}}%
   }
  {\usebibmacro{citeindex}%
   \usebibmacro{cite}}
  {\multicitedelim}
  {\iffieldundef{shorthand}{%
    \usebibmacro{cite:postnote}%
    \bibcloseparen%
    \endgroup}{%
    \usebibmacro{cite:postnote}%
    \bibclosebracket%
    \endgroup}
  }
\makeatother

This might well be a very bad and ugly hack. And I have no idea how to do this with the \mkbibfootnote wrapper, which is more complex then the ones for \mkbibparens or \mkbibbrackets.

I would like to note, that the most important thing for me here is, to have a cite command which behaves like \footcite for entries without shorthand field and which behaves like \parencite for entries with shorthand field.

Best Answer

The following solution will work smoothly if you only ever cite one work per \mycite. This is because we had to move all the code into the loopcode of the citation command which is executed for every key cited. As we need to access information about the entry (i.e. does it have a shorthand field?) which is only available in the loopcode (and to a certain degree in the pre and postcodes?), it was necessary to make this adjustment.

\DeclareCiteCommand{\mycite}[\unspace]
  {}
  {\usebibmacro{citeindex}%
   \ifboolexpr{not test {\iffieldundef{shorthand}} and test {\ifciteseen}}%
     {\space\mkbibparens{\usebibmacro{prenote}\printfield{shorthand}\usebibmacro{cite:postnote}}}
     {\mkbibfootnote{\usebibmacro{prenote}\usebibmacro{cite}\usebibmacro{cite:postnote}}}}
  {\multicitedelim}
  {}

The idea is straightforward, we check if we have an entry with a shorthand that was already cited, if so we default to a \parencite with the shorthand; otherwise we do a normal cite in the footnote.

MWE

\documentclass{article}

\usepackage{filecontents}
\begin{filecontents*}{\jobname.bib}
    @book{Book1, author={Author 1}, title={Title 1}, shorthand={Shorthand1}}
    @book{Book2, author={Author 2}, title={Title 2}}
\end{filecontents*}

\usepackage[style=verbose,backend=biber]{biblatex}
\addbibresource{\jobname.bib}

\DeclareCiteCommand{\mycite}[\unspace]
  {}
  {\usebibmacro{citeindex}%
   \ifboolexpr{not test {\iffieldundef{shorthand}} and test {\ifciteseen}}%
     {\space\mkbibparens{\usebibmacro{prenote}\printfield{shorthand}\usebibmacro{cite:postnote}}}
     {\mkbibfootnote{\usebibmacro{prenote}\usebibmacro{cite}\usebibmacro{cite:postnote}}}}
  {\multicitedelim}
  {}

\begin{document}
Citing an entry with defined shorthand field for the first time\mycite[Cf.][1]{Book1}.

Citing an entry with undefined shorthand field \mycite[2]{Book2}.

Citing an entry with defined shorthand field for the second time\mycite[4]{Book1}.
\end{document}

enter image description here