Two colors in one hyperlink

As usual writing a few documents. As one can see having too much color and gloss in a document is never good. Therefore having boxes, and a myriad of colors in the hyperlinks is a bad idea. I really like the format Type (#1) for my links as seen below. Now my question is

• Is it possible to make the whole link into a hyperlink, but only color the number?

I want to have the equation (1) format, but only the number should be colored and the whole thing should be a hyperlink. Is this possible? Also is it possible to do this with custom commands? For the MWE the second hyperlink should read Format (1). To keep it consistent.

\documentclass[12pt,a4paper]{article}
\usepackage{lipsum}
\usepackage{hyperref}
\hypersetup{colorlinks=true,pdfmenubar=false,pdfstartview={FitH},linktoc=all,urlcolor=blue}

\newcounter{foo}

\newcommand{\header}{\paragraph*{\refstepcounter{foo} Format \arabic{foo} }}

\usepackage[nameinlink]{cleveref}

\crefformat{equation}{equation~(#2#1#3)}

\begin{document}

\header \label{Format1}

\lipsum[4]

$$x^2 + x + 1 = 0\,. \label{phi}$$

\Cref{phi} and \cref{Format1} \lipsum[1]

\end{document}

• the statement of the question sounds like you want the link to be even more colorful! maybe "how to limit color to only part of a hyperlink?" – barbara beeton Apr 18 '13 at 12:31

2 Answers

A slight improvement on Heiko's answer is to customise \labelformat' instead of\crefformat. Then you don't need to redefine all of\crefformat,\crefmultiformat,\crefrangeformatand\labelcrefformatto avoid breakingcleveref's multi-reference features:

\documentclass[12pt,a4paper]{article}
\usepackage{lipsum}
\usepackage{hyperref}
\hypersetup{
colorlinks=true,
pdfmenubar=false,
pdfstartview={FitH},
linktoc=all,
urlcolor=blue,
linkcolor={},
}

\newcounter{foo}

\newcommand{\header}{%
\refstepcounter{foo}%
\paragraph*{Format \thefoo}%
}

\usepackage[nameinlink]{cleveref}

\creflabelformat{equation}{#2(\textcolor{red}{#1})#3}

\crefname{foo}{format}{formats}
\creflabelformat{foo}{#2(\textcolor{red}{#1})#3}

\begin{document}

\header \label{Format1}

\lipsum[4]

$$x^2 + x + 1 = 0\,. \label{phi}$$

\Cref{phi} and \cref{Format1} \lipsum[1]

\end{document}


Cleveref is...clever enough to still make the entire name part of the hyperlink when you use the nameinlink package option, even if you customise \creflabelformat. (This despite the fact that the \labelformat definition looks like it doesn't include the name.)

You are already formatting your links with \crefformat. Thus it is easy to add coloring. The linked text is surrounded by #2 and #3. Now the first letter is not auto-detected any more and \Crefformat is also needed to get the upper cased form. Hyperref's coloring for inner document links is disabled by linkcolor={}.

The example also fixes the issues with counter foo and its reference:

\documentclass[12pt,a4paper]{article}
\usepackage{lipsum}
\usepackage{hyperref}
\hypersetup{
colorlinks=true,
pdfmenubar=false,
pdfstartview={FitH},
linktoc=all,
urlcolor=blue,
linkcolor={},
}

\newcounter{foo}

\newcommand{\header}{%
\refstepcounter{foo}%
\paragraph*{Format \thefoo}%
}

\usepackage[nameinlink]{cleveref}

\crefformat{equation}{#2equation~(\textcolor{red}{#1})#3}
\Crefformat{equation}{#2Equation~(\textcolor{red}{#1})#3}

\crefformat{foo}{#2format~(\textcolor{red}{#1})#3}
\Crefformat{foo}{#2Format~(\textcolor{red}{#1})#3}

\begin{document}

\header \label{Format1}

\lipsum[4]

$$x^2 + x + 1 = 0\,. \label{phi}$$

\Cref{phi} and \cref{Format1} \lipsum[1]

\end{document}
`