11

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

  • 1
    Oddly enough, the correct colour is maintained with \url links, just not with \href links. – Alan Munn Jul 01 '11 at 04:59
  • 2
    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 01 '11 at 08: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 01 '11 at 14:42

2 Answers2

8

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.

egreg
  • 1,121,712
  • Nicer. :-) I didn't hunt hard enough into hycolor.sty. – Alan Munn Jul 01 '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. – Kit Johnson Jul 05 '11 at 03:39
  • Working now that I have up-to-date texlive. Thank you! – Kit Johnson Aug 29 '11 at 05:40
3

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

Alan Munn
  • 218,180
  • 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 01 '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 01 '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). – خالد حسني Jul 01 '11 at 17:16
  • @Khaled That was my understanding too, which was why I originally had \newfontfamily. So is there a non-expensive way to add the colour which will work for both \sffamiy and rmfamily (which is what motivated egreg's suggestion)? – Alan Munn Jul 01 '11 at 18:39
  • This looks great. I don't know why though, but I can't make it work on my machine. I copied and pasted your code directly to a blank file, ran xelatex on that file, and opened the resulting pdf. The hyperlink text is not nice and orange like yours, but black. The only way I can get it to go orange is by commenting out the line \setmainfont[Color=393939]{Linux Libertine O}. What's going wrong? – Kit Johnson Jul 05 '11 at 03:37
  • 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 05 '11 at 04:39
  • @Alan Munn, thank you. 2009/10/09 v6.79a. That looks horribly old. Let me work out how to update that. – Kit Johnson Jul 06 '11 at 02:33
  • @alan-munn It's taken me a week to update that package because doing so broke several others. Finally got there: hyperref.sty 2011/04/09 v6.82f But still no coloured links! : ( – Kit Johnson Jul 11 '11 at 03:38
  • 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 04:27
  • @alan-munn Just parts of it. I couldn't find a way to update all of it. I posted in the Ubuntu forums but didn't any replies. I'll go back there to find a way to get it all updated properly. – Kit Johnson Jul 11 '11 at 05:00
  • @oldmankit Try the answers to this question here: Vanilla TeXLive on Debian or Unbuntu – Alan Munn Jul 11 '11 at 05:03
  • @alan-munn Thanks very much for the link. I can see it will be quite a job, since Ubuntu uses an outdated texlive version, and there may be conflicts and broken dependencies, and also it needs more bandwidth than my mobile phone Edge connection will allow. I'll have to wait until I get somewhere with broadband...! – Kit Johnson Jul 12 '11 at 04:32
  • Finally, done! I got the entire texlive 2011 through bit torrent; it took several nights to download it. I've installed it and it's all working now. Wonderful! – Kit Johnson Aug 29 '11 at 05:39
  • @oldmankit I'm glad it's all working now. – Alan Munn Aug 29 '11 at 06:20