TransWikia.com

Problem with code for fully linked citations with biblatex-apa when citing the same name twice in a row

TeX - LaTeX Asked by FF0497 on August 14, 2021

I am having a problem which is unfortunately quite specific.
Using biber and hyperref, I was trying to use the author’s name as a hyperref reference, too (not only the year) which is similar to bibtex/natbib.
In general, I think it looks quite nice but there is a problem when trying to reference the same author twice in a row:
enter image description here

What I would like to have when citing the same authors twice is the same output as in 4) but using parencite or textcite instead of citeauthor and then citeyear.

My code for producing the output above:

documentclass{scrartcl}
usepackage[backend=biber, style=apa]{biblatex}
addbibresource{Bib.bib}
usepackage{hyperref}
usepackage{xcolor}
hypersetup{
    colorlinks,
    linkcolor={red!50!black},
    citecolor={blue!50!black},
    urlcolor={blue!80!black}
}

input{biberhyperlink.tex}

begin{document}

1) First citations parencite{Meltzer.Richard.1981} work quite nicely parencite{Facchini.2018}.

2) However, when citing one author parencite{Meltzer.Richard.1981} a second time in a row parencite{Meltzer.Richard.1983}, it does not.

3) textcite{Meltzer.Richard.1981} and textcite{Meltzer.Richard.1983} does not work properly either.

4) citeauthor{Meltzer.Richard.1981} (citeyear{Meltzer.Richard.1981}) and citeauthor{Meltzer.Richard.1983} (citeyear{Meltzer.Richard.1983}) would work as a workaround.

end{document}

Where the biberhyperlink.tex file defines the modified functions. I took them from other tex stackexchange questions (hyperlink name with biblatex authoryear and hyperlinking author names in biblatex when using citeauthor) as noted below. I would guess there is a mistake but cannot figure it out myself:

% https://tex.stackexchange.com/questions/1687/hyperlink-name-with-biblatex-authoryear

DeclareCiteCommand{cite}
  {usebibmacro{prenote}}
  {usebibmacro{citeindex}%
   printtext[bibhyperref]{usebibmacro{cite}}}
  {multicitedelim}
  {usebibmacro{postnote}}

DeclareCiteCommand*{cite}
  {usebibmacro{prenote}}
  {usebibmacro{citeindex}%
   printtext[bibhyperref]{usebibmacro{citeyear}}}
  {multicitedelim}
  {usebibmacro{postnote}}

DeclareCiteCommand{parencite}[mkbibparens]
  {usebibmacro{prenote}}
  {usebibmacro{citeindex}%
    printtext[bibhyperref]{usebibmacro{cite}}}
  {multicitedelim}
  {usebibmacro{postnote}}

DeclareCiteCommand*{parencite}[mkbibparens]
  {usebibmacro{prenote}}
  {usebibmacro{citeindex}%
    printtext[bibhyperref]{usebibmacro{citeyear}}}
  {multicitedelim}
  {usebibmacro{postnote}}

DeclareCiteCommand{footcite}[mkbibfootnote]
  {usebibmacro{prenote}}
  {usebibmacro{citeindex}%
  printtext[bibhyperref]{ usebibmacro{cite}}}
  {multicitedelim}
  {usebibmacro{postnote}}

DeclareCiteCommand{footcitetext}[mkbibfootnotetext]
  {usebibmacro{prenote}}
  {usebibmacro{citeindex}%
   printtext[bibhyperref]{usebibmacro{cite}}}
  {multicitedelim}
  {usebibmacro{postnote}}

DeclareCiteCommand{textcite}
  {boolfalse{cbx:parens}}
  {usebibmacro{citeindex}%
   printtext[bibhyperref]{usebibmacro{textcite}}}
  {ifbool{cbx:parens}
     {bibcloseparenglobalboolfalse{cbx:parens}}
     {}%
   multicitedelim}
  {usebibmacro{textcite:postnote}}

