4

Consider the following MWE:

\documentclass{beamer}

\usepackage{xpatch}
\usepackage{lmodern}

\usepackage[style=authoryear-comp,backend=biber]{biblatex}
\addbibresource{biblatex-examples.bib}

\hypersetup{colorlinks=true}

% Combine label and labelyear links
\xpatchbibmacro{cite}
{\usebibmacro{cite:label}%
\setunit{\addspace}%
\usebibmacro{cite:labelyear+extrayear}}
{\printtext[bibhyperref]{%
    \DeclareFieldAlias{bibhyperref}{default}%
    \usebibmacro{cite:label}%
    \setunit{\addspace}%
    \usebibmacro{cite:labelyear+extrayear}}}{}{}

% Include labelname in labelyear link
\xpatchbibmacro{cite}
{\printnames{labelname}%
\setunit{\nameyeardelim}%
\usebibmacro{cite:labelyear+extrayear}}
{\printtext[bibhyperref]{%
    \DeclareFieldAlias{bibhyperref}{default}%
    \printnames{labelname}%
    \setunit{\nameyeardelim}%
    \usebibmacro{cite:labelyear+extrayear}}}{}{}

% Access hyperref's citation link start/end commands
\makeatletter
\protected\def\blx@imc@biblinkstart{%
\@ifnextchar[%]
    {\blx@biblinkstart}
    {\blx@biblinkstart[\abx@field@entrykey]}}
