3

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?

3

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).
2

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}
\colorlet{links}{blue}
\hypersetup{
colorlinks=true,
urlcolor=links
}
\begin{document}

\url{hhh}

\colorlet{links}{red}

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

1

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

\documentclass{article}

\usepackage[colorlinks]{hyperref}

\hypersetup{urlcolor=blue}

\begin{document}

\color{red}{This is red text, \href{http://example.com}{but the links are still blue}.}

\href{http://example.com}{\color{red}{This is a red link.}}

\end{document}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.