[Tex/LaTex] Distinguishing \ref and \cref through different colors

cleverefcolorcross-referencinghyperref

I have the following problem with the hyperref, and the cleverefpackages. I would like to distinguish internal links (e.g. sections, etc) from mathematical links (e.g. definitions, theorems, equations, etc) through different commands and different colors.

More precisely, I would like to use \ref for internal links with color A, and \cref with nameinlink option in cleveref (!) and color B for links with mathematical content.

The problem is that – apparently – this cannot be done. Indeed, I found two previous questions — How to use different colors for different \href commands? and How to reference an inequality with \cref? — on the topic, but they did not solve my problem. The posting Specific colour in equation links gets closer, but still it works for one situation (i.e. equation), while I would like to get everything in one shot, instead of writing in the preamble the same stuff for every single situation that can arise.

Here there is the way in which I tried to solve my problem.

\documentclass{article}

\usepackage{amsmath}
\usepackage{amsthm}
\usepackage{amsfonts}
\usepackage{amssymb}

\usepackage[usenames,dvipsnames]{xcolor}

\newtheorem{theorem}{Theorem}

\usepackage{varioref}
\usepackage{hyperref}

\usepackage[noabbrev,capitalise,nameinlink]{cleveref}

\hypersetup{colorlinks={true},linkcolor={blue},citecolor=green}

\newcommand{\mref}[3][red]{\cref{#2}{\color{#1}{#3}}}

%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{document}

\section{Bla}
\label{sec:bla}

\begin{theorem}
\label{th:gauss}
Gauss
\end{theorem}

\section{blabla}

In section \ref{sec:bla} we found \mref{th:gauss}

\end{document}

I think this does not work because the red color that I give through \mref does not work when facing the default blue color that comes with \cref. What I mean here is that there has to be some sort of a priority (sorry if I do not sound that technical) that goes to \cref, and does not make the red color active.

Thus, is there a way to solve this?
Can this be done?

Looking for your feedbacks.
Thank you for your time.

PS: Last edit. Of course, mine was simply an attempt, and most probably not the smartest one. I put the code simply because I was asked to give a MWE. What is relevant is the question in the second paragraph!

Best Answer

Is this what you want?

\documentclass{article}

\usepackage{amsmath}
\usepackage{amsthm}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage[usenames,dvipsnames]{xcolor}
\newtheorem{theorem}{Theorem}
\usepackage{varioref}
\usepackage{hyperref}
\usepackage[noabbrev,capitalise,nameinlink]{cleveref}
\hypersetup{colorlinks={true},linkcolor={blue},citecolor=green}

\newcommand{\mref}[3][red]{\hypersetup{linkcolor=#1}\cref{#2}{#3}\hypersetup{linkcolor=blue}}%<<<changed

\begin{document}
\section{Bla}
\label{sec:bla}
\begin{theorem}
\label{th:gauss}
Gauss
\end{theorem}

\section{blabla}

In section \ref{sec:bla} we found \mref{th:gauss}

And again \ref{sec:bla} we found \mref{th:gauss}

\end{document}

enter image description here

Related Question