% https://tex.stackexchange.com/questions/75902/hyperlinking-author-names-in-biblatex-when-using-citeauthor
DeclareCiteCommand{citeauthor}
  {boolfalse{citetracker}%
   boolfalse{pagetracker}%
   usebibmacro{prenote}}
  {ifciteindex
     {indexnames{labelname}}
     {}%
   printtext[bibhyperref]{printnames{labelname}}}
  {multicitedelim}
  {usebibmacro{postnote}}

For reproduction purposes, this is my bib file Bib.bib:

@article{Facchini.2018,
 author = {Facchini, Francois},
 year = {2018},
 title = {{What Are the Determinants of Public Spending? An Overview of the Literature}}
}
@article{Meltzer.Richard.1981,
 author = {Meltzer, Allan H. and Richard, Scott F.},
 year = {1981},
 title = {{A Rational Theory of the Size of Government}}
}
@article{Meltzer.Richard.1983,
 author = {Meltzer, Allan H. and Richard, Scott F.},
 year = {1983},
 title = {{Tests of a Rational Theory of the Size of Government}}
}

If there is no solution to this problem, I would have to use the workaround shown in 4) but obviously, I would prefer a simpler solution.

One Answer

The code from hyperlink name with biblatex authoryear was written for style=authoryear,. It simply does not work with biblatex-apa's style=apa,.

For biblatex-apa I suggest the approach shown in BibLaTeX Colors And Links Only The Year Not The Rest Of The Citation (shown there for a slightly older biblatex-apa version). The following works for the current release version v9.14 (2020-08-28).

documentclass{scrartcl}
usepackage[backend=biber, style=apa]{biblatex}

usepackage{hyperref}
usepackage{xcolor}
hypersetup{
    colorlinks,
    linkcolor={red!50!black},
    citecolor={blue!50!black},
    urlcolor={blue!80!black}
}

makeatletter
renewbibmacro*{cite:plabelyear+extradate}{%
  iffieldundef{labelyear}{}
    {clearfield{labelmonth}% don't want months in citations
     clearfield{labelday}% don't want days in citations
     clearfield{labelendmonth}% don't want months in citations
     clearfield{labelendday}% don't want days in citations
     iffieldsequal{labelyear}{labelendyear}% Don't want no-op year ranges
       {clearfield{labelendyear}}
       {}%
     iffieldundef{origyear}
       {}
       {printorigdate%
        setunit*{addslash}}%
     iffieldundef{related}
       {}
       {iffieldequalstr{relatedtype}{reprintfrom}
         {entrydata*{thefield{related}}{printlabeldateextra}%
          setunit*{addslash}}
         {}}%
     printlabeldateextra}}

renewbibmacro*{citeyear}{%
  iffieldundef{labelyear}
    {usebibmacro{cite:init}}
    {iffieldequals{fullhash}{cbx@lasthash}
       {setunit{compcitedelim}%
        printtext[bibhyperref]{usebibmacro{cite:plabelyear+extradate}}}
       {printtext[bibhyperref]{usebibmacro{cite:plabelyear+extradate}}%
        savefield{fullhash}{cbx@lasthash}}}%
  setunit{multicitedelim}}

renewbibmacro*{cite:author}{%
  iffieldequals{fullhash}{cbx@lasthash}
   {setunit{compcitedelim}%
    printtext[bibhyperref]{usebibmacro{cite:plabelyear+extradate}}}%
   {printtext[bibhyperref]{%
      ifnameundef{labelname}
        {usebibmacro{cite:noname}%
          savefield{fullhash}{cbx@lasthash}}
        {ifnameundef{shortauthor}
          {printnames{labelname}}%
          {cbx@apa@ifnamesaved
             {printnames{shortauthor}}
             {ifnameundef{groupauthor}
               {printnames[labelname]{author}}
               {printnames[labelname]{groupauthor}}%
              addspaceprintnames[sabrackets]{shortauthor}}}%
           savefield{fullhash}{cbx@lasthash}}}}%
   setunit{multicitedelim}}

