doc-src/underscore.sty
author wenzelm
Wed Jul 25 12:38:54 2012 +0200 (2012-07-25)
changeset 48497 ba61aceaa18a
parent 26862 a79d7d5f1d06
permissions -rw-r--r--
some updates on "Building a repository version of Isabelle";
wenzelm@26862
     1
% underscore.sty     21-Sep-2005   Donald Arseneau   asnd@triumf.ca
wenzelm@26862
     2
% Make the "_" character print as "\textunderscore" in text.
wenzelm@26862
     3
% Copyright 1998,2001,2005,2006 Donald Arseneau;  
wenzelm@26862
     4
% License: LPPL version 1.2 or later.
wenzelm@26862
     5
% Instructions follow after the definitions.
wenzelm@26862
     6
wenzelm@26862
     7
\ProvidesPackage{underscore}[2006/09/13]
wenzelm@26862
     8
wenzelm@26862
     9
\begingroup
wenzelm@26862
    10
 \catcode`\_=\active
wenzelm@26862
    11
 \gdef _{% \relax % No relax gives a small vulnerability in alignments
wenzelm@26862
    12
   \ifx\if@safe@actives\iftrue % must be outermost test!
wenzelm@26862
    13
      \string_%
wenzelm@26862
    14
   \else
wenzelm@26862
    15
      \ifx\protect\@typeset@protect
wenzelm@26862
    16
         \ifmmode \sb \else \BreakableUnderscore \fi
wenzelm@26862
    17
      \else
wenzelm@26862
    18
         \ifx\protect\@unexpandable@protect \noexpand_%
wenzelm@26862
    19
         \else \protect_%
wenzelm@26862
    20
      \fi\fi
wenzelm@26862
    21
    \fi}
wenzelm@26862
    22
  \global\let\ActiveUnderscore=_
wenzelm@26862
    23
  \gdef\normalUnderscoreDef{\let_\ActiveUnderscore}
wenzelm@26862
    24
\endgroup
wenzelm@26862
    25
wenzelm@26862
    26
% At begin: set catcode; fix \long \ttdefault so I can use it in comparisons; 
wenzelm@26862
    27
% reapply definition of active _ in output routine (\@firstofone to strip
wenzelm@26862
    28
% away braces, so avoiding deeper nesting).
wenzelm@26862
    29
\AtBeginDocument{%
wenzelm@26862
    30
  {\immediate\write\@auxout{\catcode\number\string`\_ \string\active}}%
wenzelm@26862
    31
  \catcode\string`\_\string=\active
wenzelm@26862
    32
  \edef\ttdefault{\ttdefault}%
wenzelm@26862
    33
  \output=\expandafter\expandafter\expandafter
wenzelm@26862
    34
     {\expandafter\expandafter\expandafter\normalUnderscoreDef
wenzelm@26862
    35
      \expandafter\@firstofone\the\output}%
wenzelm@26862
    36
}
wenzelm@26862
    37
wenzelm@26862
    38
\newcommand{\BreakableUnderscore}{\leavevmode\nobreak\hskip\z@skip
wenzelm@26862
    39
 \ifx\f@family\ttdefault \string_\else \textunderscore\fi
wenzelm@26862
    40
 \usc@dischyph\nobreak\hskip\z@skip}
wenzelm@26862
    41
wenzelm@26862
    42
\DeclareRobustCommand{\_}{%
wenzelm@26862
    43
  \ifmmode \nfss@text{\textunderscore}\else \BreakableUnderscore \fi}
wenzelm@26862
    44
wenzelm@26862
    45
wenzelm@26862
    46
\let\usc@dischyph\@dischyph
wenzelm@26862
    47
\DeclareOption{nohyphen}{\def\usc@dischyph{\discretionary{}{}{}}}
wenzelm@26862
    48
\DeclareOption{strings}{\catcode`\_=\active}
wenzelm@26862
    49
wenzelm@26862
    50
\ProcessOptions
wenzelm@26862
    51
\ifnum\catcode`\_=\active\else \endinput \fi
wenzelm@26862
    52
wenzelm@26862
    53
%%%%%%%%   Redefine commands that use character strings   %%%%%%%%
wenzelm@26862
    54
wenzelm@26862
    55
\@ifundefined{UnderscoreCommands}{\let\UnderscoreCommands\@empty}{}
wenzelm@26862
    56
\expandafter\def\expandafter\UnderscoreCommands\expandafter{%
wenzelm@26862
    57
  \UnderscoreCommands
wenzelm@26862
    58
  \do\include \do\includeonly
wenzelm@26862
    59
  \do\@input \do\@iinput \do\InputIfFileExists
wenzelm@26862
    60
  \do\ref \do\pageref \do\newlabel
wenzelm@26862
    61
  \do\bibitem \do\@bibitem \do\cite \do\nocite \do\bibcite
wenzelm@26862
    62
  \do\Ginclude@graphics \do\@setckpt
wenzelm@26862
    63
}
wenzelm@26862
    64
wenzelm@26862
    65
% Macro to redefine a macro to pre-process its string argument
wenzelm@26862
    66
% with \protect -> \string.
wenzelm@26862
    67
\def\do#1{% Avoid double processing if user includes command twice!
wenzelm@26862
    68
 \@ifundefined{US\string_\expandafter\@gobble\string#1}{%
wenzelm@26862
    69
   \edef\@tempb{\meaning#1}% Check if macro is just a protection shell...
wenzelm@26862
    70
   \def\@tempc{\protect}%
wenzelm@26862
    71
   \edef\@tempc{\meaning\@tempc\string#1\space\space}%
wenzelm@26862
    72
   \ifx\@tempb\@tempc % just a shell: hook into the protected inner command
wenzelm@26862
    73
     \expandafter\do
wenzelm@26862
    74
       \csname \expandafter\@gobble\string#1 \expandafter\endcsname
wenzelm@26862
    75
   \else % Check if macro takes an optional argument
wenzelm@26862
    76
     \def\@tempc{\@ifnextchar[}%
wenzelm@26862
    77
     \edef\@tempa{\def\noexpand\@tempa####1\meaning\@tempc}%
wenzelm@26862
    78
     \@tempa##2##3\@tempa{##2\relax}%
wenzelm@26862
    79
     \edef\@tempb{\meaning#1\meaning\@tempc}%
wenzelm@26862
    80
     \edef\@tempc{\noexpand\@tempd \csname
wenzelm@26862
    81
        US\string_\expandafter\@gobble\string#1\endcsname}%
wenzelm@26862
    82
     \if \expandafter\@tempa\@tempb \relax 12\@tempa % then no optional arg
wenzelm@26862
    83
       \@tempc #1\US@prot
wenzelm@26862
    84
     \else  % There is optional arg
wenzelm@26862
    85
       \@tempc #1\US@protopt
wenzelm@26862
    86
     \fi
wenzelm@26862
    87
   \fi
wenzelm@26862
    88
 }{}}
wenzelm@26862
    89
wenzelm@26862
    90
\def\@tempd#1#2#3{\let#1#2\def#2{#3#1}}
wenzelm@26862
    91
wenzelm@26862
    92
\def\US@prot#1#2{\let\@@protect\protect \let\protect\string
wenzelm@26862
    93
  \edef\US@temp##1{##1{#2}}\restore@protect\US@temp#1}
wenzelm@26862
    94
\def\US@protopt#1{\@ifnextchar[{\US@protarg#1}{\US@prot#1}}
wenzelm@26862
    95
\def\US@protarg #1[#2]{\US@prot{{#1[#2]}}}
wenzelm@26862
    96
wenzelm@26862
    97
\UnderscoreCommands
wenzelm@26862
    98
\let\do\relax \let\@tempd\relax  % un-do
wenzelm@26862
    99
wenzelm@26862
   100
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
wenzelm@26862
   101
wenzelm@26862
   102
\endinput
wenzelm@26862
   103
wenzelm@26862
   104
underscore.sty    13-Sep-2006  Donald Arseneau
wenzelm@26862
   105
wenzelm@26862
   106
Features:
wenzelm@26862
   107
~~~~~~~~~
wenzelm@26862
   108
The "\_" command (which normally prints an underscore character or
wenzelm@26862
   109
facsimile) is altered so that the hyphenation of constituent words
wenzelm@26862
   110
is not affected, and hyphenation is permitted after the underscore.
wenzelm@26862
   111
For example, "compound\_fracture" hyphenates as com- pound\_- frac- ture.
wenzelm@26862
   112
If you prefer the underscore to break without a hyphen (but still with 
wenzelm@26862
   113
the same rules for explicit hyphen-breaks) then use the [nohyphen]
wenzelm@26862
   114
package option.
wenzelm@26862
   115
wenzelm@26862
   116
A simple "_" acts just like "\_" in text mode, but makes a subscript
wenzelm@26862
   117
in math mode: activation_energy $E_a$
wenzelm@26862
   118
wenzelm@26862
   119
Both forms use an underscore character if the font encoding contains
wenzelm@26862
   120
one (e.g., "\usepackage[T1]{fontenc}" or typewriter fonts in any encoding),
wenzelm@26862
   121
but they use a rule if there is no proper character.
wenzelm@26862
   122
wenzelm@26862
   123
Deficiencies:
wenzelm@26862
   124
~~~~~~~~~~~~~
wenzelm@26862
   125
The skips and penalties ruin any kerning with the underscore character
wenzelm@26862
   126
(when a character is used).  However, there doesn't seem to be much, if
wenzelm@26862
   127
any, such kerning in the ec fonts, and there is never any kerning with
wenzelm@26862
   128
a rule.
wenzelm@26862
   129
wenzelm@26862
   130
You must avoid "_" in file names and in cite or ref tags, or you must use 
wenzelm@26862
   131
the babel package, with its active-character controls, or you must give 
wenzelm@26862
   132
the [strings] option, which attempts to redefine several commands (and 
wenzelm@26862
   133
may not work perfectly).  Even without the [strings] option or babel, you 
wenzelm@26862
   134
can use occasional underscores like: "\include{file\string_name}".
wenzelm@26862
   135
wenzelm@26862
   136
Option: [strings]
wenzelm@26862
   137
~~~~~~~~~~~~~~~~~
wenzelm@26862
   138
The default operation is quite simple and needs no customization; but
wenzelm@26862
   139
you must avoid using "_" in any place where LaTeX uses an argument as
wenzelm@26862
   140
a string of characters for some control function or as a name.  These
wenzelm@26862
   141
include the tags for "\cite" and "\ref", file names for "\input", 
wenzelm@26862
   142
"\include", and "\includegraphics", environment names, counter names,
wenzelm@26862
   143
and placement parameters (like "[t]").  The problem with these contexts
wenzelm@26862
   144
is that they are `moving arguments' but LaTeX does not `switch on' the
wenzelm@26862
   145
