# Customize colours with \ref

I want to create a command alternative to \ref to distinguish links between 2 different colours (say, black and red).

In my code below I have simply use the answer here and replaced \href with \ref. It does not work but I don't know how to fix it.

\documentclass[10 pt,a4paper,oneside,openany, notitlepage]{article}
\usepackage[usenames,dvipsnames]{xcolor}
\usepackage{amsmath, amssymb, graphics}

urlcolor  = black,
citecolor = black,
anchorcolor = black]{hyperref}

\newcommand\MYref[3][red]{\ref{#2}{\color{#1}{#3}}}% %NEW COMMAND ALTERNATIVE TO REF

\begin{document}

\section{Title}
\label{blah}
$$\label{eq1} A=B$$

See Section \MYref{blah2}

See Equation \ref{eq1} or \MYref{eq1}

\newpage
\section{Title2}
\label{blah2}

\end{document}


What I want is the link (to an equation, section, subsection, figure, etc) displaying in red when I use \Myref and displaying in black when I use \ref.

• Your setup is confusing: You define \MYref as taking 3 arguments (one being optional) in the preamble, yet to invoke the macro with just 1 argument in the body of the document. Please clarify what you're trying to achieve. – Mico Mar 1 '17 at 15:21
• As Mico says, what are you trying to do and why have you removed the color of the hyperlink? If you just want certain hyplerlinked references to be colored, then that can easily be done via local color redefinitions. But please explainwhat exactly you are trying to do – daleif Mar 1 '17 at 15:25
• Your not using the 3rd argument actually and article does not know the openany option – user31729 Mar 1 '17 at 15:38

Use the \hypersetup{linkcolor=red} command in a \begingroup...\endgroup pair within your \MYref pair.

I've used xparse to provide the 3rd. optional argument which is not given here at all in the examples.

\documentclass[10 pt,a4paper,oneside, notitlepage]{article}
\usepackage[usenames,dvipsnames]{xcolor}
\usepackage{amsmath, amssymb, graphics}

urlcolor  = black,
citecolor = black,
anchorcolor = black]{hyperref}

\usepackage{xparse}

\NewDocumentCommand{\MYref}{O{red}mo}{%
\begingroup
\ref{#2}%
\IfValueT{#3}{%
\color{#1}{#3}%
}%
\endgroup
}% %NEW COMMAND ALTERNATIVE TO REF

\newcommand\MYreforig[3][red]{\begingroup\hypersetup{linkcolor=#1}\ref{#2}{\color{#1}{#3}}\endgroup}% %NEW COMMAND ALTERNATIVE TO REF

\begin{document}

\section{Title}
\label{blah}
$$\label{eq1} A=B$$

See Section \MYref{blah2}

See Equation \ref{eq1} or \MYref{eq1}

\newpage
\section{Title2}
\label{blah2}

\end{document}

• perhaps easier to just change the color, \colorlet{linkColor}{black} and set linkcolor = linkColor in the preamble. Then \colorlet{linkColor}{#1} in the macro instead of the \hypersetup (BTW shouldn't linkcolor=red be linkcolor=#1 in your code?) – daleif Mar 1 '17 at 15:55
• @daleif: Yes, it should be #1 instead of red, that was too fast by me. The idea with \colorlet is very nice, in fact. – user31729 Mar 1 '17 at 15:57
• @ChristianHupfer the code above creates problem when used in footnotes and when used with figures – user3285148 Mar 1 '17 at 16:09
• @user3285148: Which problems? There are no footnotes or figures in your example file – user31729 Mar 1 '17 at 16:11
• Apologies, I have just seen the new \MYref which works perfectly also with footnotes and figures. \MYreforig when used inside a footnote does not put a space after the link (the same with figures). Thank you – user3285148 Mar 1 '17 at 16:16