[OS X TeX] OT: setting the look of hyperlinks with hyperref

Bruno Voisin bvoisin at mac.com
Wed Mar 31 19:47:56 CEST 2004

In despair ;-) I'm turning back again to the list, after two days 
posting to comp.text.tex and seeing nothing useful turning up. Sorry 
for the OT.

I'm still looking for a way to get underlined coloured hyperlinks in 
the PDF output of hyperref (not the framed box, displayed by Acrobat 
only, which doesn't print, but instead a line showing up in Preview.app 
and in the printed output).

hyperref.sty has two options colorlinks and frenchlinks which yield 
colored or small caps links, respectively. They are defined essentially 
though two hooks \Hy at colorlink and \Hy at endcolorlink, starting and 
ending up the links respectively, as:

% for colorlinks
   \ifHy at colorlinks
     \def\Hy at colorlink#1{\begingroup\color{#1}}%
     \def\Hy at endcolorlink{\endgroup}%
% for frenchlinks
     \ifHy at frenchlinks
       \def\Hy at colorlink#1{\begingroup\fontshape{sc}\selectfont}%
       \def\Hy at endcolorlink{\endgroup}%
% otherwise
       \def\Hy at colorlink#1{\begingroup}%
       \def\Hy at endcolorlink{\endgroup}%

Both coloured and small caps switches rely on being declarations, 
\color{#1} and \fontshape{sc}\selectfont. (Apparently this way of 
proceeding, through two hooks \Hy at colorlink and \Hy at endcolorlink, is 
introduced so as to accomodate for the very different ways of creating 
the appropriate PDF code depending on the driver used, see eg 
hpdftex.def for pdfTeX and pdfmark.def for dvips.)

Alas all underlining commands that I know, LaTeX's \underline, ulem's 
\uline and soul's \ul, are commands taking arguments, not declarations.

Is there a way to turn such LaTeX command into a declaration? I've 
tried stuff like:

   \def\Hy at colorlink#1{\begingroup\color{#1}\uline\bgroup}%
   \def\Hy at endcolorlink{\egroup\endgroup}%

with all the macro trickery I could think of (introduction here and 
there of \expandafter, \relax, \aftergroup, \futurelet, 
\afterassignment), with no luck. Something which works is:

\def\Hy at colorlink#1{\begingroup\color{#1}\setbox\linkbox=\hbox\bgroup}%
   \def\Hy at endcolorlink{\egroup\uline{\copy\linkbox}\endgroup}%

However, because this transforms the link into a hbox, the underlined 
link can't be broken across several lines, which is precisely the 
advantage of ulem's \uline over LaTeX's \underline.

ulem introduces also a macro \useunder, in the form say 
\useunder{\uline}{\declul}{\commul}, creating out of any macro -- here 
\uline -- a declarative form \declul say and a command form (ie 
requiring an argument) \commul say, but it includes very tricky 
programming and does not work indeed when introduced in \Hy at colorlink 
and \Hy at endcolorlink (it produces an error message).

I've found a way of getting coloured underlined links finally, by 
introducing manual underlines in all the link-creating macros I was 
using, namely for implicit links and those created by \href and 

\def\hyper at link@[#1]#2#3#4{%
   \protected at edef\Hy at tempa{#2}%
   \ifx\Hy at tempa\@empty
     \hyper at link{#1}{#3}{\uline{#4}}%
     \expandafter\hyper at readexternallink#2\\{#1}{#3}{\uline{#4}}%

for links created by \url:

\def\url@#1{\hyper at linkurl{\uline{\Hurl{#1}}}{#1}}

and for links in the table of contents, lists of tables and figures:

     \csname l@#1\endcsname{#2}{#3}%
     \ifHy at linktocpage
       \csname l@#1\endcsname{{#2}}{%
         \hyper at linkstart{link}{#4}{\uline{#3}}\hyper at linkend
       \csname l@#1\endcsname{%
         \hyper at linkstart{link}{#4}{\uline{#2}}\hyper at linkend

but that's not very elegant compared with a solution, similar to the 
colorlinks and frenchlinks switches, which would only involve one 

Bruno Voisin

Please see <http://www.esm.psu.edu/mac-tex/> for list
guidelines, information, and LaTeX/TeX resources.

More information about the macostex-archives mailing list