# \ref different colors for figures, equations, etc

How can I have different colors for \ref{} for figures and equations? My figures have label \label{fig:name} whereas equations are just \label{name}. Is there a way I can set the color to be different depending on whether the argument has a "fig:"?

• I think it would ba a bad precedent to base any formatting on the form of the label however packages like cleverref, nameref are already classifying the reference type (depending on the counter than was used for the \label) to change the formatting eg prefixing "Figure" or use () etc so they could easily be customised to add colour at the same time. – David Carlisle Nov 24 '16 at 9:13
• Can you please add a MWE? – user36296 Nov 24 '16 at 15:50
• Since you have some responses below that seem to answer your question, please consider marking one of them as ‘Accepted’ by clicking on the tickmark below their vote count (see How do you accept an answer?). This shows which answer helped you most, and it assigns reputation points to the author of the answer (and to you!). It's part of this site's idea to identify good questions and answers through upvotes and acceptance of answers. – user36296 Dec 27 '17 at 21:26

Please look for a non-cleveref version at the end of the answer.

The cleveref package allows for versatile configurations of the cross-reference format, with \crefformat{figure}{#2#1#3} where #2 and #3 are reserved for hyperanchor settings and should not be interchanged, whereas #1 contains the content to be typeset, so font switches or \color commands are possible here.

\documentclass{article}
\usepackage{xcolor}
\usepackage{hyperref}
\usepackage{cleveref}

\crefformat{figure}{#2{\color{blue}#1}#3}
\Crefformat{figure}{#2{\color{blue}#1}#3}

\crefformat{equation}{#2{\bfseries\color{violet}#1}#3}
\Crefformat{equation}{#2{\itshape\color{violet}#1}#3}

\begin{document}

See \cref{foofigure} or \cref{fooequation} or \Cref{fooequation}

\begin{figure}
\caption{foofigure}\label{foofigure}
\end{figure}

$$E=mc^{2} \label{fooequation}$$

\end{document}


Version with the traditional p@figure etc. cross-reference format:

\documentclass{article}
\usepackage{xcolor}
\usepackage{hyperref}

\makeatletter
\def\p@figure{\color{blue}}
\def\p@equation{\itshape\color{violet}}
\makeatother

\begin{document}

See \ref{foofigure} or \ref{fooequation}

\begin{figure}
\caption{foofigure}\label{foofigure}
\end{figure}

$$E=mc^{2} \label{fooequation}$$

\end{document}


The output is basically the same as in the screen shot from the other version, hyperlink frames are not shown in the image.