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

3
  • 1
    Oddly enough, the correct colour is maintained with \url links, just not with \href links.
    – Alan Munn
    Commented Jul 1, 2011 at 4: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
    Commented Jul 1, 2011 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
    Commented Jul 1, 2011 at 14:42

2 Answers 2

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.

3
  • Nicer. :-) I didn't hunt hard enough into hycolor.sty.
    – Alan Munn
    Commented Jul 1, 2011 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. Commented Jul 5, 2011 at 3:39
  • Working now that I have up-to-date texlive. Thank you! Commented Aug 29, 2011 at 5: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

14
  • 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
    Commented Jul 1, 2011 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
    Commented Jul 1, 2011 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). Commented Jul 1, 2011 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
    Commented Jul 5, 2011 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
    Commented Jul 11, 2011 at 4:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .