3

I would like to add an [Author(s) Year] block at the beginning of every citation, exactly as described in this question: Adding an [AuthorYear] block at the beginning of bibliography entries.

I used the very code given in the accepted answer to that question. However, in addition, I want my citations to be hyperlinked, and therefore use the package hyperref. The following is a minimal working example which demonstrates my problem:

\documentclass{article}

% Adding a yearauthor block in bibliohraphy: https://tex.stackexchange.com/a/11856/42810
\usepackage[backend=bibtex,citestyle=authoryear,bibstyle=authortitle,sorting=nyt,dashed=false,maxcitenames=2]{biblatex}

\newcounter{mymaxcitenames}
\AtBeginDocument{%
  \setcounter{mymaxcitenames}{\value{maxnames}}%
}

\renewbibmacro*{begentry}{%
  \printtext[brackets]{%
    \begingroup
    \defcounter{maxnames}{\value{mymaxcitenames}}%
    \printnames{labelname}%
    \setunit{\nameyeardelim}%
    \usebibmacro{cite:labelyear+extrayear}%
    \endgroup
    }%
  \addspace%
}

\DeclareNameAlias{sortname}{first-last}

% Use hyperref to hyperlink citations
\usepackage[dvipsnames]{xcolor}
\usepackage[hyperindex=true]{hyperref}
\hypersetup{colorlinks=true,linkcolor=Blue,citecolor=BrickRed}


\begin{filecontents}{\jobname.bib}
@article{foo,
  author    = {Grace Murray Hopper},
  title     = {The Education of a Computer},
  journal   = {{IEEE} Annals of the History of Computing},
  volume    = {9},
  number    = {3/4},
  pages     = {271--281},
  year      = {1987}
}
\end{filecontents}

\bibliography{\jobname}

\begin{document}

Here is a citation: \cite{foo}.

\printbibliography

\end{document}

This produces the following: enter image description here

As you can see, the authoryear block in the bibliography contains a hyperlink to itself. I do not want this hyperlink, although I do want the hyperlink in the running text (i.e., before the bibliograpy). How can I remove the hyperlink from the authoryear block in the bibliography?

1 Answers1

2

We only need to make the bibhyperref format do nothing in the "introductory" block. To this end we insert \DeclareFieldFormat{bibhyperref}{##1}% (the double # is necessary because we are already within a macro definition) into the redefinition of begentry

\renewbibmacro*{begentry}{%
  \printtext[brackets]{%
    \begingroup
    \DeclareFieldFormat{bibhyperref}{##1}%
    \defcounter{maxnames}{\value{mymaxcitenames}}%
    \printnames{labelname}%
    \setunit{\nameyeardelim}%
    \usebibmacro{cite:labelyear+extrayear}%
    \endgroup
    }%
  \addspace%
}

This gives the desired result

\documentclass{article}

% Adding a yearauthor block in bibliohraphy: http://tex.stackexchange.com/a/11856/42810
\usepackage[backend=bibtex,citestyle=authoryear,bibstyle=authortitle,sorting=nyt,dashed=false,maxcitenames=2]{biblatex}

\newcounter{mymaxcitenames}
\AtBeginDocument{%
  \setcounter{mymaxcitenames}{\value{maxnames}}%
}

\renewbibmacro*{begentry}{%
  \printtext[brackets]{%
    \begingroup
    \DeclareFieldFormat{bibhyperref}{##1}%
    \defcounter{maxnames}{\value{mymaxcitenames}}%
    \printnames{labelname}%
    \setunit{\nameyeardelim}%
    \usebibmacro{cite:labelyear+extrayear}%
    \endgroup
    }%
  \addspace%
}

\DeclareNameAlias{sortname}{first-last}

% Use hyperref to hyperlink citations
\usepackage[dvipsnames]{xcolor}
\usepackage[hyperindex=true]{hyperref}
\hypersetup{colorlinks=true,linkcolor=Blue,citecolor=BrickRed}


\begin{filecontents}{\jobname.bib}
@article{foo,
  author    = {Grace Murray Hopper},
  title     = {The Education of a Computer},
  journal   = {{IEEE} Annals of the History of Computing},
  volume    = {9},
  number    = {3/4},
  pages     = {271--281},
  year      = {1987}
}
\end{filecontents}

\bibliography{\jobname}

\begin{document}
Here is a citation: \cite{foo}.

\printbibliography
\end{document}

enter image description here

moewe
  • 175,683
  • 1
    Note that nowadays Biber is the preferred backend (BibTeX does not support the full set of biblatex features). Starting from version 3.3 of bibaltex \DeclareNameAlias{sortname}{first-last} should be replaced by \DeclareNameAlias{sortname}{given-family}. \bibliography{\jobname} can (should) be replaced by \addbibresource{\jobname.bib} (note that this includes the file ending). – moewe Sep 26 '16 at 20:02