The space after "s.~" (which means page number in Czech) removed because it is typed...
[csplainnat.git] / url.sty
blob016a1b14c08925ae744a0c1260de632d38ecaba0
1 % url.sty ver 3.1 15-Mar-2004 Donald Arseneau asnd@triumf.ca
2 % Copyright 1996-2003 Donald Arseneau, Vancouver, Canada.
3 % This program can be used, distributed, and modified under the terms
4 % of the LaTeX Project Public License.
6 % A form of \verb that allows linebreaks at certain characters or
7 % combinations of characters, accepts reconfiguration, and can usually
8 % be used in the argument to another command. It is intended for emailsp
9 % addresses, hypertext links, directories/paths, etc., which normally
10 % have no spaces. The font may be selected using the \urlstyle command,
11 % and new url-like commands can be defined using \urldef.
13 % Usage: Conditions:
14 % \url{ } If the argument contains any "%" or "^^", or ends with
15 % "\", it can't be used in the argument to another command.
16 % The argument must not contain unbalanced braces.
17 % \url| | ...where "|" is any character not used in the argument and not
18 % "{" or a space. The same restrictions as above except that the
19 % argument may contain unbalanced braces.
20 % \urldef\xyz\url{ }
21 % \xyz a defined-url: "\xyz" can be used anywhere, no matter what
22 % characters it contains.
23 % \DeclareUrlCommand\abc{settings}: makes \abc{ } like \url{ }
25 % See further instructions after "\endinput"
28 % default style assignments
29 \def\UrlBreaks{\do\.\do\@\do\\\do\/\do\!\do\_\do\|\do\;\do\>\do\]%
30 \do\)\do\,\do\?\do\'\do\+\do\=\do\#}%
31 \def\UrlBigBreaks{\do\:\do@url@hyp}%
32 \def\UrlNoBreaks{\do\(\do\[\do\{\do\<}%
33 % any ordinary characters that aren't usually:
34 \def\UrlOrds{\do\*\do\-\do\~\do\'\do\"\do\-}%
35 \def\UrlSpecials{\do\ {\Url@space}\do\%{\Url@percent}\do\^^M{\Url@space}%
36 \Url@force@Tilde}% package option may force faked text-ascii-tilde
38 \@namedef{Url@OT1encSpecials}{%
39 \do{\mathchar"3E }\do{\mathchar"1B }\do{\mathchar"19 }%
40 % lots more to add, but I should later link into inputenc
41 \Url@OTnonTT % more for any but cmtt
42 \UrlTildeSpecial}
44 \def\Url@OTnonTT{\do\<{\langle}\do\>{\mathbin{\rangle}}\do
45 \_{\_\penalty\@m}\do\|{\mid}\do\{{\lbrace}\do\}{\mathbin{\rbrace}}\do
46 \\{\mathbin{\backslash}}}
48 \@namedef{Url@T1encSpecials}{\do{\mu}\do{\mathchar190 }%
49 \do{\mathchar190 }\do{\mathchar247 }\do{\mathchar255 }%
50 \do{\mathchar184 }}
52 \@namedef{Url@LY1encSpecials}{\do\~{\mathchar142 }}
54 \def\UrlTildeSpecial{\do\~{\raise.6ex\hbox{\m@th$\scriptstyle\sim$}}}
55 \let\Url@force@Tilde\relax
57 \def\url@ttstyle{%
58 \@ifundefined{selectfont}{\def\UrlFont{\tt}}{\def\UrlFont{\ttfamily}}}
60 \def\url@rmstyle{%
61 \@ifundefined{selectfont}{\def\UrlFont{\rm}}{\def\UrlFont{\rmfamily}}}
63 \def\url@sfstyle{%
64 \@ifundefined{selectfont}{\def\UrlFont{\sf}}{\def\UrlFont{\sffamily}}}
66 \def\url@samestyle{\def\UrlFont{}}
68 \@ifundefined{strip@prefix}{\def\strip@prefix#1>{}}{}
69 \@ifundefined{verbatim@nolig@list}{\def\verbatim@nolig@list{\do\`}}{}
71 % \Url sets up the reading the argument verbatim if possible
72 \def\Url{%
73 \Url@movingtest
74 \ifmmode\@nomatherr$\fi %$
75 \let\do\@makeother \dospecials % verbatim catcodes
76 \catcode`{\@ne \catcode`}\tw@ % with exceptions
77 \catcode`\ =10 % allow "\url {x}"
78 \catcode`\#=6 % always # -> ##, so I can later convert ## -> #
79 \@ifnextchar\bgroup{\obeyspaces\obeylines\Url@z}\Url@y}
81 % \Url@y read arguments delimited like |...|, passing to \Url@z
82 \def\Url@y#1{\catcode`{11 \catcode`}11\obeyspaces\obeylines
83 \def\@tempa##1#1{\Url@z{##1}}\@tempa}
85 % \Url@z read arguments delimited like {...}, verbatimizes with \meaning,
86 % performs character translations, then invokes the formatter.
87 \def\Url@z#1{\toks@{#1}\edef\Url@String{\the\toks@}%
88 \edef\Url@String{\expandafter\strip@prefix\meaning\Url@String}%
89 %\if\urldebug \message{------------------------------------------------------}%
90 %\message{ \Url@String}\fi
91 % \meaning introduces spurious spaces in the text, so it would be nice
92 % to preserve the real spaces before \meaning, but that doesn't work --
93 % we can only do replacement on streams of non-syntactic characters.
94 \Url@ObeySp % may be no-op; otherwise put ordinary (12) space characters
95 %\if\urldebug \message{ \Url@String}\fi
96 % we left the catcode of # with its normal value (6) so \meaning
97 % doubles it to ##. Now we convert back to single #. This allows
98 % \url{#} to appear in the arguments of other commands, where we can't
99 % make # be an ordinary character. We make the single # active rather
100 % than `other' to support hyperref.sty.
101 \Url@acthash
102 %\if\urldebug \message{ \Url@String}\fi
103 % Also make % active to support hyperref.
104 \Url@actpercent
105 % now do any hyper referencing due to hyperref (or perform a url-def)
106 \Url@HyperHook
107 % Now do the formatting in a group (can also have \Url@HyperHook take
108 % this as an argument.
109 {\Url@FormatString}%
110 \endgroup}
112 \let\Url@ObeySp\@empty
113 \let\UrlRight\@empty
114 \let\UrlLeft\@empty
115 \let\Url@HyperHook\@empty
117 % This is the normal way to format the strings, using math mode.
118 \def\Url@FormatString{%
119 \UrlFont \Url@MathSetup
120 $\fam\z@ \textfont\z@\font
121 \expandafter\UrlLeft\Url@String\UrlRight
122 \m@th$%
123 % \if\urldebug \showlists \fi
126 \def\Url@MathSetup{%
127 \medmuskip\Urlmuskip \thickmuskip\medmuskip \thinmuskip0mu%
128 \relpenalty\UrlBigBreakPenalty \binoppenalty\UrlBreakPenalty
129 \expandafter\let\expandafter\UrlEncSpecials\csname Url@\f@encoding encSpecials\endcsname
130 \ifdim\fontdimen\thr@@\font=\z@ \let\Url@nonTT\relax \fi
131 \@tempcnta\z@ % \fam\multiply\@tempcnta\@cclvi
132 \let\do\set@mathcode \UrlOrds % ordinary characters that were special
133 \advance\@tempcnta 8192 \UrlBreaks % bin
134 \advance\@tempcnta 4096 \UrlBigBreaks % rel
135 \advance\@tempcnta 4096 \UrlNoBreaks % open
136 \let\do\set@mathact \UrlSpecials \UrlEncSpecials % active
137 \let\do\set@mathnolig \verbatim@nolig@list % prevent ligatures
140 \def\set@mathcode#1{\count@`#1\advance\count@\@tempcnta
141 %\message{Set mathcode of \string #1 (\number`#1) = \number`#1 + \number\@tempcnta. }%
142 \mathcode`#1\count@}
143 \def\set@mathact#1#2{\mathcode`#1=32768 \begingroup
144 \lccode`\~`#1\lowercase{\endgroup\def~{#2}}}
145 \def\set@mathnolig#1{\ifnum\mathcode`#1<32768
146 \begingroup\lccode`\~`#1\lowercase{\endgroup
147 \edef~{\mathchar\number\mathcode`#1_{\/}}}%
148 \mathcode`#1=32768 \fi}
150 \def\Url@movingtest{\begingroup
151 \ifx\let\HyPsd@LetCommand % we are already in a pdf string!
152 \expandafter\Url@unmove \fi % give the argument and hope for the best
153 \let\Url@moving\relax\relax\relax
154 \ifx\Url@moving\relax\else \expandafter\Url@unmove \fi
155 \endgroup}
156 \long\def\Url@unmove#1\Url@y{\endgroup \ifx\protect\relax\else\protect\Url\fi}
157 \edef\Url@moving{\csname Url Error\endcsname}
158 \expandafter\edef\Url@moving
159 {\csname url used in a moving argument.\endcsname}
160 \expandafter\expandafter\expandafter \let \Url@moving\@undefined
162 \def\urldef#1#2{\begingroup \setbox\z@\hbox\bgroup
163 \def\Url@HyperHook##1\endgroup{\Url@def{#1}{#2}}#2}
164 \def\Url@def#1#2{%
165 \toks0\expandafter{\Url@String}%
166 \protected@edef\Url@String{\Url@String}%
167 \toks1\expandafter{\Url@String}%
168 \edef\Url@String{\endgroup\egroup\endgroup\def
169 \expandafter\noexpand\csname\expandafter\@gobble\string#1 \endcsname
170 {\noexpand\Url@eitherdef{\the\toks1}{\noexpand#2{\the\toks0}}}}%
171 \Url@String
172 \edef#1{\noexpand\protect
173 \expandafter\noexpand\csname\expandafter\@gobble\string#1 \endcsname}%
175 \def\Url@eitherdef{\ifx\let\HyPsd@LetCommand % we are already in a pdf string!
176 \expandafter\@firstoftwo \else \expandafter\@secondoftwo \fi}
178 \def\urlstyle#1{\expandafter\protect\csname url@#1style\endcsname}
180 \def\DeclareUrlCommand#1#2{\def#1{\begingroup #2\Url}}
182 % Sample (and default) configuration:
184 \DeclareUrlCommand\url{}
185 \urlstyle{tt}
187 \newmuskip\Urlmuskip \Urlmuskip=0mu
188 \mathchardef\UrlBreakPenalty=\binoppenalty
189 \mathchardef\UrlBigBreakPenalty=\relpenalty
191 % picTeX defines \path, so declare it optionally:
192 \@ifundefined{path}{\DeclareUrlCommand\path{\urlstyle{tt}}}{}
194 % too many styles define \email like \address, so I will not define it.
195 % \DeclareUrlCommand\email{\urlstyle{rm}}
197 % Process LaTeX \package options
199 \let\Url@sppen\@M
200 \def\Url@space{\penalty\Url@sppen}
201 \def\do@url@hyp{}% by default, no breaks after hyphens
203 \@ifundefined{ProvidesPackage}{}{\ProvidesPackage
204 {url}[2004/03/15 \space ver 3.1 \space Verb mode for urls, etc.]
205 \DeclareOption{hyphens}{\def\do@url@hyp{\do\-}}% allow breaks after hyphens
206 \DeclareOption{obeyspaces}{\let\Url@ObeySp\relax}% a flag for later
207 \DeclareOption{spaces}{\def\Url@sppen{\UrlBreakPenalty}}
208 \DeclareOption{LY1}{}
209 \DeclareOption{T1}{}
210 \DeclareOption{allowmove}{\let\Url@moving\@empty}
211 \ProcessOptions\relax
212 \ifx\Url@ObeySp\relax % [obeyspaces] was declared
213 \begingroup \lccode`\+=32 \lowercase
214 {\endgroup \def\Url@ObeySp{\Url@Edit\Url@String{ }{+}}}
215 \def\Url@space{\penalty\Url@sppen\ }
219 \begingroup
220 % \Url@acthash: convert `other' (doubled) ## to active #
221 % \Url@actpercent: convert `other' % to active %
222 \lccode`\+=`\# \lccode`\~=`\#
223 \lowercase {\long\gdef\Url@acthash{\Url@Edit\Url@String{++}{~}%
224 \ifnum\mathcode`\#<32768 \def~{\#}\fi}}%
225 \lccode`\+=`\% \lccode`\~=`\%
226 \lowercase {\long\gdef\Url@actpercent{\Url@Edit\Url@String{+}{~}%
227 \ifnum\mathcode`\%<32768 \def~{\%}\fi}}%
228 \catcode13=12 %
229 \gdef\Url@percent{\@ifnextchar^^M{\@gobble}{\mathbin{\mathchar`\%}}}%
230 \endgroup%
232 % Edit macro #1 changing all appearances of pattern #2 to replacement #3.
233 % The replacement is recursive, so don't put the search pattern into the
234 % replacement text!
236 { \catcode`Q=3
238 \gdef\Url@Edit#1#2#3{%
239 \begingroup % \tracingall
240 \def\DOE@a{\endgroup\def#1}% Just so I can \expandafter it safely.
241 \def\DOE@b##1#2##2Q##3{\@ifblank{##2}% if finished,
242 {\DOE@c ##1Q}% then remove \@empty's and redefine macro
243 {\DOE@b##1##3##2Q{##3}}% else, re-iterate
244 }% the leading \@empty preserves braces, as does the trailing pattern:
245 \expandafter\DOE@b\expandafter\@empty #1\@empty#2Q{#3}}
246 \gdef\DOE@c#1\@empty Q{\expandafter\DOE@a\expandafter{#1}}
248 % \@ifblank (LaTeX syntax) --- checks if parameter is blank (Spaces
249 % count as blank) use \@ifblank{#1}{true case}{false case} etc
250 \long\gdef\@ifblank#1{\@Ifbl@nk#1QQ\@secondoftwo\@firstoftwo!}%
251 \long\gdef\@Ifbl@nk#1#2Q#3#4#5!{#4}
253 } % restore Q catcode
255 %\ifx\urldebug\@undefined \def\urldebug{01}\fi
257 \endinput
259 % url.sty ver 3.1 15-Mar-2004 Donald Arseneau asnd@triumf.ca
261 % This package defines "\url", a form of "\verb" that allows linebreaks,
262 % and can often be used in the argument to another command. It can be
263 % configured to print in different formats, and is particularly useful for
264 % hypertext links, email addresses, directories/paths, etc. The font may
265 % be selected using the "\urlstyle" command and pre-defined text can be
266 % stored with the "\urldef" command. New url-like commands can be defined,
267 % and a "\path" command is provided this way.
269 % Usage: Conditions:
270 % \url{ } If the argument contains any "%", "#", or "^^", or ends with
271 % "\", it can't be used in the argument to another command.
272 % The argument must not contain unbalanced braces.
273 % \url| | ...where "|" is any character not used in the argument and not
274 % "{" or a space. The same restrictions as above except that the
275 % argument may contain unbalanced braces.
276 % \xyz for "\xyz" a defined-url; this can be used anywhere, no matter
277 % what characters it contains.
279 % The "\url" command is fragile, and its argument is likely to be very
280 % fragile, but a defined-url is robust.
282 % Package Option: obeyspaces
283 % Ordinarily, all spaces are ignored in the url-text. The "[obeyspaces]"
284 % option allows spaces, but may introduce spurious spaces when a url
285 % containing "\" characters is given in the argument to another command.
286 % So if you need to obey spaces you can say "\usepackage[obeyspaces]{url}",
287 % and if you need both spaces and backslashes, use a `defined-url' for
288 % anything with "\".
290 % Package Option: hyphens
291 % Ordinarily, breaks are not allowed after "-" characters because this
292 % leads to confusion. (Is the "-" part of the address or just a hyphen?)
293 % The package option "[hyphens]" allows breaks after explicit hyphen
294 % characters. The "\url" command will *never ever* hyphenate words.
296 % Package Option: spaces
297 % Likewise, breaks are not usually allowed after spaces under the
298 % "[obeyspaces]" option, but giving the options "[obeyspaces,spaces]"
299 % will allow breaks at those spaces. (Note that it seems logical to
300 % allow the sole option "[spaces]" to let input spaces indicate break
301 % points, but not to display them in the output. This would be easy to
302 % implement, but is left out to avoid(?) confusion.)
304 % Defining a defined-url:
305 % Take for example the email address "myself%node@gateway.net" which could
306 % not be given (using "\url" or "\verb") in a caption or parbox due to the
307 % percent sign. This address can be predefined with
308 % \urldef{\myself}\url{myself%node@gateway.net} or
309 % \urldef{\myself}\url|myself%node@gateway.net|
310 % and then you may use "\myself" instead of "\url{myself%node@gateway.net}"
311 % in an argument, and even in a moving argument like a caption because a
312 % defined-url is robust.
314 % Style:
315 % You can switch the style of printing using "\urlstyle{tt}", where "tt"
316 % can be any defined style. The pre-defined styles are "tt", "rm", "sf",
317 % and "same" which all allow the same linebreaks but different fonts --
318 % the first three select a specific font and the "same" style uses the
319 % current text font. You can define your own styles with different fonts
320 % and/or line-breaking by following the explanations below. The "\url"
321 % command follows whatever the currently-set style dictates.
323 % Alternate commands:
324 % It may be desireable to have different things treated differently, each
325 % in a predefined style; e.g., if you want directory paths to always be
326 % in typewriter and email addresses to be roman, then you would define new
327 % url-like commands as follows:
329 % \DeclareUrlCommand\email{\urlstyle{rm}}
330 % \DeclareUrlCommand\directory{\urlstyle{tt}}
332 % In fact, the "\directory" example is exactly the "\path" definition which
333 % is pre-defined in the package. If you look above, you will see that "\url"
334 % is defined with
335 % \DeclareUrlCommand\url{}
336 % I.e., using whatever "\urlstyle" and other settings are already in effect.
338 % You can make a defined-url for these other styles, using the usual
339 % "\urldef" command as in this example:
341 % \urldef{\myself}{\email}{myself%node.domain@gateway.net}
343 % which makes "\myself" act like "\email{myself%node.domain@gateway.net}",
344 % if the "\email" command is defined as above. The "\myself" command
345 % would then be robust.
347 % Defining styles:
348 % Before describing how to customize the printing style, it is best to
349 % mention something about the unusual implementation of "\url". Although
350 % the material is textual in nature, and the font specification required
351 % is a text-font command, the text is actually typeset in *math* mode.
352 % This allows the context-sensitive linebreaking, but also accounts for
353 % the default behavior of ignoring spaces. Now on to defining styles.
355 % To change the font or the list of characters that allow linebreaks, you
356 % could redefine the commands "\UrlFont", "\UrlBreaks", "\UrlSpecials" etc.
357 % directly in the document, but it is better to define a new `url-style'
358 % (following the example of "\url@ttstyle" and "\url@rmstyle") which defines
359 % all of "\UrlBigbreaks", "\UrlNoBreaks", "\UrlBreaks", "\UrlSpecials", and
360 % "\UrlFont".
362 % Changing font:
363 % The "\UrlFont" command selects the font. The definition of "\UrlFont"
364 % done by the pre-defined styles varies to cope with a variety of LaTeX
365 % font selection schemes, but it could be as simple as "\def\UrlFont{\tt}".
366 % Depending on the font selected, some characters may need to be defined
367 % in the "\UrlSpecials" list because many fonts don't contain all the
368 % standard input characters.
370 % Changing linebreaks:
371 % The list of characters that allow line-breaks is given by "\UrlBreaks"
372 % and "\UrlBigBreaks", which have the format "\do\c" for character "c".
373 % The differences are that `BigBreaks' usually have a lower penalty and have
374 % different breakpoints when in sequence (as in "http://"): `BigBreaks'
375 % are treated as mathrels while `Breaks' are mathbins (see The TeXbook,
376 % p.170). In particular, a series of `BigBreak' characters will break at
377 % the end and only at the end; a series of `Break' characters will break
378 % after the first and after every following *pair*; there will be no
379 % break after a `Break' character if a `BigBreak' follows. In the case
380 % of "http://" it doesn't matter whether ":" is a `Break' or `BigBreak' --
381 % the breaks are the same in either case; but for DECnet nodes with "::"
382 % it is important to prevent breaks *between* the colons, and that is why
383 % colons are `BigBreaks'.
385 % It is possible for characters to prevent breaks after the next following
386 % character (I use this for parentheses). Specify these in "\UrlNoBreaks".
388 % You can do arbitrarily complex things with characters by making them
389 % active in math mode (mathcode hex-8000) and specifying the definition(s)
390 % in "\UrlSpecials". This is used in the rm and sf styles for OT1 font
391 % encoding to handle several characters that are not present in those
392 % computer-modern style fonts. See the definition of "\Url@do", which
393 % is used by both "\url@rmstyle" and "\url@sfstyle"; it handles missing
394 % characters via "\UrlSpecials". The nominal format for setting each
395 % special character "c" is: "\do\c{<definition>}", but you can include
396 % other definitions too.
398 % If all this sounds confusing ... well, it is! But I hope you won't need
399 % to redefine breakpoints -- the default assignments seem to work well for
400 % a wide variety of applications. If you do need to make changes, you can
401 % test for breakpoints using regular math mode and the characters "+=(a".
403 % You can allow some spacing around the breakable characters by assigning
404 % \Urlmuskip = 0mu plus 1mu
405 % You can change the penalties used for BigBreaks and Breaks by assigning
406 % \mathchardef\UrlBreakPenalty=100 \mathchardef\UrlBigBreakPenalty=100
407 % The default penalties are "\binoppenalty" and "\relpenalty". These have
408 % such odd non-LaTeX syntax because I don't expect people to need to
409 % change them often.
411 % Yet more flexibility:
412 % You can also customize the verbatim text by defining "\UrlRight" and/or
413 % "\UrlLeft", e.g., for ISO formatting of urls surrounded by "< >", define
415 % \DeclareUrlCommand\url{\def\UrlLeft{<url:\ }\def\UrlRight{>}%
416 % \urlstyle{tt}}
418 % The meanings of "\UrlLeft" and "\UrlRight" are *not* reproduced verbatim.
419 % This lets you use formatting commands there, but you must be careful not
420 % to use TeX's special characters ("\^_%~#$&{}" etc.) improperly.
421 % You can also define "\UrlLeft" to reprocess the verbatim text, but the
422 % format of the definition is special:
424 % \def\UrlLeft#1\UrlRight{ ... do things with #1 ... }
426 % Yes, that is "#1" followed by "\UrlRight" then the definition. For
427 % example, to put a hyperTeX hypertext link in the DVI file:
429 % \def\UrlLeft#1\UrlRight{\special{html:<a href="#1">}#1\special{html:</a>}}
431 % Using this technique, url.sty can provide a convenient interface for
432 % performing various operations on verbatim text. You don't even need
433 % to print out the argument! For greatest efficiency in such obscure
434 % applications, you can define a null url-style where all the lists like
435 % "\UrlBreaks" are empty.
437 % Revision History:
438 % ver 1.1 6-Feb-1996:
439 % Fix hyphens that wouldn't break and ligatures that weren't suppressed.
440 % ver 1.2 19-Oct-1996:
441 % Package option for T1 encoding; Hooks: "\UrlLeft" and "\UrlRight".
442 % ver 1.3 21-Jul-1997:
443 % Prohibit spaces as delimiter characters; change ascii tilde in OT1.
444 % ver 1.4 02-Mar-1999:
445 % LaTeX license; moving-argument-error
446 % ver 1.5 28-Mar-1999:
447 % possibility of spacing around break characters; re-settable penalties
448 % ver 1.6 20-Jun-2002:
449 % un-double #, fix obeyed-spaces, ignore trailing %, hook for hyperref
450 % (\Url@HyperHook), no macros in pre-processed url string (in \Url@String),
451 % limit catcode change of ~.
452 % ver 3.0 June 2003/Nov 2003:
453 % \DeclareUrlCommand; make font encoding automatic (only a few inputenc characters
454 % are supported yet - needs refactoring); reverse penalties.
455 % ver 3.1 Mar 2004:
456 % Remove spurious spaces in \url@XXstyle commands.
457 % The End
459 Test file integrity: ASCII 32-57, 58-126: !"#$%&'()*+,-./0123456789
460 :;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~