Different color in citation with Hyperref

colorhyperref

I want to highlight some citations by putting them in blue, while the others are in black. I managed to do so by defining this command:

\newcommand\mycite[1]{\hypersetup{citecolor=blue}\cite{#1}\hypersetup{citecolor=black}

It works as intended, but if I have more than one cite, one of which should be marked and the others not, this does not help.

Here is a minimal example

\documentclass{article}

\usepackage[colorlinks,citecolor=black]{hyperref}
\newcommand\mycite[1]{\hypersetup{citecolor=blue}\cite{#1}\hypersetup{citecolor=black}}

\begin{document}
This is OK: \mycite{one} \cite{two}

But I have to chose between \mycite{one,two} or \cite{one,two}

\bibliographystyle{apalike}
\bibliography{biblio}
\end{document}

The content of biblio.bib is:

@misc{one,
  author = "Author one",
  title = "The reference to this should be in blue",
  howpublished = "Not at all",
  year = "2022"
}

@misc{two,
  author = "Author two",
  title = "The reference to this should be in black",
  howpublished = "Not at all",
  year = "2022"
}

This is what I get. I would like in the second case to have one, 2022 in blue and two, 2022 in black.

enter image description here

Best Answer

\documentclass{article}
\usepackage[colorlinks,citecolor=black]{hyperref}
\makeatletter
\def\bibcite#1#2{%
    \@newl@bel{b}{#1\@extra@binfo}{%
      \hyper@@link[cite\ifcsname citecontext-#1\endcsname \csname citecontext-#1\endcsname\fi]{}{cite.#1\@extra@b@citeb}{#2}%
    }%
  }%

\newcommand\@citespecialcolor{blue}
\makeatother  
  
 
\begin{document}
Give one a specialcolor

\makeatletter
\@namedef{citecontext-one}{special}
\makeatother
\cite{one,two}

Revert again
\makeatletter
\@namedef{citecontext-one}{}
\makeatother
\cite{one,two}

\bibliographystyle{apalike}
\bibliography{biblio}
\end{document}

enter image description here

Related Question