[Tex/LaTex] How to change the color of first half of parenthesis in citation

biblatexcolorhyperref

Hi I am using the natbib compatibility mode under biblatex:

\usepackage[style=authoryear-comp, sorting=nyt, natbib,
            uniquename=false, maxbibnames=99, dashed=false]{biblatex}

I frequently use the citation format of author (year), that is \citet.

First, I wanted to have link for all parts of the citation — author and year (default is just year). So I followed the solution here: 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}
     {\bibcloseparen\global\boolfalse{cbx:parens}}
     {}%
   \multicitedelim}
  {\usebibmacro{textcite:postnote}}

Then, I wanted to change color of the links to be blue, so I changed the setting in hyperset:

\usepackage[hyperfootnotes=true,bookmarksopen]{hyperref}                
\hypersetup{  
  pdffitwindow=false, 
  pdfstartview={XYZ null null 1.00}, 
  pdfnewwindow=true,     
  colorlinks=true,                % false: boxed links; true: colored links
  linkcolor=darkblue,             % Color of internal links
  citecolor=darkblue,             % Color of links to bibliography
  urlcolor=darkblue}              % Color of external links

However, once I call \citet, say XX(2000), the color for XX(2000 is in darkblue but the color for ) is in black.

Is there anyway to fix the color of ( to be black?

Best Answer

Full linking is hard for biblatex. So there is a good reason why only the year is linked by default. Have a look at https://tex.stackexchange.com/a/27615/35864 for a full-link solution that colours both parentheses for \textcite.

Alternatively, I could offer you this answer I once gave at golatex.de: https://golatex.de/biblatex-textcite-link-fehler-bei-einem-autor-t20366.html (at least I think it was me ...)

\documentclass{article}
\usepackage[british]{babel}
\usepackage{csquotes}
\usepackage[backend=biber,
         style=authoryear-comp,]{biblatex}
\usepackage{hyperref}

\usepackage{filecontents}
\begin{filecontents}{\jobname.bib}
@article{AuthorA2000,
author = {AuthorA},
title = {TitleA},
year = {2000}
}
@article{AuthorB2001,
author = {AuthorB},
title = {TitleB},
year = {2001}
}
@article{AuthorC2002a,
author = {AuthorC},
title = {TitleCa},
year = {2002}
}
@article{AuthorC2002b,
author = {AuthorC},
title = {TitleCb},
year = {2002}
}
@article{AuthorC2002c,
author = {AuthorC},
title = {TitleCc},
year = {2002}
}
@article{AuthorC2003,
   author = {AuthorC},
   title = {TitleC3},
   year = {2003}
}
\end{filecontents}

\addbibresource{\jobname.bib}
\addbibresource{biblatex-examples.bib}

\makeatletter
\newrobustcmd{\blx@imc@transparentprinttext}[2][]{%
  \ifblank{#2}
    {}
    {\ifblank{#1}
       {\let\blx@theformat\@firstofone}
       {\blx@getformat\blx@theformat{ffd}{#1}{}}%
     \ifdefvoid\blx@theformat
       {}
       {\blx@theformat{#2}}}}

\blx@regimcs{\transparentprinttext}

\DeclareCiteCommand{\textcite}[\cbx@textcite@init\cbx@textcite]
  {\gdef\cbx@savedkeys{}%
   \citetrackerfalse%
   \pagetrackerfalse%
   \DeferNextCitekeyHook%
   \usebibmacro{cite:init}%
   \global\undef\cbx@lasthash
   \global\undef\cbx@lastyear}
  {\ifthenelse{\iffirstcitekey\AND\value{multicitetotal}>0}
     {\protected@xappto\cbx@savedcites{()(\thefield{multipostnote})}%
      \global\clearfield{multipostnote}}
     {}%
   \xappto\cbx@savedkeys{\thefield{entrykey},}%
   \iffieldequals{namehash}{\cbx@lasthash}
     {\iffieldequals{labelyear}{\cbx@lastyear}
        {\csnumgdef{cbx@textcite@state@\the\c@citecount}{2}}
        {\csnumgdef{cbx@textcite@state@\the\c@citecount}{1}%
         \savefield{labelyear}{\cbx@lastyear}}}
     {\csnumgdef{cbx@textcite@state@\the\c@citecount}{0}%
      \stepcounter{textcitetotal}%
      \savefield{namehash}{\cbx@lasthash}}}
  {}
  {\protected@xappto\cbx@savedcites{%
     [\thefield{prenote}][\thefield{postnote}]{\cbx@savedkeys}}}


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

\DeclareFieldFormat{textcitehyperref}{%
  \DeclareFieldAlias{bibhyperref}{noformat}% Avoid nested links
  \bibhyperref{%
    #1%
    \ifnumgreater{\value{citecount}+1}{\value{citetotal}}
      {\ifbool{cbx:parens}
         {\bibcloseparen\global\boolfalse{cbx:parens}}
         {}}
      {\ifnumequal{\csuse{cbx@textcite@state@\the\numexpr\c@citecount+1\relax}}{0}
         {\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}}
   {}%
   \transparentprinttext[textcitehyperref]{%
      \restorebibmacro{textcite}%
      \usebibmacro{textcite}}}
\makeatother



\begin{document}

First, single cites which are working: \textcite{AuthorA2000} and so on \textcite{AuthorB2001} and so on \textcite{AuthorC2002a} and so on \textcite{AuthorC2002b} and so on \textcite{AuthorC2002c}. This works!
\par
Now combined: Different Authors \\
Two: \textcite{AuthorA2000,AuthorB2001}\\
Three: \textcite{AuthorA2000,AuthorB2001,AuthorC2002a}\\
Works again! \par
Now combined, single author same year
The problem if linking is on: \textcite{AuthorC2002a,AuthorC2002b,AuthorC2002c} \par
Same Author, diff year: Same problem: \textcite{AuthorC2002a,AuthorC2003}

\parencite{knuth:ct:a,knuth:ct:b}

\printbibliography
\end{document}

enter image description here


Here is a solution that never links (colours) the brackets. For \cite/\parencite we can use Audrey's approach from hyperlink name with biblatex authoryear (biblatex 1.4b). But for \textcite only want to additionally link the name, so we patch the textcite bibmacro with xpatch to link the labelname.

\documentclass{article}
\usepackage[british]{babel}
\usepackage{csquotes}
\usepackage[backend=biber,
         style=authoryear-comp,]{biblatex}
\usepackage{hyperref}

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

\savebibmacro{cite}

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

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

\usepackage{filecontents}
\begin{filecontents}{\jobname.bib}
@article{AuthorA2000,
author = {AuthorA},
title = {TitleA},
year = {2000}
}
@article{AuthorB2001,
author = {AuthorB},
title = {TitleB},
year = {2001}
}
@article{AuthorC2002a,
author = {AuthorC},
title = {TitleCa},
year = {2002}
}
@article{AuthorC2002b,
author = {AuthorC},
title = {TitleCb},
year = {2002}
}
@article{AuthorC2002c,
author = {AuthorC},
title = {TitleCc},
year = {2002}
}
@article{AuthorC2003,
   author = {AuthorC},
   title = {TitleC3},
   year = {2003}
}
\end{filecontents}

\addbibresource{\jobname.bib}
\addbibresource{biblatex-examples.bib}

\begin{document}
First, single cites which are working: \textcite{AuthorA2000} and so on \textcite{AuthorB2001} and so on \textcite{AuthorC2002a} and so on \textcite{AuthorC2002b} and so on \textcite{AuthorC2002c}. This works!
\par
Now combined: Different Authors \\
Two: \textcite{AuthorA2000,AuthorB2001}\\
Three: \textcite{AuthorA2000,AuthorB2001,AuthorC2002a}\\
Works again! \par
Now combined, single author same year
The problem if linking is on: \textcite{AuthorC2002a,AuthorC2002b,AuthorC2002c} \par
Same Author, diff year: Same problem: \textcite{AuthorC2002a,AuthorC2003}

\parencite{knuth:ct:a,knuth:ct:b}

\printbibliography
\end{document}

enter image description here