4

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!

4
  • Welcome to TeX.SX! Please help us to help you and add a minimal working example (MWE) that illustrates your problem. It will be much easier for us to reproduce your situation and find out what the issue is when we see compilable code, starting with \documentclass{...} and ending with \end{document}.
    – user31729
    Commented May 8, 2015 at 12:57
  • Hello Christian. Thank you for the welcome. Actually, I did not add a MWE for the simple reason that I thought my problem was general enough to be addressed without a specific example. Anyway, I will edit the post.
    – Kolmin
    Commented May 8, 2015 at 13:02
  • We appreciate a starter ;-)
    – user31729
    Commented May 8, 2015 at 13:03
  • Sorry if it took that much time, but I am really having some troubles with the Internet.
    – Kolmin
    Commented May 8, 2015 at 13:38

1 Answer 1

4

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

3
  • I don't use cref myself, so not sure if you really want that 3rd argument in your MWE. Commented May 8, 2015 at 18:16
  • I started to use it yesterday, so... but apparently (by reading around) it seems you cannot really get rid of it. However, I am not really sure.
    – Kolmin
    Commented May 9, 2015 at 8:56
  • If I try to apply mref to equations or sections it does not work properly. Any idea on how to fix it?
    – Star
    Commented Mar 1, 2017 at 14:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .