]> mj.ucw.cz Git - ucwmac.git/blob - ucw-link.tex
Separate modules for links and references
[ucwmac.git] / ucw-link.tex
1 % The UCW Macro Collection: Destinations and links
2 % Written by Martin Mares <mj@ucw.cz> in 2018 and placed into public domain
3 % -------------------------------------------------------------------------
4
5 % Should clickable links be produced?
6 \newif\ifclickable
7 \clickabletrue
8
9 % Common style of all clickable links
10 \pdflinkmargin=1pt
11 \def\commonlinkargs{height \the\dimexpr\ht\strutbox-0.5pt\relax depth \the\dimexpr\dp\strutbox-0.5pt\relax attr {/C [0 0 0.5] /Border [0 0 2]}}
12
13 % Define a PDF destination for the current position at the page
14 \def\destpos#1{\ifclickable\pdfdest name {#1} xyz\relax\fi}
15
16 % Define a PDF destination for the current page
17 \def\destpage#1{\ifclickable\pdfdest name {#1} fit\relax\fi}
18
19 % Typeset a clickable link to the given destination
20 % \link{dest}{text}
21 \def\link#1#2{%
22         \ifclickable
23                 \pdfstartlink\commonlinkargs goto name {#1}\relax
24                 #2%
25                 \pdfendlink\relax
26         \else
27                 #2%
28         \fi
29 }
30
31 % Typeset a clickable link to the given page number
32 % (This does not use named destinations. We use it in tables of contents and indices,
33 % where absolute page numbers are known from other sources.)
34 % \linkpage{page}{text}
35 \def\linkpage#1#2{\ifclickable\pdfstartlink\commonlinkargs goto page #1 {/Fit}\relax #2\pdfendlink\else #2\fi}
36
37 % Typeset a clickable URL
38 % Currently, all weird characters must be properly escaped.
39 \def\url#1{%
40         \leavevmode
41         \ifclickable
42                 \pdfstartlink\commonlinkargs user {/Subtype/Link /A << /Type/Action /S/URI /URI(#1) >>}\relax
43         \fi
44         \xurl #1^^X%
45         \ifclickable
46                 \pdfendlink\relax
47         \fi
48 }
49 \def\xurl#1:#2#3^^X{{\I #1:#2\ifx#2/\kern-0.1em\fi#3}}