# What's a better way to define individual link colours for a url in a macro?

I've got a document where I'd like two colours of links - cyan for links in headers that since it's brighter, and navy blue for links in the main body which is more subtle/less distracting. I already tried using the xcolor package and changing the text colour with textcolor, like \textcolor{cyan}{\href{url.com}{link name}}, but it doesn't override whatever link or url colour I have defined in hypersetup.

So this is what I've done:

I've defined the colour navy:

\definecolor{navy}{HTML}{2F729C}


And also imported the hyperref package and defined commands to set the global link/url colours to cyan / navy.

\usepackage{hyperref}


\newcommand{\headerlink}[1]{\bluelinks\href{#2}{#1}\navylinks}


My issue with this is that I feel like there could be a better way to do it than to set the global link colour every time I use a link macro; is there a way to define the link colour just for one link, like one would do with \textcolor{...}, that would be able to override hypersetup?

Preliminary remarks:

• I would not change the link color inside headers. I think, it is more confusing than improving the readability.
• URLs inside chapter/section/... titles are a little debatable.

Instead of different commands, I would change the behavior at such places, where the links should display differently. In case of \section (and \subsection, ...) titles, a good place is \@sect, where the eighth argument contains the section title. The seventh argument is used for the table of contents and the head lines.

\documentclass{article}

\usepackage{xcolor}
\definecolor{navy}{HTML}{2F729C}

\makeatletter
\newcommand*{\org@sect}{}
\let\org@sect\@sect
\def\@sect#1#2#3#4#5#6[#7]#8{%
\org@sect{#1}{#2}{#3}{#4}{#5}{#6}[{#7}]{%
#8%
}%
}
\makeatother

\begin{document}
% Test document that tries to put all cases in one page
\section{First chapter with URL
\href{http://tex.stackexchange.com/}{TeX.SE}}
\tableofcontents
\end{document}


Remarks:

• There is a new option allcolors (and allbordercolors), that can be used as shortcut to set all color types (link, url, file, …) to the specified color (since hyperref 2011/02/05 v6.82a).

Make sure you using the xcolor package. Then define a color you want the hyperref setup to refer to. Then in the headers simply redefine this color using, say, \colorlet{mylinkcolor}{myheaderlinkcolor}.

Here is an example

\documentclass[a4paper]{memoir}
\usepackage{xcolor,hyperref,url}
\hypersetup{
}
\begin{document}

\url{hhh}

\url{hhh}

\end{document}


(Had a look at one of my own constructions, turnes out, I did the same as you, reissued \hyperretup, but the example above show that the color is just a reference.

I would just use the \color macro inside the \href instead of outside, i.e. \href{url.com}{\color{cyan}{link name}}.

\documentclass{article}