How to use different colors for different \href commands?

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}

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.


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

Tags:

Color

Hyperref