1 % The UCW Macro Collection: References
2 % Written by Martin Mares <mj@ucw.cz> in 2018 and placed into public domain
3 % -------------------------------------------------------------------------
5 % Should clickable links be produced?
10 % We maintain a collection of objects. Each object has:
11 % - type (namespace -- e.g., chapter)
12 % - identifier (alphanumeric string used to refer to the object in TeX source)
13 % - label (to be typeset in clickable links -- e.g., chapter number)
14 % - location (it is tied to a specific place in the output document)
16 % Some output formats (e.g., EPUB) have output split to multiple files.
17 % We need to keep track of the file name for every object.
18 % When producing a PDF, it is always empty.
21 % An auxiliary file to which we write definitions of all identifiers
23 \def\writeid#1#2{\immediate\write\ids{\string\iddef{#1}{#2}{\refcurrentfile}}}
24 \def\delayedwriteid#1#2{\write\ids{\string\iddef{#1}{#2}{\refcurrentfile}}}
26 % Used in auxiliary files
27 \def\iddef#1#2#3{\ifcsname id:#1\endcsname\immediate\write16{*** Warning: Identifier #1 re-defined ***}\else
28 \expandafter\def\csname id:#1\endcsname{#2}%
29 \expandafter\def\csname ff:#1\endcsname{#3}%
32 % Read the auxiliary file from the previous run of TeX, create a new one
33 \immediate\openin\ids=\jobname.ids.aux
38 \immediate\closein\ids
39 \immediate\openout\ids=\jobname.ids.aux
41 % Define an object. It is called from macros with optional arguments
42 % as \addid{type}{label}. It defines a new object whose identifier is the
44 % It sets \currentid to either ID prefixed by its type, or to \relax.
49 \edef\currentid{#1\optarg}%
50 \writeid{\currentid}{#2}%
52 \pdfdest name {\currentid} xyz\relax
57 % A low-level interface for typesetting references: produces a clickable link
58 % to the identifier #2 of type #1 with a label translated by macro #3 (use \relax
59 % for no translation).
60 \def\reflink#1#2#3{\expandafter\ifx\csname id:#1#2\endcsname\relax
62 \immediate\write16{*** Warning: Reference #1 undefined ***}%
65 \pdfstartlink\commonlinkargs goto name {#1}\relax
67 #3{\csname id:#1#2\endcsname}%
74 % Common style of all clickable links
76 \def\commonlinkargs{height 8pt depth 2pt attr {/C [0 0 0.5] /Border [0 0 2]}}
78 % Typeset a link to identifier #2 of type #1
79 \def\ref#1#2{\reflink{#1}{#2}\relax}
81 % Define an identifier of type page pointing to the current page
82 \def\pageid#1{\delayedwriteid{page#1}{\the\count0}\ifclickable\pdfdest name {page#1} fit\relax\fi}
84 % Typeset a reference to the given page number (used in tables of contents and indices,
85 % where we know the page number from other sources).
86 \def\pagelink#1{\ifclickable\pdfstartlink\commonlinkargs goto page #1 {/Fit}\relax #1\pdfendlink\else #1\fi}
88 % Typeset a clickable URL
89 % Currently, all weird characters must be properly escaped.
92 \pdfstartlink\commonlinkargs user {/Subtype/Link /A << /Type/Action /S/URI /URI(#1) >>}\relax
99 \def\xurl#1:#2#3^^X{{\I #1:#2\ifx#2/\kern-0.1em\fi#3}}