"\protect" mechanism for them.
wenzelm@26862
   146
wenzelm@26862
   147
If you need to use the underscore character in these places, the package
wenzelm@26862
   148
option [strings] is provided to redefine commands that take such a string
wenzelm@26862
   149
argument so that protection is applied (with "\protect" being "\string").
wenzelm@26862
   150
The list of commands is given in "\UnderscoreCommands", with "\do" before
wenzelm@26862
   151
each; plus several others covering "\input", "\includegraphics, "\cite", 
wenzelm@26862
   152
"\ref", and their variants.  Not included are many commands regarding font 
wenzelm@26862
   153
names, everything with counter names, environment names, page styles, and 
wenzelm@26862
   154
versions of "\ref" and "\cite" defined by external packages (e.g., "\vref" 
wenzelm@26862
   155
and "\citeyear").
wenzelm@26862
   156
wenzelm@26862
   157
You can add to the list of supported commands by defining "\UnderscoreCommands"
wenzelm@26862
   158
before loading this package; e.g.
wenzelm@26862
   159
wenzelm@26862
   160
   \usepackage{chicago}
wenzelm@26862
   161
   \newcommand{\UnderscoreCommands}{%   (\cite already done)
wenzelm@26862
   162
     \do\citeNP \do\citeA \do\citeANP \do\citeN \do\shortcite
wenzelm@26862
   163
     \do\shortciteNP \do\shortciteA \do\shortciteANP \do\shortciteN
wenzelm@26862
   164
     \do\citeyear \do\citeyearNP
wenzelm@26862
   165
   }
wenzelm@26862
   166
   \usepackage[strings]{underscore}
wenzelm@26862
   167
wenzelm@26862
   168
Not all commands can be supported this way!  Only commands that take a
wenzelm@26862
   169
string argument *first* can be protected.  One optional argument before
wenzelm@26862
   170
the string argument is also permitted, as exemplified by "\cite": both
wenzelm@26862
   171
"\cite{tags}" and "\cite[text]{tags}" are allowed.  A command like
wenzelm@26862
   172
"\@addtoreset" which takes two counter names as arguments could not
wenzelm@26862
   173
be protected by listing it in "\UnderscoreCommands".
wenzelm@26862
   174
wenzelm@26862
   175
*When you use the [strings] option, you must load this package
wenzelm@26862
   176
last* (or nearly last).
wenzelm@26862
   177
wenzelm@26862
   178
There are two reasons: 1) The redefinitions done for protection must come
wenzelm@26862
   179
after other packages define their customized versions of those commands.
wenzelm@26862
   180
2) The [strings] option requires the "_" character to be activated immediately
wenzelm@26862
   181
in order for the cite and ref tags to be read properly from the .aux file
wenzelm@26862
   182
as plain strings, and this catcode setting might disrupt other packages.
wenzelm@26862
   183
wenzelm@26862
   184
The babel package implements a protection mechanism for many commands,
wenzelm@26862
   185
and will be a complete fix for most documents without the [strings] option.
wenzelm@26862
   186
Many add-on packages are compatible with babel, so they will get the
wenzelm@26862
   187
