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

Bruno Voisin bvoisin at mac.com
Wed Mar 31 12:47:56 EST 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:

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

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:

\AtBeginDocument{%
   \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:

\newbox\linkbox
\AtBeginDocument{%
   
\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 
\hyperlink:

\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}}%
   \else
     \expandafter\hyper at readexternallink#2\\{#1}{#3}{\uline{#4}}%
   \fi
}

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:

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

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

Bruno Voisin

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





More information about the MacOSX-TeX mailing list