renewbibmacro*{cite}{%
  iffieldequals{fullhash}{cbx@lasthash}
   {setunit{compcitedelim}%
    printtext[bibhyperref]{%
      usebibmacro{cite:plabelyear+extradate}}}%
   {printtext[bibhyperref]{%
      ifnameundef{labelname}
       {usebibmacro{cite:noname}%
         setunit{printdelim{nameyeardelim}}%
         usebibmacro{cite:plabelyear+extradate}%
         savefield{fullhash}{cbx@lasthash}}
       {ifnameundef{shortauthor}
         {printnames{labelname}}%
         {cbx@apa@ifnamesaved
           {printnames{shortauthor}}
           {ifnameundef{groupauthor}
             {printnames[labelname]{author}}
             {printnames[labelname]{groupauthor}}%
            addspaceprintnames[sabrackets]{shortauthor}}}%
         setunit{printdelim{nameyeardelim}}%
        usebibmacro{cite:plabelyear+extradate}%
        savefield{fullhash}{cbx@lasthash}}}}%
   setunit{multicitedelim}}

renewbibmacro*{textcite}{%
  iffieldequals{fullhash}{cbx@lasthash}
% Compact cite - more than one thing for same author
    {setunit{compcitedelim}%
     printtext[bibhyperref]{%
       usebibmacro{cite:plabelyear+extradate}}}
% New cite
    {ifbool{cbx:parens}
       {bibcloseparenglobalboolfalse{cbx:parens}}
       {}%
     setunit{textcitedelim}%
     ifnameundef{labelname}
     % No author/editor
       {iffieldundef{shorthand}%
    % Cite using title
         {printtext[bibhyperref]{usebibmacro{cite:noname}}%
          setunit{globalbooltrue{cbx:parens}addspacebibopenparen}%
          printtext[bibhyperref]{usebibmacro{cite:plabelyear+extradate}}}
    % Cite using shorthand
         {printtext[bibhyperref]{usebibmacro{cite:shorthand}}}}
  % Normal cite with author/editor
  % Normal full cite
       {printtext[bibhyperref]{%
          ifnameundef{shortauthor}%
    % Normal full cite
           {printnames{labelname}}
    % Cite using short author
           {cbx@apa@ifnamesaved
             {printnames{shortauthor}}
             {ifnameundef{groupauthor}
               {printnames[labelname]{author}}
               {printnames[labelname]{groupauthor}}}}}%
  % Year
        setunit{globalbooltrue{cbx:parens}addspacebibopenparen}%
  % Put the shortauthor inside the year brackets if necessary
        printtext[bibhyperref]{%
          ifnameundef{shortauthor}
           {}
           {cbx@apa@ifnamesaved
             {}
             {printnames{shortauthor}%
              setunit{printdelim{nameyeardelim}}}}%
  % Print prenote (belongs to first cite)
          ifnumequal{value{citecount}}{1}
             {usebibmacro{prenote}}
             {}%
  % Actual year printing
          usebibmacro{cite:plabelyear+extradate}}%
  % Save name hash for checks later
        savefield{fullhash}{cbx@lasthash}}%
    stepcounter{textcitecount}}}
makeatother

begin{filecontents}{jobname.bib}
@article{Facchini.2018,
  author = {Facchini, Francois},
  year   = {2018},
  title  = {What Are the Determinants of Public Spending? An Overview of the Literature},
}
@article{Meltzer.Richard.1981,
  author = {Meltzer, Allan H. and Richard, Scott F.},
  year   = {1981},
  title  = {A Rational Theory of the Size of Government},
}
@article{Meltzer.Richard.1983,
  author = {Meltzer, Allan H. and Richard, Scott F.},
  year   = {1983},
 title   = {Tests of a Rational Theory of the Size of Government}, 
}
end{filecontents}
addbibresource{jobname.bib}

begin{document}

begin{enumerate}
item First citations parencite{Meltzer.Richard.1981}
  work quite nicely parencite{Facchini.2018}.
item Citing one author parencite{Meltzer.Richard.1981}
  a second time in a row parencite{Meltzer.Richard.1983} also works.
item textcite{Meltzer.Richard.1981} and textcite{Meltzer.Richard.1983}
  works as well.
item citeauthor{Meltzer.Richard.1981}'s autocite*{Meltzer.Richard.1981}
end{enumerate}
printbibliography
end{document}

Nicely linked citations.

Correct answer by moewe on August 14, 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