strings protection also.  However, there are several commands that are 
wenzelm@26862
   188
not covered by babel, but can easily be supported by the [strings] and 
wenzelm@26862
   189
"\UnderscoreCommands" mechanism.  Beware that using both [strings] and
wenzelm@26862
   190
babel might lead to conflicts, but none are seen yet (load babel last).
wenzelm@26862
   191
wenzelm@26862
   192
Implementation Notes:
wenzelm@26862
   193
~~~~~~~~~~~~~~~~~~~~~
wenzelm@26862
   194
The first setting of "_" to be an active character is performed in a local
wenzelm@26862
   195
group so as to not interfere with other packages.  The catcode setting
wenzelm@26862
   196
is repeated with "\AtBeginDocument" so the definition is in effect for the
wenzelm@26862
   197
text.  However, the catcode setting is repeated immediately when the
wenzelm@26862
   198
[strings] option is detected.
wenzelm@26862
   199
wenzelm@26862
   200
The definition of the active "_" is essentially:
wenzelm@26862
   201
wenzelm@26862
   202
       \ifmmode \sb \else \BreakableUnderscore \fi
wenzelm@26862
   203
wenzelm@26862
   204
where "\sb" retains the normal subscript meaning of "_" and where
wenzelm@26862
   205
"\BreakableUnderscore" is essentially "\_".  The rest of the definition
wenzelm@26862
   206
handles the "\protect"ion without causing "\relax" to be inserted before
wenzelm@26862
   207
the character.
wenzelm@26862
   208
wenzelm@26862
   209
"\BreakableUnderscore" uses "\nobreak\hskip\z@skip" to separate the
wenzelm@26862
   210
underscore from surrounding words, thus allowing TeX to hyphenate them,
wenzelm@26862
   211
but preventing free breaks around the underscore. Next, it checks the
wenzelm@26862
   212
current font family, and uses the underscore character from tt fonts or
wenzelm@26862
   213
otherwise "\textunderscore" (which is a character or rule depending on
wenzelm@26862
   214
the font encoding).  After the underscore, it inserts a discretionary
wenzelm@26862
   215
hyphenation point as "\usc@dischyph", which is usually just "\-"
wenzelm@26862
   216
except that it still works in the tabbing environment, although it
wenzelm@26862
   217
will give "\discretionary{}{}{}" under the [nohyphen] option.  After
wenzelm@26862
   218
that, another piece of non-breaking interword glue is inserted. 
wenzelm@26862
   219
Ordinarily, the comparison "\ifx\f@family\ttdefault" will always fail 
wenzelm@26862
   220
because "\ttdefault" is `long' whereas "\f@family" is not (boooo hisss),
wenzelm@26862
   221
but "\ttdefault" is redefined to be non-long by "\AtBeginDocument".
wenzelm@26862
   222
wenzelm@26862
   223
The "\_" command is then defined to use "\BreakableUnderscore".
wenzelm@26862
   224
wenzelm@26862
   225
If the [strings] option is not given, then that is all!
wenzelm@26862
   226
wenzelm@26862
   227
Under the [strings] option, the list of special commands is processed to:
wenzelm@26862
   228
wenzelm@26862
   229
 - retain the original command as "\US_"*command* (e.g., "\US_ref")
wenzelm@26862
   230
 - redefine the command as "\US@prot\US_command" for ordinary commands
wenzelm@26862
   231
   ("\US@prot\US_ref") or as "\US@protopt\US_command" when an optional
wenzelm@26862
   232
   argument is possible (e.g., "\US@protopt\US_bibitem").
wenzelm@26862
   233
 - self-protecting commands ("\cite") retain their self-protection.
wenzelm@26862
   234
wenzelm@26862
   235
Diagnosing the state of the pre-existing command is done by painful
wenzelm@26862
   236
contortions involving "\meaning".
wenzelm@26862
   237
wenzelm@26862
   238
"\US@prot" and "\US@protopt" read the argument, process it with 
wenzelm@26862
   239
"\protect" enabled, then invoke the saved "\US_command".
wenzelm@26862
   240
wenzelm@26862
   241
Modifications:
wenzelm@26862
   242
~~~~~~~~~~~~~~
wenzelm@26862
   243
13-Sep-2006  Reassert my definition in the output routine (listings).
wenzelm@26862
   244
21-Sep-2005  \includegraphics safe.
wenzelm@26862
   245
12-Oct-2001  Babel (safe@actives) compatibility and [nohyphen] option.
wenzelm@26862
   246
wenzelm@26862
   247
Test file integrity:  ASCII 32-57, 58-126:  !"#$%&'()*+,-./0123456789
wenzelm@26862
   248
:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~