[Tex/LaTex] What’s a better way to define individual link colours for a url in a macro

colorhyperreflinksmacrosurls

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}                        
\hypersetup{colorlinks=true,linkcolor=navy,urlcolor=navy}                       
\newcommand{\navylinks}{\hypersetup{linkcolor=navy,urlcolor=navy}}              
\newcommand{\bluelinks}{\hypersetup{linkcolor=cyan,urlcolor=cyan}}  

Then I've defined another command specifically for the header links, where I turn on cyan links globally, include the header text and link, and then turn on navy links again:

\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?

Best Answer

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.
  • My interpretation of "headers" are section titles inside the text, where the title is printed in larger font size and bold. But links in head lines or the table of contents use the normal text link color.

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}
\colorlet{linkcolor}{navy}
\colorlet{headerlinkcolor}{cyan}

\usepackage[colorlinks]{hyperref}[2011/02/05]% option allcolors

\hypersetup{allcolors=linkcolor}

\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}]{%
    \hypersetup{allcolors=headerlinkcolor}%
    #8%
  }%
}
\makeatother

\pagestyle{headings}

\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}}
Link inside text: \href{http://tex.stackexchange.com/}{TeX.SE}.
\tableofcontents
\end{document}

Result

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).
Related Question