[Tex/LaTex] How to use different colors for different \href commands

colorhyperref

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.

Best Answer

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}
Related Question