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?

3
  • Is the hyperlink really that bad?
    – moewe
    Sep 26 '16 at 19:49
  • For me, yes. :) There has to be some way around this? Sep 26 '16 at 19:52
  • Yes, there definitely is. But I thought it might be easier to talk you out of it than to find the solution. ;-)But alas, ... let me see what I can do.
    – moewe
    Sep 26 '16 at 19:52
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

1
  • 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.