[Tex/LaTex] Different link colors for citeauthor and cite commands

colorhyperref

This question is apparently the same as this one, where the author did not follow up by providing a working example.

I would like to make the color of \citeauthor links to be black and the color of \cite to be blue. In the example that follows, both the author and the number citation are colored blue, and there is apparently not a citeauthorcolor keyword for hyperref. How can I produce different colors?

main.tex

\documentclass[12pt]{article}

\usepackage[colorlinks=true, citecolor=blue]{hyperref}
\usepackage[numbers]{natbib}

\begin{document}

The work by \citeauthor{myref} was a fancy read~\cite{myref}.

\bibliographystyle{plainnat}
\bibliography{main}

\end{document}

main.bib

@article{myref,
    title = {Fancy title},
    journal = {Journal of Fancy Publications},
    author = {Jane Dee and John Doe},
    year = {2017},
    pages = {293--311},
}

Compile: pdflatex main; bibtex main; pdflatex main; pdflatex main;

Best Answer

Here is a "hack":

\documentclass[12pt]{article}

\usepackage[colorlinks=true, citecolor=blue]{hyperref}
\usepackage[numbers]{natbib}
\usepackage{filecontents}

\let\oldciteauthor=\citeauthor
\def\citeauthor#1{\hypersetup{citecolor=blue}\oldciteauthor{#1}}
\let\oldcite=\cite
\def\cite#1{\hypersetup{citecolor=red}\oldcite{#1}}
\begin{filecontents}{main.bib}
@article{myref,
    title = {Fancy title},
    journal = {Journal of Fancy Publications},
    author = {Jane Dee and John Doe},
    year = {2017},
    pages = {293--311},
}
\end{filecontents}

\begin{document}

The work by \citeauthor{myref} was a fancy read~\cite{myref}.

\bibliographystyle{plainnat}
\bibliography{main}

\end{document}

Output:

enter image description here