Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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.

share|improve this question

2 Answers 2

up vote 17 down vote accepted

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}
share|improve this answer
    
I applied this procedure but after replacing href with MYhref (no matter if I define a color or not), instead of looking in the internet, it tries to open a file and it writes: "Go to file "Google.pdf" " for example –  makhlaghi Jun 24 '12 at 8:47
    
@astroboy: Opps.. there was a typo in the definition of \MYhref- the first parameter to \href was wrong. Try with the new definition. –  Peter Grill Jun 24 '12 at 8:50
    
Yes, now it is working perfectly. Thanks a lot.... –  makhlaghi Jun 24 '12 at 8:58

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

share|improve this answer
    
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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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