\def\blx@biblinkstart[#1]{%
\blx@sfsave\hyper@natlinkstart{\the\c@refsection @#1}\blx@sfrest}
\protected\def\blx@imc@biblinkend{%
\blx@sfsave\hyper@natlinkend\blx@sfrest}
\blx@regimcs{\biblinkstart \biblinkend}
\makeatother

\newbool{cbx:link}

% Include parentheses around labelyear in \textcite only in
% single citations without pre- and postnotes
\def\iflinkparens{%
\ifboolexpr{ test {\ifnumequal{\value{multicitetotal}}{0}} and
            test {\ifnumequal{\value{citetotal}}{1}} and
            test {\iffieldundef{prenote}} and
            test {\iffieldundef{postnote}} }}

\xpatchbibmacro{textcite}
{\printnames{labelname}}
{\iflinkparens
    {\DeclareFieldAlias{bibhyperref}{default}%
    \global\booltrue{cbx:link}\biblinkstart%
    \printnames{labelname}}
    {\printtext[bibhyperref]{\printnames{labelname}}}}{}{}

\xpatchbibmacro{textcite}
{\usebibmacro{cite:label}}
{\iflinkparens
    {\DeclareFieldAlias{bibhyperref}{default}%
    \global\booltrue{cbx:link}\biblinkstart%
    \usebibmacro{cite:label}}
    {\usebibmacro{cite:label}}}{}{}

\xpretobibmacro{textcite:postnote}
{\ifbool{cbx:link}% patch 2.7+
    {\ifbool{cbx:parens}
        {\bibcloseparen\global\boolfalse{cbx:parens}}
        {}%
        \biblinkend\global\boolfalse{cbx:link}}
        {}}
{}
{\xpatchbibmacro{textcite}% patch earlier releases
    {\setunit{%
        \ifbool{cbx:parens}
        {\bibcloseparen\global\boolfalse{cbx:parens}}
        {}%
        \multicitedelim}}
    {\ifbool{cbx:link}
        {\ifbool{cbx:parens}
        {\bibcloseparen\global\boolfalse{cbx:parens}}
        {}%
        \biblinkend\global\boolfalse{cbx:link}}
        {}%
    \setunit{%
        \ifbool{cbx:parens}
        {\bibcloseparen\global\boolfalse{cbx:parens}}
        {}%
        \multicitedelim}}
    {}{}}

\begin{document}
\begin{frame}
  \textcite{knuth:ct:e}
\end{frame}
\begin{frame}
  \frametitle{Bibliography}
  \printbibliography
\end{frame}
\end{document}

Since I want hyperlink in citations to include the name of the author and not just the year, I'm using, after the line that begins with \hypersetup, the code in this answer. However by doing this I'm getting the error: Undefined control sequence.

The problem might be with this part of the code:

\xpatchbibmacro{textcite}
    {\printnames{labelname}}
    {\iflinkparens
        {\DeclareFieldAlias{bibhyperref}{default}%
        \global\booltrue{cbx:link}\biblinkstart%
        \printnames{labelname}}
        {\printtext[bibhyperref]{\printnames{labelname}}}}{}{}

When I remove it, the error goes away but I don't get the desired result of hyperlinking both the author and the year.

  • Which code? .... – cfr Sep 22 '14 at 21:15
  • @cfr See my updated question please. – petobens Sep 22 '14 at 21:55
  • And how is the biblatex code integrated in your example .tex file? That file has its own problem: you use \hypersetup without first loading the hyperref package. Add \usepackage{hyperref} before the line \hypersetup. – jon Sep 27 '14 at 0:34
  • @jon Mm I'm pretty sure beamer loads hyperref by default (adding it explicitly still generates the error). In the post I state how the code is integrated in my file: after the \hypersetup line. – petobens Sep 27 '14 at 1:01
  • Ah, true. Never noticed that before. However, I can compile your .tex file without problems --- is the posted file the one that is causing the problems? Because, if so, then none of the biblatex code can be the culprit, since it is not being used in the example file. And if you are simply adding those lines in your preamble, that will fail miserably for a few reasons. Please edit your question so that you have one complete file that leaves no guesswork to others. – jon Sep 27 '14 at 1:05
1

If you replace the problematic part of your code with

\xpatchbibmacro{textcite}
{\printnames{labelname}}
{\printtext[bibhyperref]{\printnames{labelname}}}{}{}

You get a clickable author name and year. Only the space between and the parenthesis remain unclickable. Is this enough clickability?

enter image description here

\documentclass{beamer}

\usepackage{xpatch}
\usepackage{lmodern}

\usepackage[style=authoryear-comp,backend=biber]{biblatex}
\addbibresource{biblatex-examples.bib}

\hypersetup{colorlinks=true}

% Combine label and labelyear links
\xpatchbibmacro{cite}
{\usebibmacro{cite:label}%
    \setunit{\addspace}%
    \usebibmacro{cite:labelyear+extrayear}}
{\printtext[bibhyperref]{%
        \DeclareFieldAlias{bibhyperref}{default}%
        \usebibmacro{cite:label}%
        \setunit{\addspace}%
        \usebibmacro{cite:labelyear+extrayear}}}{}{}

% Include labelname in labelyear link
\xpatchbibmacro{cite}
{\printnames{labelname}%
    \setunit{\nameyeardelim}%
    \usebibmacro{cite:labelyear+extrayear}}
{\printtext[bibhyperref]{%
        \DeclareFieldAlias{bibhyperref}{default}%
        \printnames{labelname}%
        \setunit{\nameyeardelim}%
        \usebibmacro{cite:labelyear+extrayear}}}{}{}

% Access hyperref's citation link start/end commands
\makeatletter
\protected\def\blx@imc@biblinkstart{%
    \@ifnextchar[%]
    {\blx@biblinkstart}
    {\blx@biblinkstart[\abx@field@entrykey]}}
\def\blx@biblinkstart[#1]{%
    \blx@sfsave\hyper@natlinkstart{\the\c@refsection @#1}\blx@sfrest}
\protected\def\blx@imc@biblinkend{%
    \blx@sfsave\hyper@natlinkend\blx@sfrest}
\blx@regimcs{\biblinkstart \biblinkend}
\makeatother

\newbool{cbx:link}

% Include parentheses around labelyear in \textcite only in
% single citations without pre- and postnotes
\def\iflinkparens{%
    \ifboolexpr{ test {\ifnumequal{\value{multicitetotal}}{0}} and
        test {\ifnumequal{\value{citetotal}}{1}} and
        test {\iffieldundef{prenote}} and
        test {\iffieldundef{postnote}} }}

%\xpatchbibmacro{textcite}
%{\printnames{labelname}}
%{\iflinkparens
%   {\DeclareFieldAlias{bibhyperref}{default}%
%       \global\booltrue{cbx:link}\biblinkstart%
%       \printnames{labelname}}
%   {\printtext[bibhyperref]{\printnames{labelname}}}}{}{}

\xpatchbibmacro{textcite}
{\printnames{labelname}}
{\printtext[bibhyperref]{\printnames{labelname}}}{}{}

\xpatchbibmacro{textcite}
{\usebibmacro{cite:label}}
{\iflinkparens
    {\DeclareFieldAlias{bibhyperref}{default}%
        \global\booltrue{cbx:link}\biblinkstart%
        \usebibmacro{cite:label}}
    {\usebibmacro{cite:label}}}{}{}

\xpretobibmacro{textcite:postnote}
{\ifbool{cbx:link}% patch 2.7+
    {\ifbool{cbx:parens}
        {\bibcloseparen\global\boolfalse{cbx:parens}}
        {}%
        \biblinkend\global\boolfalse{cbx:link}}
    {}}
{}
{\xpatchbibmacro{textcite}% patch earlier releases
    {\setunit{%
            \ifbool{cbx:parens}
            {\bibcloseparen\global\boolfalse{cbx:parens}}
            {}%
            \multicitedelim}}
    {\ifbool{cbx:link}
        {\ifbool{cbx:parens}
            {\bibcloseparen\global\boolfalse{cbx:parens}}
            {}%
            \biblinkend\global\boolfalse{cbx:link}}
        {}%
        \setunit{%
            \ifbool{cbx:parens}
            {\bibcloseparen\global\boolfalse{cbx:parens}}
            {}%
            \multicitedelim}}
    {}{}}

\begin{document}
    \begin{frame}
        \textcite{knuth:ct:e}
    \end{frame}
    \begin{frame}
        \frametitle{Bibliography}
        \printbibliography
    \end{frame}
\end{document}

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.