2

I am using the macro below to override the \cite{} command, so that the entire citation is highlighted/hyperlinked.

The \cite command gives me a citation of the form "Smith, 2000". The \textcite{} command gives me a citation of the form "Smith (2000"). How can I add commands that will result in "(Smith, 2000)" and "(Smith, 2000, p. 1)"?

\DeclareFieldFormat{citehyperref}{%
    \DeclareFieldAlias{bibhyperref}{noformat}% Avoid nested links
    \bibhyperref{#1}}

\DeclareFieldFormat{textcitehyperref}{%
    \DeclareFieldAlias{bibhyperref}{noformat}% Avoid nested links
    \bibhyperref{%
        #1%
        \ifbool{cbx:parens}
        {\bibcloseparen\global\boolfalse{cbx:parens}}
        {}}}

\savebibmacro{cite}
\savebibmacro{textcite}

\renewbibmacro*{cite}{%
    \printtext[citehyperref]{%
        \restorebibmacro{cite}%
        \usebibmacro{cite}}}

\renewbibmacro*{textcite}{%
    \ifboolexpr{
        ( not test {\iffieldundef{prenote}} and
        test {\ifnumequal{\value{citecount}}{1}} )
        or
        ( not test {\iffieldundef{postnote}} and
        test {\ifnumequal{\value{citecount}}{\value{citetotal}}} )
    }
    {\DeclareFieldAlias{textcitehyperref}{noformat}}
    {}%
    \printtext[textcitehyperref]{%
        \restorebibmacro{textcite}%
        \usebibmacro{textcite}}}
Adam_G
  • 816
  • Are you just looking for \parencite? Yes, the parentheses are not linked there, but that would get more complicated, plus you would have to split the links if you had two citations in the same bracket group. – moewe Mar 15 '16 at 06:56
  • Got it. Thanks! Does the current macro not work with multiple citations in the same group? I thought it did. – Adam_G Mar 15 '16 at 11:37
  • It does, it just doesn't link the parentheses themselves, I don't think. The rest should all be working as expected. Why don't you try it and report back. – moewe Mar 15 '16 at 13:10
  • Thanks. My test bibliography was small, which is why I didn't realize it was working. Is the a way to change the color of the parenthesis in parencite? I just want it to match the color in textcite. – Adam_G Mar 15 '16 at 14:09
  • I cooked something up that should also link the parentheses correctly with \parecite. – moewe Mar 15 '16 at 14:34

1 Answers1

2

Credit, where credit is due. The code in the OP came from Audrey in hyperlink name with biblatex authoryear (biblatex 1.4b).

You can get the parentheses of \parencite linked if you modify the command itself and introduce a new macro

\newbibmacro*{parencite}{%
  \printtext[citehyperref]{%
    \DeclareFieldAlias{citehyperref}{noformat}%
    \iffirstcitekey
      {\bibopenparen}{}%
    \ifnumequal{\value{citecount}}{1}
      {\usebibmacro{prenote}}{}%
    \usebibmacro{cite}%
    \ifnumequal{\value{citecount}}{\value{citetotal}}
      {\usebibmacro{postnote}}{}%
    \iflastcitekey
      {\bibcloseparen}{}}}

\DeclareCiteCommand{\parencite}
  {}
  {\usebibmacro{citeindex}%
   \usebibmacro{parencite}}
  {\multicitedelim}
  {}

\DeclareMultiCiteCommand{\parencites}{\parencite}{\setunit{\multicitedelim}}

We essentially move the parentheses from the wrapper to the bibmacro itself allowing them to be linked to the correct entry at the cost of having to check whether to print the parentheses all the time.

MWE

\documentclass[12pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[style=authoryear]{biblatex}
\usepackage{hyperref}

\addbibresource{biblatex-examples.bib}

\DeclareFieldFormat{citehyperref}{%
    \DeclareFieldAlias{bibhyperref}{noformat}% Avoid nested links
    \bibhyperref{#1}}

\DeclareFieldFormat{textcitehyperref}{%
    \DeclareFieldAlias{bibhyperref}{noformat}% Avoid nested links
    \bibhyperref{%
        #1%
        \ifbool{cbx:parens}
        {\bibcloseparen\global\boolfalse{cbx:parens}}
        {}}}

\savebibmacro{cite}
\savebibmacro{textcite}

\renewbibmacro*{cite}{%
    \printtext[citehyperref]{%
        \restorebibmacro{cite}%
        \usebibmacro{cite}}}

\renewbibmacro*{textcite}{%
    \ifboolexpr{
        ( not test {\iffieldundef{prenote}} and
        test {\ifnumequal{\value{citecount}}{1}} )
        or
        ( not test {\iffieldundef{postnote}} and
        test {\ifnumequal{\value{citecount}}{\value{citetotal}}} )
    }
    {\DeclareFieldAlias{textcitehyperref}{noformat}}
    {}%
    \printtext[textcitehyperref]{%
        \restorebibmacro{textcite}%
        \usebibmacro{textcite}}}

\newbibmacro*{parencite}{%
  \printtext[citehyperref]{%
    \DeclareFieldAlias{citehyperref}{noformat}%
    \iffirstcitekey
      {\bibopenparen}{}%
    \ifnumequal{\value{citecount}}{1}
      {\usebibmacro{prenote}}{}%
    \usebibmacro{cite}%
    \ifnumequal{\value{citecount}}{\value{citetotal}}
      {\usebibmacro{postnote}}{}%
    \iflastcitekey
      {\bibcloseparen}{}}}

\DeclareCiteCommand{\parencite}
  {}
  {\usebibmacro{citeindex}%
   \usebibmacro{parencite}}
  {\multicitedelim}
  {}

\DeclareMultiCiteCommand{\parencites}{\parencite}{\setunit{\multicitedelim}}

\begin{document}
\cite{sigfridsson} and \parencite{sigfridsson} and \textcite{sigfridsson}

\cite{geer,worman} and \parencite{geer,worman} and \textcite{geer,worman}

\cite{knuth:ct:a,knuth:ct:b,knuth:ct:c} and \parencite{knuth:ct:a,knuth:ct:b,knuth:ct:c} and \textcite{knuth:ct:a,knuth:ct:b,knuth:ct:c}

\parencites{geer}{worman} and \parencites{knuth:ct:a}{knuth:ct:b}{knuth:ct:c}

\printbibliography
\end{document}

citation portion of the MWE

moewe
  • 175,683
  • That's perfect, thank you! What are your thoughts about renaming the \parencite to \cite, just for the sake of brevity, since it's what I'll be using in my document? Is this not a good idea? – Adam_G Mar 15 '16 at 14:49
  • 1
    @Adam_G If you are not using the "standard" \cite macro that might be OK. You could also think about using the slightly higher level command \autocite and configure that to use \parencite. But that really is up to you. I personally would not rename something that is essentially \parencite to \cite, but that is probably me expecting \cite not to have parentheses in author-year styles. – moewe Mar 15 '16 at 14:52
  • Ok, thanks. I appreciate it. Any chance you know of a good tutorial to understand a macro like this? I don't want to just do copy/pasting. – Adam_G Mar 15 '16 at 14:54
  • @Adam_G I don't know about any good biblatex-programming tutorials. You will find some great answers on this site though, that explain what is going on the higher-level stuff in the def and bbx, cbx files is also fairly digestible. And if you are a bit familiar with the general going ons of LaTeX, you will find that biblatex macros are not that different. There are two German articles in DTK from 2008 http://biblatex.dominik-wassenhoven.de/dtk.shtml I know of. – moewe Mar 15 '16 at 14:59
  • @Adam_G A start on this site would be Guidelines for customizing biblatex styles – moewe Mar 15 '16 at 15:00
  • One small problem with this is using something like \parencite[inter alia]{smith2000,jacobs2000}. The "inter alia" ends up outside of the parenthesis. – Adam_G Mar 15 '16 at 15:31
  • @Adam_G Mhhh, yes, try the edited version. The problem is that biblatex is not exited about the trickeries we used here. – moewe Mar 15 '16 at 15:41
  • @Adam_G I just noticed an issue with \parencites. Please check the updated version, it gets uglier but should be safer now. – moewe Mar 15 '16 at 15:58
  • What was the issue? – Adam_G Mar 15 '16 at 18:11
  • 1
    @Adam_G \parencites[vgl.][12]{geer}[cf.][13]{worman} swallowed some post and pre-notes. – moewe Mar 16 '16 at 07:13
  • I completely overlooked that there was a parenciteS. I guess I'd use that when there are different details about each citation? – Adam_G Mar 16 '16 at 11:32
  • 1
    @Adam_G Indeed. If you use \parencite[pre][post]{foo,bar} the prenote will only refer to the first citation and the postnote to the last citation. With \parencites(globale pre)(global post)[pre][post]{foo}[pre][post]{bar} you have a finer control over the note placement. – moewe Mar 16 '16 at 11:36