2

I customised my biblatex citation to look exactly how I want them to look, except the comma in \citep should have the same color as the link. (It does not matter if the comma is actually inside the hyperlink or if it will just have the same color)

MWE:

\documentclass{book}

\usepackage{xcolor}
\usepackage[
    style=authoryear,
    natbib=true,            
]{biblatex}

\addbibresource{Thesis.bib}

% modify \cite to add parenthesis around the year
\renewbibmacro*{cite}{%
    \iffieldundef{shorthand}{%
        \ifthenelse{%
            \ifnameundef{labelname}\OR\iffieldundef{labelyear}%
        }{%
            \usebibmacro{cite:label}%
            \setunit{\addspace}%
        }{%
            \printnames{labelname}%
            \setunit{\addspace}%
        }%
%       \ifentrytype{online}{\addspace \textendash\addspace\usebibmacro{title}}{%
            \printtext[parens]{\usebibmacro{cite:labeldate+extradate}}%
%       }%  
    }%
    {\usebibmacro{cite:shorthand}}%
}

\DeclareCiteCommand{\cite}
    {\usebibmacro{prenote}}
    {\usebibmacro{citeindex}%
        \printtext[bibhyperref]{\usebibmacro{cite}}}
    {   
%   \multicitedelim
    \textcitedelim
    }
    {\usebibmacro{postnote}}

\DeclareCiteCommand{\citep}[\mkbibparens]{%
    \usebibmacro{prenote}%
}{%
    \usebibmacro{citeindex}%
    \iffieldundef{shorthand}{%
        \ifthenelse{%
            \ifnameundef{labelname}\OR\iffieldundef{labelyear}%
        }{%
            \usebibmacro{cite:label}%
            \setunit{\addspace}%
        }{%
            \printtext[bibhyperref]{% Apply citation link to bibmacro output
            \DeclareFieldAlias{bibhyperref}{default}%
            \printnames[labelname_p]{labelname}%
            \setunit{\nameyeardelim}%
        }
        }%
        \usebibmacro{cite:labeldate+extradate}%
    }{%
        \usebibmacro{cite:shorthand}%
    }%
}{%
%   \multicitedelim%
  \ifnumequal{\value{citecount}}{\value{citetotal}}
    {\addspace\bibstring{and}\addspace}%
    {\multicitedelim}%
}{%
    \usebibmacro{postnote}%
}

\usepackage{hyperref}               
\hypersetup{
    colorlinks=true,            
    citecolor=red,
}

\begin{filecontents}{Thesis.bib}
@ARTICLE{2019arXiv190606091W,
       author = {{Wolf}, C. and {Langer}, C. and {Montes}, F. and {Pereira}, J. and
         {Ong}, W. -J. and {Poxon-Pearson}, T. and {Ahn}, S. and {Ayoub}, S. and
         {Baumann}, T. and {Bazin}, D.},
        title = "{Constraining the Neutron Star Compactness: Extraction of the $^{23}$Al($p,\gamma$) Reaction Rate for the $rp$-Process}",
      journal = {arXiv e-prints},
     keywords = {Nuclear Experiment, Astrophysics - Instrumentation and Methods for Astrophysics, Astrophysics - Solar and Stellar Astrophysics, Nuclear Theory},
         year = "2019",
        month = "Jun",
          eid = {arXiv:1906.06091},
        pages = {arXiv:1906.06091},
archivePrefix = {arXiv},
       eprint = {1906.06091},
 primaryClass = {nucl-ex},
       adsurl = {https://ui.adsabs.harvard.edu/abs/2019arXiv190606091W},
      adsnote = {Provided by the SAO/NASA Astrophysics Data System}
}
\end{filecontents}

\begin{document}

\cite{2019arXiv190606091W}  

\citep{2019arXiv190606091W}

\citep[this text][and this text should remain black]{2019arXiv190606091W}

\printbibliography


\end{document}

enter image description here

gigi
  • 402
  • Have you considered including the outer parentheses (and, by extension, everything they encase) into a single hyperlink? That way, you wouldn't have to worry about the treatment of the comma that separates the author and year fields. (Aside: I think that the separator comma is unnecessary and shouldn't be there.) Making the parentheses in the parenthetic-style citation call-out part of the hyperlink would also restore visual parity with the text-style citation call-out. – Mico Jun 21 '19 at 13:35
  • @Mico Thanks for your comment! The outer parentheses should not be colored. I also don't want to color everything, for example if there are two citations, the "and" between them should remain black. I only want to color this comma between the author and the year – gigi Jun 21 '19 at 13:42

1 Answers1

2

The following code applies the link to entire citation label consisting of author names, comma and year. Hence, the comma is also coloured. I used my biblatex-ext style and code similar to Macro For Hyperlinked Citation in Parentheses to simplify the complete setup.

The idea is to add the parentheses around the year with \DeclareInnerCiteDelims and the links by copying the original cite macro with \letbibmacro and re-wrapping it in a link.

Note that \letbibmacro needs a recent version of biblatex. (The version currently run on Overleaf is too old for \letbibmacro and also does not come with biblatex-ext.)

\documentclass{article}

\usepackage{xcolor}
\usepackage[
  style=ext-authoryear,
  natbib=true,
]{biblatex}

\usepackage{hyperref}
\hypersetup{
  colorlinks=true,
  citecolor=red,
}

\DeclareInnerCiteDelims{cite}{\bibopenparen}{\bibcloseparen}
\DeclareDelimFormat[cite]{nameyeardelim}{\addspace}

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

\letbibmacro{saved:cite}{cite}
\renewbibmacro*{cite}{%
  \printtext[citehyperref]{%
    \usebibmacro{saved:cite}}}

\addbibresource{biblatex-examples.bib}

\begin{document}
\cite{sigfridsson}

\parencite{sigfridsson}

\parencite[this text][and this text should remain black]{sigfridsson}

\printbibliography
\end{document}

enter image description here


If you really only want to colour the comma, you would normally use

\DeclareDelimFormat[cite,parencite]{nameyeardelim}{\textcolor{red}{\addcomma\space}}

but your redefinition of \citep uses \nameyeardelim and not the context-sensitive \printdelim{nameyeardelim}, so you may as well replace

\setunit{\nameyeardelim}%

with

\setunit{\textcolor{red}{\nameyeardelim}}%

Note that you will have to add a forgotten % after the following closing curly brace (the } in the next line) to avoid unwanted white space (that space is automatically removed by \addcomma, but can not be removed by \textcolor{red}{\addcomma}).

moewe
  • 175,683