Debian Codegraph @

texmf-dist/source/latex/textcase/textcase.dtx

raw
1 % \iffalse
2 %% Source File: textcase.dtx
3 %% Copyright 1997 1998 2019 2022 2023 2025 David Carlisle
4 %%
5 %% This file may be distributed under the terms of the LPPL.
6 %% See 00readme.txt for details.
7 %
8 %<*dtx>
9 \ProvidesFile{textcase.dtx}
10 %</dtx>
11 %<package>\NeedsTeXFormat{LaTeX2e}
12 %<package>\ProvidesPackage{textcase}
13 %<driver> \ProvidesFile{textcase.drv}
14 % \fi
15 % \ProvidesFile{textcase.dtx}
16 [2025/02/16 v1.05 Text only upper/lower case changing (DPC)]
17 %
18 % \iffalse
19 %<*driver>
20 \documentclass{ltxdoc}
21 \usepackage{textcase}
22 \begin{document}
23 \DocInput{textcase.dtx}
24 \end{document}
25 %</driver>
26 % \fi
27 %
28 % \GetFileInfo{textcase.dtx}
29 %
30 % \changes{v0.01}{1997/09/28}
31 % {Posted to c.t.t}
32 % \changes{v0.02}{1997/09/28}
33 % {Support \cs{ensuremath}}
34 % \changes{v0.03}{1997/12/12}
35 % {Donald Arseneau: support \cs{ref} \cs{cite} \cs{label}}
36 % \changes{v0.04}{1997/12/14}
37 % {Convert to dtx form, add \cs{NoCaseChange}}
38 % \changes{v0.04}{1997/12/14}
39 % {Support (most uses of) optional argument of \cs{cite}}
40 % \changes{v0.05}{1997/12/16}
41 % {More comments from Donald,
42 % perhaps I should just give him this package for Christmas}
43 % \changes{v0.06}{1998/11/12}
44 % {overload option}
45 % \changes{v0.07}{2004/10/07}
46 % {missing percent added}
47 %
48 %
49 % \title{The \textsf{textcase} package\thanks{This file
50 % has version number \fileversion, last
51 % revised \filedate.}}
52 % \author{David Carlisle\thanks{%^^A
53 % Reorganisation to suppport skipping a wider class of
54 % things than just math done by Donald Arseneau}}
55 %
56 % \date{\filedate}
57 %
58 % \maketitle
59 %
60 % \section{Introduction}\label{intro}
61 %
62 % \subsection*{This package is obsolete with the June 2022 \LaTeX\ release.}
63 % \begin{sloppypar}
64 % With new formats |\MakeUppercase| supports the extensions provided
65 % here, with improved support for UTF-8 input. So now the package
66 % just defines |\MakeTextUppercase| and |\MakeTextLowercase| as
67 % aliases for the standard |\MakeUppercase| and
68 % |\MakeLowercase|\footnote{Since v1.04, not quite alises, one
69 % grouping level is dropped for compatibility, required by
70 % \textsf{amsart}.}.
71 % \end{sloppypar}
72 %
73 %
74 % \subsection*{Old Introduction}
75 %
76 % |\MakeTextUppercase| and |\MakeTextLowercase| are versions of the
77 % standard |\MakeUppercase| and |\MakeLowercase| that do not change
78 % the case of any math sections in their arguments.
79 %\begin{verbatim}
80 % \MakeTextUppercase{abc\ae\ \( a = b \) and $\alpha \neq a$
81 % or even \ensuremath{x=y} and $\ensuremath{x=y}$}
82 %\end{verbatim}
83 % Should produce:
84 % \begin{quotation}
85 % ABC\AE\ \( a = b \) AND $\alpha \neq a$
86 % OR EVEN \ensuremath{x=y} AND $\ensuremath{x=y}$
87 % \end{quotation}
88 %
89 % Version 0.03 of this package incorporates some changes suggested by
90 % Donald Arseneau so that as well as math mode, the arguments of
91 % |\cite|, |\label| and |\ref| are also prevented from being uppercased.
92 % So you can now go
93 %\begin{verbatim}
94 % \MakeTextUppercase{%
95 % Text in section~\ref{intro}, about \cite[pp 2--4]{bbb}}
96 %\end{verbatim}
97 % which produces
98 % \begin{quotation}
99 % \MakeTextUppercase{%
100 % Text in section~\ref{intro}, about \cite[pp 2--4]{bbb}}
101 % \end{quotation}
102 % If, instead, the standard |\MakeUppercase| were used here, the keys
103 % `into' and `bbb' would be uppercased and generate errors about
104 % undefined references to INTRO and BBB.
105 %
106 % Sometimes there may be a special section of text that should not be
107 % uppercased. This can be marked with |\NoCaseChange|, as follows.
108 %\begin{verbatim}
109 % \MakeTextUppercase{%
110 % Text \NoCaseChange{More Text} yet more text}
111 %\end{verbatim}
112 % which produces
113 % \begin{quotation}
114 % \MakeTextUppercase{%
115 % Text \NoCaseChange{More Text} yet more text}
116 % \end{quotation}
117 %
118 % |\NoCaseChange| has other uses. If for some reason you need a
119 % tabular environment within an uppercased section, then you need
120 % to ensure that the name `tabular' and the preamble (eg `ll')
121 % does not get uppercased:
122 %\begin{verbatim}
123 % \MakeTextUppercase{%
124 % Text \NoCaseChange{\begin{tabular}{ll}}%
125 % table&stuff\\goes&here
126 % \NoCaseChange{\end{tabular}}
127 % More text}
128 %\end{verbatim}
129 % which produces
130 % \begin{quotation}
131 % \MakeTextUppercase{%
132 % Text \NoCaseChange{\begin{tabular}{ll}}%^^A
133 % table&stuff\\goes&here
134 % \NoCaseChange{\end{tabular}}
135 % More text}
136 % \end{quotation}
137 %
138 % \section{Features and Foibles}
139 %
140 % \subsection{Nested text}
141 % The commands defined here only skip math sections and |\ref| arguments
142 % if they are not `hidden' inside a |{ }| brace group. All text inside
143 % such a group will be made uppercase just as with the standard
144 % |\MakeUppercase|.
145 %\begin{verbatim}
146 % \MakeTextUppercase{a b {c $d$} $e$}
147 %\end{verbatim}
148 % produces
149 % \begin{quotation}
150 % \MakeTextUppercase{a b {c $d$} $e$}
151 % \end{quotation}
152 % Of course, this restriction does not apply to the arguments of the
153 % supported commands |\ensuremath|, |\label|, |\ref|, and |\cite|.
154 %
155 % If you cannot arrange for your
156 % mathematics to be at the outer level of brace grouping, you should
157 % use the following basic technique (which works even with the standard
158 % |\MakeUppercase| command). Define a new command that expands to your
159 % math expression, and then use that command, with |\protect|, in the
160 % text to be uppercased. Note that if the text being uppercased is in a
161 % section title or other moving argument you may need to make the
162 % definition in the document preamble, rather than just before the
163 % section command, so that the command is defined when the table of
164 % contents file is read.
165 %\begin{verbatim}
166 % \MakeTextUppercase{%
167 % Text \fbox{$a=b$ and $x=y$}}%
168 %
169 % \newcommand{\mathexprone}{$a=b$}
170 % \newcommand{\mathexprtwo}{$x=y$}
171 % \MakeTextUppercase{%
172 % Text \fbox{\protect\mathexprone\ and \protect\mathexprtwo}}%
173 %\end{verbatim}
174 % which produces
175 % \begin{quotation}
176 % \MakeTextUppercase{%
177 % Text \fbox{$a=b$ and $x=y$}}%
178 %
179 % \newcommand{\mathexprone}{$a=b$}
180 % \newcommand{\mathexprtwo}{$x=y$}
181 % \MakeTextUppercase{%
182 % Text \fbox{\protect\mathexprone\ and \protect\mathexprtwo}}%
183 % \end{quotation}
184 %
185 % \subsection{Citations}
186 % As documented above, |\cite| and |\ref| commands are not uppercased by
187 % |\MakeTextUppercase|. If you are using a non-numeric citation scheme
188 % you may want the replacement text for |\cite| to be uppercased.
189 %
190 % It is difficult to arrange that |\MakeTextUppercase| uppercases such
191 % text, not least because this would lead to interaction with the many
192 % bibliography packages which redefine |\cite| one way or another. One
193 % possibility to achieve this is to use Donald Arseneau's cite package
194 % and to locally redefine |\citeform| to add |\MakeUppercase| around
195 % the final text string produced by |\cite|.
196 %\begin{verbatim}
197 % \MakeTextUppercase{%
198 % Text \cite{bbb} and \cite{ccc}}
199 %
200 % {\renewcommand\citeform{\MakeUppercase}\MakeTextUppercase{%
201 % Text \cite{bbb} and \cite{ccc}}}
202 %\end{verbatim}
203 % which produces\footnote{This is faked, so this document does not
204 % rely on \texttt{cite.sty} being installed}
205 % \begin{quotation}
206 % TEXT [1] AND [David Carlisle 1997]
207 %
208 % TEXT [1] AND [DAVID CARLISLE 1997]
209 % \end{quotation}
210 %
211 % \subsection{overload Option}
212 % By default the package only defines new commands, |\MakeTextUppercase|
213 % and |\MakeTextlowercase| as described above. You may wish to redefine
214 % the standard |\MakeUppercase| and |\MakeLowercase| commands to be
215 % the same as these new commands. So that for example headings in the
216 % book class have this new feature without any further redefinition.
217 % You may use the package option |[overload]| in which case these
218 % new definitions will be overloaded on to the existing command names.
219 %
220 % \begin{thebibliography}{9}
221 %
222 % \bibitem{bbb} Something numeric
223 % \bibitem[David Carlisle 1997]{ccc} Something textual
224 % \end{thebibliography}
225 %
226 % \StopEventually{}
227 %
228 % \section{Implementation}
229 %
230 % \begin{macrocode}
231 %<*package>
232 % \end{macrocode}
233 %
234 % In current \LaTeX\ just define legacy names.
235 % \begin{macrocode}
236 \ifx\AddToNoCaseChangeList\@undefined\else
237 \DeclareOption{overload}{}
238 \ProcessOptions
239 \PackageInfo{textcase}{Defining Legacy names}
240 % \end{macrocode}
241 % \changes{v1.04}{2023/07/27}
242 % {define without outer group}
243 % \begin{macrocode}
244 \ExplSyntaxOn
245 \cs_set_protected:Npn \reserved@a #1
246 {
247 \cs_generate_variant:cn { text_ \str_lowercase:n {#1} case:nn } { V }
248 \ExpandArgs { cnx } \NewExpandableDocumentCommand
249 { MakeText#1case }
250 { O{} +m }
251 { \exp_not:c { MakeText#1case \c_space_tl \c_space_tl \c_space_tl }
252 [####1] {####2} }
253 }
254 \reserved@a { Upper }
255 \reserved@a { Lower }
256 % \end{macrocode}
257 % Internal change to \verb|\@@text@case@aux| requires modifed arguments in new formats
258 % \changes{v1.05}{2025/02/16}
259 % {changed internal in new formats}
260 % \begin{macrocode}
261 \IfFormatAtLeastTF{2024-11-12}{\def\textcase@case{case}}{\def\textcase@case{}}
262 % \end{macrocode}
263 % \begin{macrocode}
264 \cs_new_protected:cpn {
265 MakeTextLowercase \c_space_tl \c_space_tl \c_space_tl } [#1] #2
266 {%{
267 \let \OE \oe
268 \@@text@case@aux { lower \textcase@case } {#1} {#2}
269 }%}
270 \cs_new_protected:cpn {
271 MakeTextUppercase \c_space_tl \c_space_tl \c_space_tl } [#1] #2
272 {%{
273 \let \oe \OE
274 \@@text@case@aux { upper \textcase@case } {#1} {#2}
275 }%}
276 \ExplSyntaxOff
277 % \end{macrocode}
278 %
279 % \begin{macrocode}
280 \expandafter\endinput
281 \fi
282 % \end{macrocode}
283 %
284 % \begin{macro}{\@uclcnotmath}
285 % This is the main macro of this package. It is basically
286 % a copy of |\MakeUppercase| and |\MakeLowercase|
287 % from the \LaTeX\ kernel, modified slightly so that they
288 % can share code (that modification could be done to the
289 % standard versions as well) and then further changed to
290 % skip certain features like math mode and |\label| arguments.
291 %
292 % The arguments are:\\
293 % |#1|: Extra commands to apply for case changing.
294 % Used to locally redefine |\i| and |\j| for uppercasing.\\
295 % |#2|: Either |##1##2| or |##2##1| to control the order
296 % in which |\let| is applied to the pairs of control
297 % sequences in |\@uclclist|.\\
298 % |#3|: |\uppercase| or |\lowercase|.\\
299 % |#4|: The text to be upper (or lower) cased.
300 % \begin{macrocode}
301 \def\@uclcnotmath#1#2#3#4{\begingroup
302 % \end{macrocode}
303 % Run extra commands (currently just to redefine |\i| and |\j|).
304 % \begin{macrocode}
305 #1%
306 % \end{macrocode}
307 %
308 % Locally set |\( \)| to be just |$ $|, so that the math skipping
309 % code can be simplified, just to look for |$|.
310 % \begin{macrocode}
311 \def\({$}\let\)\(%
312 % \end{macrocode}
313 %
314 % \changes{v1.00}{2019/09/14}
315 % {UTF-8 support for 2019 LaTeX format}
316 % Allow UTF-8 characters to expand in the |\protected@edef| so that
317 % they become for example |\IeC{\'{e}}| and uppercase correctly.
318 % \changes{v1.01}{2022/02/14}
319 % {UTF-8 support for 2022 LaTeX format (unreleased)}
320 % \changes{v1.02}{2022/06/12}
321 % {Obsolete this package in newer formats}
322 % \changes{v1.02s}{2022/067/10}
323 % {Adjust test for newer formats}
324 % Allow UTF-8 characters are e-\TeX\ |\protected| in newer \LaTeX\ formats.
325 %
326 % \begin{macrocode}
327 \let\UTF@two@octets@noexpand\@empty
328 \let\UTF@three@octets@noexpand\@empty
329 \let\UTF@four@octets@noexpand\@empty
330 % \end{macrocode}
331 %
332 % Set up the `non-math' things that also have to be skipped.
333 % \begin{macrocode}
334 \def\NoCaseChange##1{\noexpand\NoCaseChange{\noexpand##1}}%
335 \@nonchangecase\label
336 \@nonchangecase\ref
337 \@nonchangecase\ensuremath
338 % \end{macrocode}
339 %
340 % |\cite| a bit trickier, as we want to uppercase any optional argument.
341 % This will fail if the optional argument contains a brace group, but
342 % should catch most cases.
343 %
344 % |text \cite[page 1]{foo} more text| ends up as
345 %\begin{verbatim}
346 % \uppercase{text \toks@{\cite[page1]}%
347 % \the\toks@{foo}%
348 % \uppercase{ more text}
349 %\end{verbatim}
350 % \begin{macrocode}
351 \def\cite##1##{\toks@{\noexpand\cite##1}\@citex}%
352 \def\@citex##1{\NoCaseChange{\the\toks@{##1}}}%
353 % \end{macrocode}
354 % (|\@citex| is a scratch macro here, not a redefinition of the existing
355 % |\@citex|.)
356 %
357 % The following is essentially taken from |\MakeUppercase|.
358 % Recursively execute |\reserved@a| to |\let| the pairs in |\@uclclist|.
359 % The strange construction with |\@gobble| at the end just gobbles the
360 % final recursive call.
361 %
362 % Incidentally, packages should not use the |\reserved@|\ldots\ scratch
363 % macros, which are `reserved' for use within the \LaTeX\ kernel, but
364 % (a) this code is essentially a copy from the kernel, and
365 % (b) I'm allowed to break the rules, so there.
366 % \begin{macrocode}
367 \def\reserved@a##1##2{\let#2\reserved@a}%
368 \expandafter\reserved@a\@uclclist\reserved@b{\reserved@b\@gobble}%
369 % \end{macrocode}
370 %
371 % Expand everything first so that the `skipping' code can see what to
372 % skip and so that tokens are revealed to |\uppercase|.
373 % This makes the argument `moving'.
374 % The |$\valign$| is just a fake math expression used to terminate
375 % the parsing done by |\@skipmath|.
376 % \begin{macrocode}
377 \protected@edef\reserved@a{\endgroup
378 \noexpand\@skipmath#3#4$\valign$}%
379 % \end{macrocode}
380 %
381 % \begin{macrocode}
382 \reserved@a}
383 % \end{macrocode}
384 % \end{macro}
385 %
386 % \begin{macro}{\@nonchangecase}
387 % \begin{macrocode}
388 \def\@nonchangecase#1{\def#1##1{\NoCaseChange{#1{##1}}}}
389 % \end{macrocode}
390 % \end{macro}
391 %
392 % \begin{macro}{\NoCaseChange}
393 % For hiding arbitrary text from |\uppercase|. This innocuous
394 % definition is used for any occurrence of |\NoCaseChange|
395 % in text that is not passed to |\MakeTextUppercase|. For example a
396 % section heading may be uppercased, but the toc entry may not.
397 % It is also used for nested definitions of |\cite| etc, where the
398 % |\NoCaseChange| is inserted by expansion, but not removed as it is
399 % hidden by the brace group.
400 % \begin{macrocode}
401 \let\NoCaseChange\@firstofone
402 % \end{macrocode}
403 % \end{macro}
404 %
405 %\begin{macro}{\@skipmath}
406 % |#1|: operation |\uppercase| / |\lowercase|\\
407 % |#2|: text up to first (next) |$|\\
408 % |#3|: first math mode material (or |\valign| sentinel)
409 % \begin{macrocode}
410 \def\@skipmath#1#2$#3${%
411 \@skip@nonchangecase#1#2\NoCaseChange\valign
412 \ifx\valign#3%
413 \else
414 $#3$%
415 \expandafter\@skipmath\expandafter#1%
416 \fi}
417 % \end{macrocode}
418 % \end{macro}
419 %
420 %\begin{macro}{\@skip@nonchangecase}
421 % |#1|: |\uppercase| or |\lowercase|\\
422 % |#2|: Text up to the first command (such as |\cite|) made `safe'.
423 % or the first use of |\NoCaseChange|.
424 % \begin{macrocode}
425 \def\@skip@nonchangecase#1#2\NoCaseChange#3{%
426 #1{#2}%
427 \ifx\valign#3%
428 \else
429 #3%
430 \expandafter\@skip@nonchangecase\expandafter#1%
431 \fi}
432 % \end{macrocode}
433 % \end{macro}
434 %
435 % \begin{macro}{\MakeTextUppercase}
436 % \changes{v0.06}{1998/11/12}
437 % {Add edef to make behaviour more like \cs{MakeUppercase}}
438 % Put it all together. Arrange for \i\ and \j\ to uppercase, and
439 % to read the pairs in the ucase list `forwards'.
440 % \begin{macrocode}
441 \DeclareRobustCommand\MakeTextUppercase{%
442 \@uclcnotmath{\def\i{I}\def\j{J}}{##1##2}\uppercase}
443 % \end{macrocode}
444 %
445 % \begin{macrocode}
446 \protected@edef\MakeTextUppercase#1{\MakeTextUppercase{#1}}
447 % \end{macrocode}
448 % \end{macro}
449 %
450 % \begin{macro}{\MakeTextLowercase}
451 % \changes{v0.06}{1998/11/12}
452 % {Add edef to make behaviour more like \cs{MakeLowercase}}
453 % probably will never be used, but it is easy to implement.
454 % Use |\lowercase| and read the pairs in the uppercase list `backwards'.
455 % \begin{macrocode}
456 \DeclareRobustCommand\MakeTextLowercase{%
457 \@uclcnotmath{}{##2##1}\lowercase}
458 % \end{macrocode}
459 %
460 % \begin{macrocode}
461 \protected@edef\MakeTextLowercase#1{\MakeTextLowercase{#1}}
462 % \end{macrocode}
463 % \end{macro}
464 %
465 % \begin{macro}{\MakeUppercase}
466 % \begin{macro}{\MakeLowercase}
467 % \changes{v0.06}{1998/11/12}
468 % {Option to overload added. latex/2906}
469 % \begin{macrocode}
470 \DeclareOption{overload}{%
471 \expandafter\let\csname MakeUppercase \expandafter\endcsname
472 \csname MakeTextUppercase \endcsname
473 \expandafter\let\csname MakeLowercase \expandafter\endcsname
474 \csname MakeTextLowercase \endcsname}
475 % \end{macrocode}
476 %
477 % \begin{macrocode}
478 \ProcessOptions
479 % \end{macrocode}
480 % \end{macro}
481 % \end{macro}
482 %
483 % \begin{macrocode}
484 %</package>
485 % \end{macrocode}
486 %
487 % \Finale
488 %