| 1 |
%% This is part of the OpTeX project, see http://petr.olsak.net/optex |
| 2 |
|
| 3 |
\_codedecl \ulink {Hyperlinks <2026-05-19>} % preloaded in format |
| 4 |
|
| 5 |
\_doc ---------------------------- |
| 6 |
\`\dest``[<type>:<spec>]` creates a destination of internal links. The |
| 7 |
destination is declared in the format `<type>:<spec>`. If the \^`\hyperlinks` |
| 8 |
command in not used, then `\dest` does nothing else it is set to `\_destactive`. |
| 9 |
The \`\_destactive` is implemented by `\_pdfdest` primitive. It creates a |
| 10 |
box using \`\_destbox``[<type>:<spec>]` |
| 11 |
in which the destination is shifted by \`\_destheight`. The reason is that |
| 12 |
the destination is exactly at the top border of the PDF viewer but we want to see |
| 13 |
the line where the destination is. The destination box is positioned |
| 14 |
differently dependent on the current vertical or horizontal mode. |
| 15 |
\_cod ---------------------------- |
| 16 |
|
| 17 |
\_def\_destheight{1.4em} |
| 18 |
\_def\_destactive[#1:#2]{\_if$#2$\_else\_ifvmode |
| 19 |
\_tmpdim=\_prevdepth \_prevdepth=-1000pt |
| 20 |
\_destbox[#1:#2]\_prevdepth=\_tmpdim |
| 21 |
\_else \_destbox[#1:#2]% |
| 22 |
\_fi\_fi |
| 23 |
} |
| 24 |
\_def\_destbox[#1]{\_vbox to\_zo{\_kern-\_destheight \_pdfdest name{#1} xyz\_vss}} |
| 25 |
\_def\_dest[#1]{} |
| 26 |
\_public \dest ; |
| 27 |
|
| 28 |
\_doc ---------------------------- |
| 29 |
Each hyperlink is created internally by \`\_xlink``{<type>}{<spec>}{<color>}{<text>}`. |
| 30 |
This macro expands to `\_quitvmode{<text>}` by default, i.e.\ no active |
| 31 |
hyperlink is created, only <text> is printed in horizontal mode (and in a group). |
| 32 |
If \^`\hyperlinks` is used, then `\_xlink` gets the meaning of |
| 33 |
\`\_xlinkactive` and hyperlinks are created by the `\pdfstartlink`/% |
| 34 |
`\pdfendlink` primitives. The <text> has given <color> only when |
| 35 |
hyperlink is created. If `\_<type>linkcolor` is defined, it has |
| 36 |
precedence over <color>. |
| 37 |
\nl |
| 38 |
The \`\_linkdimens` macro declares the dimensions of link area. |
| 39 |
\nl |
| 40 |
A specific action can be defined for each link <type> by the macro |
| 41 |
`\_<type>action{<spec>}`. \OpTeX/ defines only \`\_urlaction``{<url>}`. |
| 42 |
The default link action (when `\_<type>action` is not defined) is |
| 43 |
`goto name{<type>:<spec>}` (an internal link). It is declared in the |
| 44 |
\`\_linkactions``{<type>}{<spec>}` macro. |
| 45 |
\nl |
| 46 |
The `\_pdfstartlink` primitive uses `attr{\_pdfborder{<type>}}`. The |
| 47 |
\`\_pdfborder``{<type>}` macro expands to `/C[? ? ?] /Border[0 0 .6]` if the |
| 48 |
`\_<type>border` macro (i.e.\ \`\_refborder`, \`\_citeborder`, \`\_tocborder`, |
| 49 |
\`\_pgborder`, \`\_urlborder`, \`\_fntborder` or \`\_fnfborder`) |
| 50 |
is defined.\nl |
| 51 |
The \^`\_xlink` is surrounded by the pair \`\_startxlink` and \`\_stopxlink`. |
| 52 |
Their default values are `\pdfrunninglinkon` and `\pdfrunninglinkoff`, |
| 53 |
i.e.\ the link can be broken to lines and pages. These primitives are used |
| 54 |
in output routine too in order to suppress the active area of the link |
| 55 |
in headline and footline when the link is broken to more pages. |
| 56 |
But the case of a broken link to more pages AND broken another links |
| 57 |
to more lines or pages in footnotes at the same page doesn't work correctly. |
| 58 |
This very specific case should be solved by \LuaTeX/ attributes. |
| 59 |
For the sake of simplicity, \OpTeX/ does not handle this now. |
| 60 |
\_cod |
| 61 |
|
| 62 |
\_protected\_def\_xlinkactive#1#2#3#4{\_quitvmode |
| 63 |
\_startxlink \_pdfstartlink \_linkdimens attr{\_pdfborder{#1}}\_linkactions{#1}{#2}\_relax |
| 64 |
{\_localcolor\_trycs{_#1linkcolor}{#3}#4}\_pdfendlink \_stopxlink |
| 65 |
} |
| 66 |
\_protected\_def\_xlink#1#2#3#4{\_quitvmode{#4}} |
| 67 |
|
| 68 |
\_def\_linkdimens{height.9em depth.3em} |
| 69 |
|
| 70 |
\_def\_linkactions#1#2{\_ifcsname _#1action\_endcsname |
| 71 |
\_lastnamedcs{#2}\_else goto name{#1:#2}\_fi} |
| 72 |
\_def\_urlaction #1{user{/Subtype/Link/A <</Type/Action/S/URI/URI(#1)>>}} |
| 73 |
|
| 74 |
\_def\_pdfborder#1{\_ifcsname _#1border\_endcsname |
| 75 |
/C [\_csname _#1border\_endcsname] /Border [0 0 .6]\_else /Border [0 0 0]\_fi |
| 76 |
} |
| 77 |
|
| 78 |
\let\_startxlink=\_pdfrunninglinkon \let\_stopxlink=\_pdfrunninglinkoff |
| 79 |
|
| 80 |
|
| 81 |
\_doc ------------------------------ |
| 82 |
\`\link``[<type>:<spec>]{<color>}{<text>}` creates a link. It is |
| 83 |
kept here for backward compatibility and it is |
| 84 |
equivalent to \^`\_xlink{<type>}{<spec>}{<color>}{<text>}`. |
| 85 |
If `\_<type>action` is not defined then |
| 86 |
`\link` creates internal link do the \^`\dest[<type>:<spec>]`. |
| 87 |
You can have more links with the same `<type>:<spec>` but only one \^`\dest` |
| 88 |
in the document. |
| 89 |
\nl |
| 90 |
\`\ilink``[<type>:<spec>]{<text>}` is equivalent to \^`\link` but |
| 91 |
the `<color>` is used from \^`\hyperlinks` declaration |
| 92 |
(or it is overwritten by `\def\_<type>linkcolor`). |
| 93 |
\nl |
| 94 |
\`\ulink``[<url>]{<text>}` creates external link. |
| 95 |
The `<url>` is detokenized with `\escapechar=-1` before it is used, so |
| 96 |
`\%`, `\#` etc. can be used in the `<url>`. |
| 97 |
\_cod ---------------------------- |
| 98 |
|
| 99 |
\_def\_link[#1:#2]{\_xlink{#1}{#2}} |
| 100 |
\_def\_ilink[#1:#2]#3{\_xlink{#1}{#2}\_ilinkcolor{#3}} |
| 101 |
\_def\_ulink[#1]#2{{\_escapechar=-1 \_ea}\_expanded |
| 102 |
{\_noexpand\_xlink{url}{\_detokenize{#1}}}\_elinkcolor{#2}} |
| 103 |
|
| 104 |
\_public \ilink \ulink \link ; |
| 105 |
|
| 106 |
\_doc ---------------------------- |
| 107 |
\`\hyperlinks``<ilink color><ulink color>` activates `\dest`, `\xlink`, |
| 108 |
so that they create links. Not setting colors (`\hyperlinks{}{}`) is also |
| 109 |
supported. |
| 110 |
\_cod ---------------------------- |
| 111 |
|
| 112 |
\_def\_hyperlinks#1#2{% |
| 113 |
\_let\_dest=\_destactive \_let\_xlink=\_xlinkactive |
| 114 |
\_let\_ilinkcolor=#1\_empty |
| 115 |
\_let\_elinkcolor=#2\_empty |
| 116 |
\_public \dest \xlink ;% |
| 117 |
} |
| 118 |
\_public \hyperlinks ; |
| 119 |
|
| 120 |
\_doc ---------------------------- |
| 121 |
\`\url``{<url>}` does approximately the same as \^`\ulink``[<url>]{<url>}`, but |
| 122 |
more work is done before the `\ulink` is processed. The link-version of <url> |
| 123 |
is saved to `\_tmpa` and the printed version in `\_tmpb`. The printed |
| 124 |
version is processed in four steps: 1.~the `\|` are replaced by `[||]` (we |
| 125 |
suppose that such string does not exist in any URL). 2.~it is detokenized with |
| 126 |
`\escapechar=-1`. 3.~muti-strings and spaces are replaced by strings in |
| 127 |
braces `{...}`. 4.~internal penalties and skips are put between characters |
| 128 |
using \`\_urlA`, \`\_urlB` and \`\_urlC`. The step~4 do following: |
| 129 |
The \`\_urlxskip` is inserted between each pair of \"normal characters", |
| 130 |
i.e.\ characters not declared by `\sdef{_ur:<character>}`. |
| 131 |
The special characters declared by `\sdef{_ur:<character>}` are replaced |
| 132 |
by the body of their corresponding macro. |
| 133 |
The \`\_urlskip`, \`\_urlbskip`, \`\_urlgskip` |
| 134 |
are typical skips used for special characters, their meaning is |
| 135 |
documented in the code below. You can change them. |
| 136 |
Default values: penalty 9990 is inserted between each pair of normal |
| 137 |
characters, penalty 100 is inserted after special characters, nobreak before special |
| 138 |
characters. The URL can be broken at any place using these default |
| 139 |
values. If you want to disable breaking between normal characters, say |
| 140 |
`\let\_urlxskip=\nobreak`. |
| 141 |
\nl |
| 142 |
The text version of the `<url>` is printed in \`\_urlfont`. |
| 143 |
\_cod ---------------------------- |
| 144 |
|
| 145 |
\_def\_url#1{{% |
| 146 |
\_def\_tmpa{#1}\_replstring\_tmpa {\|}{}% |
| 147 |
\_def\_tmpb{#1}\_replstring\_tmpb {\|}{[||]}% |
| 148 |
{\_escapechar=-1 \_ea}\_ea\_edef\_ea\_tmpb\_ea{\_detokenize\_ea{\_tmpb}}% |
| 149 |
\_replstring\_tmpb{[||]}{{gb|}}% |
| 150 |
\_replstring\_tmpb{ }{{ }}% |
| 151 |
\_replstring\_tmpb{://}{{://}}% |
| 152 |
\_ea\_ulink \_ea[\_ea{\_tmpa}] {\_urlfont \_textdirection=0 \_ea\_urlA\_tmpb\_fin}% |
| 153 |
}} |
| 154 |
\_def\_urlA#1{\_ifx\_fin#1\_else \_urlC{}{#1}\_fi} |
| 155 |
\_def\_urlB#1{\_ifx\_fin#1\_else \_urlC{\_urlxskip}{#1}\_fi} |
| 156 |
\_def\_urlC#1#2{% |
| 157 |
\_ifcsname _ur:#2\_endcsname \_lastnamedcs \_ea\_ea\_ea \_urlA |
| 158 |
\_else #1#2\_ea\_ea\_ea \_urlB \_fi |
| 159 |
} |
| 160 |
\_sdef{_ur:://}{\_urlskip:\_urlskip/\_urlskip/\_urlbskip} |
| 161 |
\_sdef{_ur:/}{\_urlskip/\_urlbskip} |
| 162 |
\_sdef{_ur:.}{\_urlskip.\_urlbskip} |
| 163 |
\_sdef{_ur:?}{\_urlskip?\_urlbskip} |
| 164 |
\_sdef{_ur:=}{\_urlskip=\_urlbskip} |
| 165 |
\_sdef{_ur:-}{\_urlskip-\_urlbskip} |
| 166 |
\_sdef{_ur:&}{\_urlskip\_char`\&\_urlbskip} |
| 167 |
\_sdef{_ur:gb|}{\_urlgskip} |
| 168 |
|
| 169 |
\_def\_urlfont{\_tt} % url font |
| 170 |
\_def\_urlxskip{\_penalty9990\_hskip0pt plus0.03em\_relax} % skip between normal characters |
| 171 |
\_def\_urlskip{\_null\_nobreak\_hskip0pt plus0.1em\_relax} % skip before :// / . ? = - & |
| 172 |
\_def\_urlbskip{\_penalty100 \_hskip0pt plus0.1em\_relax} % skip after :// / . ? = - & |
| 173 |
\_def\_urlgskip{\_penalty-500\_relax} % "goodbreak" penalty generated by \| |
| 174 |
|
| 175 |
\_public \url ; |
| 176 |
|
| 177 |
\_endcode % ---------------------------------------- |
| 178 |
|
| 179 |
|
| 180 |
There are six types of internal links and one type of external link used |
| 181 |
in \OpTeX/. They are used in the format <type>:<spec>. |
| 182 |
|
| 183 |
\begitems |
| 184 |
* `ref:<label>` -- the destination is created when `\label[<label>]` is used, |
| 185 |
see also the section \ref[references]. |
| 186 |
* `toc:<tocrefnum>` -- the destination is created at chap/sec/secc titles, |
| 187 |
see also the section \ref[maketoc]. |
| 188 |
* `pg:<gpageno>` -- the destination is created at beginning of each page, |
| 189 |
see also the section \ref[output]. |
| 190 |
* `cite:<bibpart>/<bibnum>` -- the destination is created in bibliography reference, |
| 191 |
see section \ref[bib]. |
| 192 |
* `fnt:<gfnotenum>` -- link form text to footnote, see also section~\ref[fnotes]. |
| 193 |
* `fnf:<gfnotenum>` -- link from footnote to text, see also section~\ref[fnotes]. |
| 194 |
* `url:<url>` -- used by `\url` or `\ulink`, |
| 195 |
see also the end of this section. |
| 196 |
\enditems |
| 197 |
|
| 198 |
The `<tocrefnum>`, `<gpageno>`, `<bibnum>`, and `<gfnotenum>` are numbers starting |
| 199 |
from one and globally incremented by one in the whole document. The registers \^`\tocrefnum`, |
| 200 |
\^`\gpageno`, \^`\bibnum`, and \^`\_gfnotenum` are used for these numbers. |
| 201 |
|
| 202 |
When a chap/sec/secc title is prefixed by `\label[<label>]`, then both types |
| 203 |
of internal links are created at the same destination place: |
| 204 |
`toc:<tocrefnum>` and `ref:<label>`. |
| 205 |
|
| 206 |
The color for active links can be declared by `\def\_<type>linkcolor`, the |
| 207 |
border around link can be declared by `\def\_<type>border`. These macros are |
| 208 |
not declared by default, so color for active links are given only by |
| 209 |
\^`\hyperlinks` macro and borders are invisible. |
| 210 |
For example `\def\_toclinkcolor{\Red}` means that links from table of |
| 211 |
contents are in red. |
| 212 |
Another example `\def\_tocborder{1 0 0}` causes red frames in TOC |
| 213 |
(not printed, only visible in PDF viewers). |
| 214 |
|
| 215 |
|
| 216 |
\_endinput |
| 217 |
|
| 218 |
2026-05-19 \_startxlink, \_stopxlink introduced |
| 219 |
2021-08-31 \hyperlinks{}{} allowed, bug fixed |
| 220 |
2021-05-14 \_xlink introduced, \link, \ilink, \ulink re-implemented. |
| 221 |
2021-05-12 \url: triplet :// instead //, \_urlslashslash removed. |
| 222 |
2021-05-11 \url reimplemented, \_urlxskip added. |
| 223 |
2021-04-30 \url: \detokenize of \tmpb added, bug fix. |
| 224 |
2021-04-17 attr{\_pdfborder{...}} instead expanding \_pdfborder to attr{...} |
| 225 |
2021-04-02 The possibility of \def\_toclinkcolor introduced, \localcolor moved |
| 226 |
2021-01-27 \public \link added to \hyperlinks, bug fixed. |
| 227 |
2020-04-22 \| in \url: bug fixed |
| 228 |
2020-03-15 introduced |