# How to use different colors for different \href commands?

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}
urlcolor  = blue,
citecolor = blue,
anchorcolor = blue]{hyperref}
\begin{document}
\end{document}


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:

## Code:

\documentclass{article}
\usepackage{xcolor}
urlcolor  = blue,
citecolor = blue,
anchorcolor = blue]{hyperref}

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

\begin{document}

\end{document}

• 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
• @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
• 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

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

Example:

\documentclass{article}
urlcolor  = blue,
citecolor = blue,
anchorcolor = blue]{hyperref}

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

\begin{document}

\end{document}


Result:

• 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

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.