118

I have two different \href{}{} links and I want each to be a different color. In the example below they will both be blue, how can I make each link have a different color?

\documentclass{article}
\usepackage[colorlinks = true,
            linkcolor = blue,
            urlcolor  = blue,
            citecolor = blue,
            anchorcolor = blue]{hyperref}
\begin{document}
Here is \href{http://www.google.com}{Google} and \href{http://www.yahoo.com}{Yahoo!}.
\end{document}

Thanks in advance for your help.

3 Answers 3

111

You can define a custom \MYhref macro to provide a color change with an optional parameter. If you don't provide the first parameter you would get the first line (which is identical to what your code produces). The second line illustrate the how you can change the color with the optional parameter:

enter image description here

Code:

\documentclass{article}
\usepackage{xcolor}
\usepackage[colorlinks = true,
            linkcolor = blue,
            urlcolor  = blue,
            citecolor = blue,
            anchorcolor = blue]{hyperref}

\newcommand{\MYhref}[3][blue]{\href{#2}{\color{#1}{#3}}}%

\begin{document}
Here is \MYhref{http://www.google.com}{Google} and \MYhref{http://www.yahoo.com}{Yahoo!}.

Here is \MYhref[brown]{http://www.google.com}{Google} and \MYhref[red]{http://www.yahoo.com}{Yahoo!}.
\end{document}
3
  • 2
    on my computer, blue is coming out more like Cyan, and not as shown here; example. is there a way to fix this? Jan 17, 2018 at 20:42
  • 1
    @user5359531: Well, either it is an issue with your computer, or it is an issue with how you are doing things. If what is provided here does not achive the desired results, I suggest you post a new question and include a fully compilable MWE including \documentclass and the appropriate packages that reproduces the problem. If this question/answer is releavant you should reference it in the question. Jan 18, 2018 at 1:44
  • 1
    thanks, I ended up figuring out that I had to add filecolor = blue to the options; my 'test' URL was actually just the word 'foo' which it was interpreting as a file and not an actual URL. Jan 18, 2018 at 18:29
61

If you're only going to use it once or twice, it may be most simple to just use this directly:

\href{http://google.com}{\color{red}{Google}}

This is the code from the macro of Peter's answer.

0
37

One way to go is to define a command that change the color before the call to \href.

Example:

\documentclass{article}
\usepackage[colorlinks = true,
            linkcolor = blue,
            urlcolor  = blue,
            citecolor = blue,
            anchorcolor = blue]{hyperref}

\newcommand{\changeurlcolor}[1]{\hypersetup{urlcolor=#1}}       

\begin{document}
Here is \href{http://tex.stackexchange.com}{TeX.SX}, \changeurlcolor{red}\href{http://www.google.com}{Google} and \changeurlcolor{green}\href{http://www.yahoo.com}{Yahoo!}.

\end{document}

Result:

enter image description here

2
  • 1
    Interesting, did not occur to me. Combining the two methods one could use this to change the default color, and then still make use of my solution to tweak it on a per use basis. Jun 24, 2012 at 8:28
  • Yes: indeed what I noticed from your answer is that my approach does not provide a way to use the default color. I mean: once one has called \changeurlcolor that is the new standard. Your answer, instead, allows to come back to the standard. Jun 24, 2012 at 8:30

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.