summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/latex/gmiflink/gmiflink.sty
blob: 3bd01e739113e3076319b6240b8d8245cb17da81 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
%
% \GetFileInfo{gmiflink.sty}
% \title{The \pk{gmiflink} Package\thfileinfo}
% \author{Grzegorz `Natror' Murzynowski}
% \maketitle
%
%
% \begin{copyrnote}
%
%%   Written by Grzegorz `Natror' Murzynowski,
%%   natror at o2 dot pl
%%
%% \copyright\,2005, 2006 by Grzegorz `Natror' Murzynowski.
%%
%% This program is subject to the \LaTeX\ Project Public License.
%% See ^^A
%% \url{http://www.ctan.org/tex-archive/help/Catalogue/licenses.lppl.html} ^^A
%% for the details of that license.
%%
%% LPPL status: "author-maintained".\par
%
%\end{copyrnote}
%
% \CheckSum{61}
%% 

\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{gmiflink}
     [2006/08/16 v0.97 Conditionally hyperlinking package (GM)]

%%
%% \division{Introduction, usage}
%%
%% This package protects you against an error when a link is
%% dangling and typesets some plain text instead of a hyperlink then. It
%% is intended for use with the \pk{hyperref} package. Needs
%% \emph{two} \LaTeX\ runs.
%
% I used it for typesetting the names of the objects in
% a~documentation of a~computer program. If the object had been
% defined a \cs{hyperlink} to its definition was made, otherwise a
% plain object's name was typeset.  I also use this package in
% authomatic making of hyperlinking indexes.
% \dekmedskip
%
% The package provides the macros \cs{gmiflink}, \cs{gmifref} and
% \cs{gmhypertarget} for conditional making of hyperlinks in your
% document.
%
% \newcommand*\aName{\gmiflink[@name]{\hskip-1.5pt @name}}
% \TextUsage\gmhypertarget\oarg{name}\marg{\gmiflink{text}} makes
% a~\cs{hypertarget\marg{\aName}\marg{text}} and \possfil
% a~\cs{label\marg{\aName}}.
%
% \TextUsage\gmiflink\oarg{name}\marg{text} makes
% a~\cs{hyperlink\marg{\aName}\marg{text}} to a~proper hypertarget if
% the corresponding \emph{label} exists, otherwise it typesets
% \<text>.
%
% \TextUsage\gmifref\oarg{name}\marg{text} makes a~(hyper-)
% \cs{ref\marg{\aName}} to the given label if the label exists,
% otherwise it typesets \<text>.
%
% The \raisebox{1em}[0pt]{\gmhypertarget[@name]{}}\<\hskip-1.5pt @name>
% ^^A~raisebox 'cause Acrobat sets the hypertarget at the baseline
% argument is just \<name> if the \<name>
% is given, otherwise it's \<text> in all three macros.
%
% For the example(s) of use, examine the \pk{gmiflink.sty} file, lines
% 45--58.
%
% \begin{gmlonely}
%   \subdivision{Installation}
%
%   Just put the \pk{gmiflink.sty} somewhere in the \file{texmf/\:tex/\:latex}
%   branch. Creating a~\file{texmf/\:tex/\:latex/\:gm} directory may be advisable
%   if you consider using other packages written by me.
%
%   Then you should refresh your \TeX\ distribution's files' database
%   most probably. 
% \end{gmlonely}
%
%
% \subdivision{Contents of the \pk{gmiflink.zip} archive}
%
% The distribution of the \pk{gmiflink} package consists of the
% following four files.
% \begin{verse}
%   \pk{gmiflink.sty}\\
%   \pk{README}\\
%   \pk{gmiflinkDoc.tex}\\
%   \pk{gmiflinkDoc.pdf}
% \end{verse}
%
%
% \begin{gmlonely}
%   \subdivision{Compiling the Documentation}
%
%   The last of the above files (the \pk{.pdf}, i.e., \emph{this
%     file}) is a~documentation compiled from the \pk{.sty} file by
%   running \LaTeX\ on the \pk{gmiflinkDoc.tex} file.
%   Compiling the documentation requires the packages: \pk{gmdoc}
%   (\pk{gmdoc.sty} and \pk{gmdocc.cls}), \pk{gmverb.sty},
%   \pk{gmutils.sty}, \pk{gmiflink.sty} and also some standard
%   packages: \pk{hyperref.sty}, \pk{color.sty}, \pk{geometry.sty},
%   \pk{multicol.sty}, \pk{lmodern.sty}, \pk{fontenc.sty} that should
%   be installed on your computer by default.
%
%   If you had not installed the \pk{mwcls} classes (available on
%   CTAN and present in \TeX\ Live e.g.), the result of your
%   compilation might differ a~bit from the \pk{.pdf} provided in this
%   \pk{.zip} archive in formatting: If you had not installed
%   \pk{mwcls}, the standard \pk{article.cls} class would be used.
% \end{gmlonely}
%
% \StopEventually{\NoEOF}
%
% 
% \division{The Code}

\@ifpackageloaded{hyperref}{}{\message {^^J^^J gmiflink package:
    There's no use of me without hyperref package, I end my input.^^J}\endinput}

\providecommand\empty{} 
% A~new counter, just in case
\newcounter{GMhlabel}
\setcounter{GMhlabel}{0}

% The macro given below creates both hypertarget and hyperlabel, 
% so that you may reference both ways: via |\hyperlink| and via |\ref|.
% It's pattern is the |\label| macro, see \LaTeX\ Source2e, file x, line 32.
%
% But we don't want to gobble spaces before and after.
% First argument will be a~name of the hypertarget,
% by default the same as typeset text, i.e., argument |#2|.
% \CodeDefine\gmhypertarget
\DeclareRobustCommand*\gmhypertarget{%
  \@ifnextchar{[}{\gm@hypertarget}{\@dblarg{\gm@hypertarget}}}
%^^A]

\def\gm@hypertarget[#1]#2{% ^^A
  % If argument |#1|\equals|\empty|, then we'll use |#2|, i.e., the same as
  % name of hypertarget.
  \refstepcounter{GMhlabel}% ^^A
  % we |\label{\gmht@firstpar}|
  \hypertarget{#1}{#2}%
  \protected@write\@auxout{}{%
    \string\newlabel{#1}{{#2}{\thepage}{\relax}{GMhlabel.\arabic{GMhlabel}}{}}}%
}% end of |\gm@hypertartget|.

% We define a~macro such that if the target exists, it makes
% |\ref|, else it typesets ordinary text.
% \CodeDefine\gmifref
\DeclareRobustCommand*\gmifref{\@ifnextchar{[}{\gm@ifref}{%]
    \@dblarg{\gm@ifref}}}

\def\gm@ifref[#1]#2{%
  \expandafter\ifx\csname r@#1\endcsname\relax\relax%
  #2\else\ref{#1}\fi%
}%  end of |\gm@ifref|

% \CodeDefine\gmiflink
\DeclareRobustCommand*\gmiflink{\@ifnextchar{[}{\gm@iflink}{%^^A]
    \@dblarg{\gm@iflink}}}

\def\gm@iflink[#1]#2{%
  \expandafter\ifx\csname r@#1\endcsname\relax\relax%
  #2\else\hyperlink{#1}{#2}\fi%
}% end of |\gm@iflink|
%
% It's robust because when just |\newcommand*|ed, use of |\gmiflink| in an
% indexing macro resulted in errors: |\@ifnextchar| has to be
% |\noexpand|ed in |\edef|s.

\endinput

% The old version --- all three were this way primarily.
%\begin{verbatim}
\newcommand*\gmiflink[2][\empty]{{%
  \def\gmht@test{\empty}\def\gmht@firstpar{#1}%
  \ifx\gmht@test\gmht@firstpar\def\gmht@firstpar{#2}\fi%
  \expandafter\ifx\csname r@\gmht@firstpar\endcsname\relax\relax%
  #2\else\hyperlink{\gmht@firstpar}{#2}\fi%
}}
% \end{verbatim}
% \Finale

% (For my GNU Emacs:)
%%% Local Variables: 
%%% mode: latex
%%% TeX-master: "e:/LaTeX/TeXGuru/gmiflink/gmiflinkDoc"
%%% End: