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'm happy with the way fontspec is working for me, but have one problem. I have the command: \setmainfont[Color=393939]{URW Palladio L} which colors all the text in my document. I have darker greys for headings.

The problem is that I want all hyperlinks to be automatically colored with the color of my choice. I use \usepackage{hyperref} \hypersetup{...urlcolor=orange}

Coloring hyperlinks like this works fine if I do not have the \setmainfont[Color=393939]{URW Palladio L} command, but it does not work when it is included.

Is there a way I can color all body text and also automatically color hyperlinks?

Kit

share|improve this question
1  
Oddly enough, the correct colour is maintained with \url links, just not with \href links. –  Alan Munn Jul 1 '11 at 4:59
1  
The coloring by hyperref relies on the \color macro, which is overridden by the color specification for the font. URL work because they use a different font. –  egreg Jul 1 '11 at 8:20
    
So the solution would be to "trick" xetex into thinking the hrefs are a different font. Any ideas how that might work? –  Seamus Jul 1 '11 at 14:42
add comment

2 Answers 2

up vote 2 down vote accepted

Building again on Alan's answer, this seems to work (even better), so long as the color has been defined beforehand or it's predefined:

\documentclass{article}
\usepackage{fontspec}
\usepackage{xcolor}

\definecolor{mycolor}{rgb}{1,0.3,0.5}

\setmainfont[Color=FF0000]{Linux Libertine O}
\setsansfont[Color=00FF00]{Linux Biolinum O}
\usepackage{hyperref}
\hypersetup{colorlinks=true,urlcolor=mycolor,filecolor=orange}
\makeatletter
\def\HyColor@@@@UseColor#1\@nil{\addfontfeatures{Color=#1}}
\makeatother

\begin{document}

A hyperlink:
\href{http://tex.stackexchange.com}{TeX StackExchange}

\sffamily A hyperlink:
\href{abc}{TeX StackExchange}
\end{document}

You'll notice that the second hyperlink is colored differently because it's not a URL and filecolor is used.

share|improve this answer
    
Nicer. :-) I didn't hunt hard enough into hycolor.sty. –  Alan Munn Jul 1 '11 at 16:02
    
Thank you so much for all this support. I don't even understand what you're doing. Regardless, it is not working for me, the same problem as above (hyperlinks coming out black, but I do have the red and green colours working before the links). I must be doing something very basic wrong here. –  oldmankit Jul 5 '11 at 3:39
    
Working now that I have up-to-date texlive. Thank you! –  oldmankit Aug 29 '11 at 5:40
add comment

Building on Seamus' suggestion in the comments (and edited to include egreg's suggestion) into an answer, here's what seems like a solution:

I redefine the relevant part of the \href command to add the orange colour in the links. I've left the urlcolor in the \hypersetup command to get the color for plain \url links.

\documentclass{article}
\usepackage{fontspec}
\usepackage{xcolor}
\setmainfont[Color=393939]{Linux Libertine O}
\usepackage{hyperref}
\hypersetup{colorlinks=true,urlcolor=orange}
\makeatletter
\def\Hy@href#{%
  \addfontfeatures{Color=orange}\hyper@normalise\href@
}
\makeatother
\begin{document}
A hyperlink:
\href{http://tex.stackexchange.com}{TeX StackExchange}
\end{document}

output of code

share|improve this answer
    
Nice. However, simply writing \addfontfeatures{Color=orange} instead of \hreffont seems to work the same; it doesn't require a new font family and works also in a "sans serif" context. One should probably act where urlcolor= does its business. –  egreg Jul 1 '11 at 15:24
    
Thanks, you're right, using \addfontfeatures is a better way to go. I've updated my answer. Your answer is even more general, though. –  Alan Munn Jul 1 '11 at 16:05
    
@Alan, actually defining a separate font might be a wiser choice (IIRC, \addfontfeatures defines a new font behind the scenes each time it is invoked, which can be expensive resources-wise). –  Khaled Hosny Jul 1 '11 at 17:16
1  
@oldmankit What version of hyperref are you using? Mine is 6.82g (2011/04/17). (Put \listfiles in your preamble to see what versions of the packages are loaded (the list will appear in the log file (or the console output.))) –  Alan Munn Jul 5 '11 at 4:39
1  
@oldmankit Did you update your whole distribution or just parts of it? If the latter, this may be the cause of your problem. –  Alan Munn Jul 11 '11 at 4:27
show 9 more comments

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.