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:
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.Remarks:
allcolors
(andallbordercolors
), that can be used as shortcut to set all color types (link, url, file, …) to the specified color (sincehyperref
2011/02/05 v6.82a).