1 % The UCW Macro Collection: Destinations and links
2 % Written by Martin Mares <mj@ucw.cz> in 2018 and placed into public domain
3 % -------------------------------------------------------------------------
7 % Should clickable links be produced?
15 % Common style of all clickable links
17 \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]}}
19 % Define a PDF destination for the current position at the page
20 \def\destpos#1{\ifclickable\pdfdest name {#1} xyz\relax\fi}
22 % Define a PDF destination for the current page
23 \def\destpage#1{\ifclickable\pdfdest name {#1} fit\relax\fi}
25 % Typeset a clickable link to the given destination
29 \pdfstartlink\commonlinkargs goto name {#1}\relax
37 % Typeset a clickable link to the given page number
38 % (This does not use named destinations. We use it in tables of contents and indices,
39 % where absolute page numbers are known from other sources.)
40 % \linkpage{page}{text}
41 \def\linkpage#1#2{\ifclickable\pdfstartlink\commonlinkargs goto page #1 {/Fit}\relax #2\pdfendlink\else #2\fi}
43 % Typesetting of URLs is tricky:
45 % - They can contain various characters considered special by TeX.
46 % - We want to adjust appearance of "//", "_", "~" according to font.
47 % - We want to insert a breakpoint after "/", "?", "&".
48 % - We need the raw form of the URL for PDF links.
49 % - We cannot rely purely on changing catcodes, as we sometimes need
50 % to parse URLs given as arguments of macros.
51 % - Sometimes, it is useful to insert a manual line break or another
52 % typesetting hack to the URL.
56 % - In our front-end macros (\url, \linkurl) we switch catcodes
57 % to accept '%' and '#' as normal characters; if the macros are
58 % called indirectly, these characters must be escaped as '\%' and '\#'.
59 % - The URL is preprocessed: special characters (with their original
60 % catcode) are replaced by calls of auxiliary macros.
61 % - When producing PDF links, the auxiliary macros expand to ordinary
63 % - When typesetting the URL, the auxiliary macros expand differently.
64 % Furthermore, they can be temporarily re-defined in the \urlprefix macro.
65 % - "\\" (which is usually called to produce a line break) disappears in PDF links.
66 % - If you use \urlhack{X} in the URL, it is typeset as X, but it disappears
68 % - If you call a custom macro in the URL, you can modify its definition
69 % for typesetting in \urlprefix and for PDF links by appending to \urlplainascii.
71 % Typeset a clickable URL
72 % \url{http://example.com/}
73 \def\url{\begingroup\begingroup\allowurlchars\urlaux}
74 \def\urlaux#1{\urlauxarg{#1}\linkurlauxB{\displayurl}}
76 % Typeset a clickable link to the given URL
77 % \linkurl{http://example.com/}{text}
78 \def\linkurl{\begingroup\begingroup\allowurlchars\linkurlaux}
79 \def\linkurlaux#1{\urlauxarg{#1}\linkurlauxB}
85 \pdfstartlink\commonlinkargs user {/Subtype/Link /A << /Type/Action /S/URI /URI(\tmpb) >>}\relax
92 \endgroup % opened in \url or \linkurl
95 % Catcode '%' and '#' to 'other'
96 \def\allowurlchars{\catcode`\%=12\catcode`\#=12\relax}
99 \endgroup % opened in \url or \linkurl
100 \toks0={#1}\edef\tmpb{\the\toks0}%
101 \replacestrings{//}{\urlslashslash}%
102 \replacestrings{_}{\urlunderscore}%
103 \replacestrings{~}{\urltilde}%
104 \replacestrings{/}{\urlslash}%
105 \replacestrings{?}{\urlquestion}%
106 \replacestrings{&}{\urlamp}%
107 \replacestrings{=}{\urlequal}%
110 % Style switches at the beginning/end of an URL (feel free to re-define them)
114 % Default appearance of characters special in URLs
115 \def\urlslashslash{/\kern\urlinterslashkern/}
116 \def\urlunderscore{\_}
117 \def\urltilde{{\tt\char126}}
118 \def\urlslash{/\penalty100\relax}
119 \def\urlquestion{?\penalty100\relax}
120 \def\urlamp{\&\penalty100\relax}
121 \def\urlequal{=\penalty100\relax}
124 % Kern to place between "//" in an URL
125 \newdimen\urlinterslashkern
126 \urlinterslashkern=-0.1em
128 % Switch auxiliary macros, so special characters expand to plain ASCII characters
129 % (since we need to replace them in the expand processor, we cannot use \let for that)
130 % If you want to modify expansion of your macros, extend \urlplainascii using \appendef.
137 \lowercase{\gdef\urlplainascii{%
138 \def\urlslashslash{//}%
139 \def\urlunderscore{A}%
151 % Typeset the URL stored in \tmpb. In most cases, this is used internally by \url,
152 % but you can call it explicity from the second argument of \linkurl to typeset the current URL.
153 \def\displayurl{{\urlprefix\tmpb\urlsuffix}}