61

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.

58

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}
  • 1
    on my computer, blue is coming out more like Cyan, and not as shown here; example. is there a way to fix this? – user5359531 Jan 17 '18 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. – Peter Grill Jan 18 '18 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. – user5359531 Jan 18 '18 at 18:29
23

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

  • 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. – Peter Grill Jun 24 '12 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. – Claudio Fiandrino Jun 24 '12 at 8:30
18

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.

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.