Debian Codegraph @

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

raw
1 % \iffalse meta-comment
2 %
3 % Copyright (C) 2022 by Julien LabbĂ© <[email protected]>
4 %
5 %
6 % This file may be distributed and/or modified under the conditions of the LaTeX
7 % Project Public License, either version 1.3 of this license or (at your option)
8 % any later version. The latest version of this license is in:
9 %
10 %
11 % http://www.latex-project.org/lppl.txt
12 %
13 % and version 1.3 or later is part of all distributions of LaTeX version
14 % 2005/12/01 or later.
15 %
16 % Note : this file was created from the version 2.1 of the dtxtut tutorial,
17 % maintained by Scott Pakin.
18 %
19 % \fi
20 %
21 % \iffalse
22 %<*driver>
23 \ProvidesFile{altsubsup.dtx}
24 %</driver>
25 %<package> \NeedsTeXFormat{LaTeX2e}
26 %<package> \ProvidesPackage{altsubsup}
27 %<*package>
28 [2022/03/15 v1.1 .dtx altsubsup file]
29 %</package>
30 %
31 %<*driver>
32 \documentclass{ltxdoc}
33 \usepackage{altsubsup}
34 \usepackage{spbmark}
35 \usepackage[hidelinks]{hyperref}
36 \usepackage{xcolor}
37 \EnableCrossrefs
38 \CodelineIndex
39 \RecordChanges
40 % \OnlyDescription
41 \begin{document}
42 \DocInput{altsubsup.dtx}
43 \PrintChanges
44 \PrintIndex
45 \end{document}
46 %</driver>
47 % \fi
48 %
49 % \CheckSum{147}
50 %
51 % \CharacterTable
52 % {Upper-case \A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z
53 % Lower-case \a\b\c\d\e\f\g\h\i\j\k\l\m\n\o\p\q\r\s\t\u\v\w\x\y\z
54 % Digits \0\1\2\3\4\5\6\7\8\9
55 % Exclamation \! Double quote \" Hash (number) \#
56 % Dollar \$ Percent \% Ampersand \&
57 % Acute accent \' Left paren \( Right paren \)
58 % Asterisk \* Plus \+ Comma \,
59 % Minus \- Point \. Solidus \/
60 % Colon \: Semicolon \; Less than \<
61 % Equals \= Greater than \> Question mark \?
62 % Commercial at \@ Left bracket \[ Backslash \\
63 % Right bracket \] Circumflex \^ Underscore \_
64 % Grave accent \` Left brace \{ Vertical bar \|
65 % Right brace \} Tilde \~}
66 %
67 %
68 % \changes{v1.0}{2022/01/23}{Initial version.}
69 %
70 % \GetFileInfo{altsubsup.dtx}
71 %
72 % \DoNotIndex{\newcommand,\newenvironment,\let}
73 % \DoNotIndex{\#,\$,\%,\&,\@,\\,\{,\},\^,\_,\~,\ }
74 % \DoNotIndex{\begingroup,\endgroup,\bgroup,\egroup}
75 % \DoNotIndex{\catcode,\mathcode,\lccode,\lowercase}
76 % \DoNotIndex{\relax,\expandafter,\endinput}
77 % \DoNotIndex{\def,\edef,\gdef,\global,\newif,\ifx,\else,\fi}
78 % \DoNotIndex{\@ifnextchar,\@let@token}
79 % \DoNotIndex{\sb,\sp,\text}
80 %
81 % \title{The \textsf{altsubsup} package\thanks{This document
82 % corresponds to \textsf{altsubsup}~\fileversion, dated \filedate.}}
83 % \author{Julien Labb\'e \\ \texttt{[email protected]}}
84 %
85 % \maketitle
86 %
87 % \begin{abstract}
88 % A \LaTeX{} package to write alternative and customisable subscripts and
89 % superscripts, with square brackets.
90 %
91 % Typical use:
92 % \begin{center}
93 % \begin{tabular}{ccc}
94 % |x_[roman]^{italic}|& $\longrightarrow$ & $x_[roman]^{italic}$ \\[6pt]
95 % |x_{italic}^[roman]|& $\longrightarrow$ & $x_{italic}^[roman]$
96 % \end{tabular}
97 % \end{center}
98 % \end{abstract}
99 %
100 % \tableofcontents{}
101 % \clearpage
102 %
103 % \section{Introduction}
104 %
105 % The \textsf{altsubsup} package allows to write alternate subscripts and
106 % superscripts, in math mode, with square brackets :
107 % \begin{center}
108 % |x_[my subscript]| \quad or \quad |x^[my superscript]|.
109 % \end{center}
110 % These alternate superscripts and superscripts are formatted by the commands
111 % set, respectively, with |\SetAltSubscriptCommand| and
112 % |\SetAltSuperscriptCommand|. By default, the |\text| command, from
113 % \textsf{amstext} (part of \textsf{amsmath}) is used. This gives:
114 % \begin{center}
115 % $x_[my subscript]$ \quad or \quad $x^[my superscript]$.
116 % \end{center}
117 %
118 % This package redefine |_| and |^| symbols. Options allow to redefine both
119 % (by default), only subscript |_| symbol, or only superscript |^| symbol.
120 %
121 % \section{Motivations}
122 %
123 % Common typographic conventions\footnotemark{} use italic (sloping) type for
124 % physical quantities or mathematical variables and roman (upright) type for
125 % words or fixed numbers. For example, heat capacity at constant pressure should
126 % be printed $C_P$, but kinetic energy $E_[k]$ (instead of $E_k$) and relative
127 % permeability $\mu_[r]$ (instead of $\mu_r$). This can be obtainted in \LaTeX{}
128 % with\footnotemark{} |E_{\mathrm{k}}| and |\mu_{\mathrm{r}}|. This package
129 % allows to write them simply |E_[k]| and |\mu_[r]|.
130 % \footnotetext{See, for example: International Organization for
131 % Standardization. (2009). \emph{Quantities and units -- Part 1: General} (ISO
132 % Standard No. 80000-1:2009).
133 % \href{https://www.iso.org/standard/30669.html}{https://www.iso.org/standard/30669.html}.}
134 % \footnotetext{Instead of \texttt{\textbackslash{}mathrm}, a best choice is
135 % the \texttt{\textbackslash{}text} macro provided by \textsf{amsmath}
136 % package, which, for example, handle spaces. It's the formatting macro used
137 % by default by the \textsf{altsubsup} package.}
138 %
139 % \section{User interface}
140 %
141 % \subsection{Usage}
142 %
143 % \DescribeMacro{\SetAltSubscriptCommand\marg{cmd}}
144 %
145 % Set the command \meta{cmd} used to format square brackets subscripts
146 % |_[...]|. By default, \meta{cmd} is the |\text| command, provided by the
147 % \textsf{amstext} package (part of \textsf{amsmath} package).
148 %
149 % \DescribeMacro{\SetAltSuperscriptCommand\marg{cmd}}
150 %
151 % Set the command \meta{cmd} used to format square brackets superscripts
152 % |^[...]|. By default, \meta{cmd} is the |\text| command, provided by the
153 % \textsf{amstext} package (part of \textsf{amsmath} package).
154 %
155 % \DescribeMacro{\SetAltSubSupCommands\marg{cmd}}
156 %
157 % Set both square brackets subscripts and square brackets superscripts, with the
158 % same command \meta{cmd}.
159 %
160 % \pagebreak
161 % \subsection{Options}
162 %
163 % To load the package, add in your preamble:
164 % \begin{center}
165 % \begin{minipage}{0.7\linewidth}
166 % |\usepackage|\oarg{option}|{altsubsup}|
167 % \end{minipage}
168 % \end{center}
169 % Available values for \meta{option}:
170 % \begin{description}
171 % \item[|subscript|] redefine only the |_| subscript symbol.
172 % \item[|superscript|] redefine only the |^| superscript symbol.
173 % \item[|both|] redefine both |_| and |^| symbols (default).
174 % \item[|spbmark|] use the \textsf{spbmark} package to handle bracket form of
175 % superscripts and superscripts (see below).
176 % \end{description}
177 %
178 % \subsubsection*{\texttt{spbmark} option}
179 %
180 % The \textsf{spbmark} package
181 % (\href{https://www.ctan.org/pkg/spbmark}{https://www.ctan.org/pkg/spbmark}),
182 % by Qu Yi, allows a complete customisation of subscripts and superscripts.
183 % With the |spbmark| option, the \textsf{altsubsup} package use the |\sub| and
184 % |\super| macros of the \textsf{spbmark} package to handle subscripts and
185 % superscripts in place of the standard |_| and |^| commands.
186 %
187 % Theses two macros are called with the respective |altsub| and |altsup| styles,
188 % allowing simple customization (these styles are initially created empty).
189 % For example, to display subscripts in blue and superscripts in red, use:
190 % \begin{center}\begin{minipage}{0.7\linewidth}
191 % \begin{verbatim}
192 % \defspbstyle{altsub}{cmd=\color{blue}}
193 % \defspbstyle{altsup}{cmd=\color{red}}
194 % \end{verbatim}
195 % \end{minipage}\end{center}
196 %
197 % A major limitation is that using simultaneously a subscript and a superscript gives
198 % bad formatting (the \textsf{spbmark} macro for this is |\supersub|).
199 % For example, |x_[sub]^[super]| gives $x\sub{\text{sub}}\super{\text{super}}$
200 % instead of $x_[sub]^[super]$.
201 %
202 % \pagebreak
203 % \section{Example}
204 %
205 % The following input:
206 % \begin{center}\begin{minipage}{0.7\linewidth}
207 % \begin{verbatim}
208 % Default:
209 % \begin{displaymath}
210 % x_a^b \quad
211 % x_{braces sub}^{braces sup} \quad
212 % x_[brackets sub]^{braces sup} \quad
213 % x_{braces sub}^[brackets sup] \quad
214 % x_[brackets sub]^[brackets sup]
215 % \end{displaymath}
216 %
217 % New formats:
218 % % \text from amstext package
219 % % \color from xcolor package
220 % \newcommand{\bluecolor}[1]{\text{\color{blue}#1}}
221 % \newcommand{\redcolor}[1] {\text{\color{red}#1}}
222 % \SetAltSubscriptCommand{\bluecolor}
223 % \SetAltSuperscriptCommand{\redcolor}
224 % \begin{displaymath}
225 % x_a^b \quad
226 % x_{braces sub}^{braces sup} \quad
227 % x_[brackets sub]^{braces sup} \quad
228 % x_{braces sub}^[brackets sup] \quad
229 % x_[brackets sub]^[brackets sup]
230 % \end{displaymath}
231 %
232 % Same command for subscripts and superscripts:
233 % \SetAltSubSupCommands{\mathbf}
234 % \begin{displaymath}
235 % x_a^b \quad
236 % x_{braces sub}^{braces sup} \quad
237 % x_[brackets sub]^{braces sup} \quad
238 % x_{braces sub}^[brackets sup] \quad
239 % x_[brackets sub]^[brackets sup]
240 % \end{displaymath}
241 % \end{verbatim}
242 % \end{minipage}\end{center}
243 %
244 % gives:
245 % \begin{center}\begin{minipage}{0.7\linewidth}
246 % Default:
247 % \begin{displaymath}
248 % x_a^b \quad
249 % x_{braces sub}^{braces sup} \quad
250 % x_[brackets sub]^{braces sup} \quad
251 % x_{braces sub}^[brackets sup] \quad
252 % x_[brackets sub]^[brackets sup]
253 % \end{displaymath}
254 %
255 % New formats:
256 % \newcommand{\bluecolor}[1]{\text{\color{blue}#1}}
257 % \newcommand{\redcolor}[1] {\text{\color{red}#1}}
258 % \SetAltSubscriptCommand{\bluecolor}
259 % \SetAltSuperscriptCommand{\redcolor}
260 % \begin{displaymath}
261 % x_a^b \quad
262 % x_{braces sub}^{braces sup} \quad
263 % x_[brackets sub]^{braces sup} \quad
264 % x_{braces sub}^[brackets sup] \quad
265 % x_[brackets sub]^[brackets sup]
266 % \end{displaymath}
267 %
268 % Same command for subscripts and superscripts:
269 % \SetAltSubSupCommands{\mathbf}
270 % \begin{displaymath}
271 % x_a^b \quad
272 % x_{braces sub}^{braces sup} \quad
273 % x_[brackets sub]^{braces sup} \quad
274 % x_{braces sub}^[brackets sup] \quad
275 % x_[brackets sub]^[brackets sup]
276 % \end{displaymath}
277 % \end{minipage}\end{center}
278 %
279 % \section{Complements}
280 %
281 % \subsection{Known issue}
282 %
283 % The use of the prime symbol |'| can raise the \emph{Double superscript}
284 % error message. This is normally fixed (|x'^2| gives $x'^2$ correctly).
285 % If needed, enclose the expression with |{...}|. In particular, |x'^[sup]|
286 % doesn't work, and should be written: |{x'}^[sup]|.
287 %
288 % \subsection{Alternative}
289 %
290 % \begin{description}
291 % \item[the \textsf{subtext} package]
292 % (\href{https://www.ctan.org/pkg/subtext}{https://www.ctan.org/pkg/subtext}),
293 % by Palle J\o{}rgensen, formats |_[...]| subscripts with |\text| (the
294 % differences, is that the \textsf{altsubsup} package works both for
295 % subscripts and superscripts, allows to customise the commands, and
296 % redefine symbols only in math mode).
297 % \end{description}
298 %
299 % \subsection{Changelog}
300 %
301 % \begin{description}
302 % \item[v1.1]
303 % \begin{itemize}
304 % \item Backup standard subscript |_| and superscript |^| commands to handle
305 % packages that redefine |\sb| or |\sp| macros, as \textsf{spbmark}.
306 % \item Add option |spbmark| to format subscripts and superscripts with the
307 % \textsf{spbmark} package.
308 % \end{itemize}
309 % \item[v1.0] Initial version.
310 % \end{description}
311
312 % \StopEventually{}
313 % \section{Implementation}
314 %
315 % \subsection*{Package declaration}
316 %
317 % \begin{macrocode}
318 \ProvidesPackage{altsubsup}[2022/03/15, v1.1, Alternative and customisable
319 subscripts and superscripts, with square brackets.]
320 % \end{macrocode}
321 %
322 % \subsection*{Flags declaration}
323 %
324 % \subsubsection*{Determine the commands that will be redefined}
325 %
326 % \begin{macrocode}
327 \newif\ifaltsbsp@subscript \altsbsp@subscripttrue
328 \newif\ifaltsbsp@superscript \altsbsp@superscripttrue
329 % \end{macrocode}
330 %
331 % \subsubsection*{Use the spbmark mechanism}
332 %
333 % \begin{macrocode}
334 \newif\ifaltsbsp@spbmark \altsbsp@spbmarkfalse
335 % \end{macrocode}
336 %
337 % \subsection*{Options declarations and processing}
338 %
339 % \changes{v1.1}{2022/03/15}{Add sbpmark option}
340 % \begin{macrocode}
341 \DeclareOption{subscript} {\altsbsp@subscripttrue \altsbsp@superscriptfalse}
342 \DeclareOption{superscript} {\altsbsp@subscriptfalse \altsbsp@superscripttrue }
343 \DeclareOption{both} {\altsbsp@subscripttrue \altsbsp@superscripttrue }
344 \DeclareOption{spbmark} {\altsbsp@spbmarktrue}
345 \DeclareOption*{\PackageWarning{altsubsup}{Unknown option \CurrentOption.}}
346 \ProcessOptions\relax
347 \ifaltsbsp@spbmark
348 \RequirePackage{spbmark}
349 \fi
350 % \end{macrocode}
351 %
352 % \subsection*{Backup standard superscript and subscript commands}
353 %
354 % \changes{v1.1}{2022/03/15}{Backup standard superscript and superscript commands}
355 %
356 % \begin{macrocode}
357 \AtBeginDocument{%
358 \begingroup\catcode`\_=8 \global\let\altsbsp@standardsub=_\endgroup
359 \begingroup\catcode`\^=7 \global\let\altsbsp@standardsup=^\endgroup
360 % \end{macrocode}
361 %
362 % \subsection*{Redefine catcodes and make symbols active in mathmode}
363 %
364 % \begin{macrocode}
365 \ifaltsbsp@subscript \catcode`\_=12 \mathcode`\_="8000 \fi%
366 \ifaltsbsp@superscript \catcode`\^=12 \mathcode`\^="8000 \fi%
367 }
368 % \end{macrocode}
369 %
370 % \subsection*{Redefinition of the subscript symbol}
371 %
372 % \begin{macrocode}
373 \ifaltsbsp@subscript%
374 \begingroup\lccode`\~=`\_\lowercase{\endgroup%
375 \def~}{\@ifnextchar[% dummy bracket ]
376 {\altsbsp@subwrapper}% bracket wrapper
377 {\altsbsp@standardsub}% standard form
378 }%
379 \fi
380 % \end{macrocode}
381 %
382 % \subsection*{Redefinition of the superscript symbol}
383 %
384 % \begin{macrocode}
385 \ifaltsbsp@superscript%
386 \begingroup\lccode`\~=`\^\lowercase{\endgroup%
387 \def~}{\@ifnextchar[% dummy bracket ]
388 {\altsbsp@supwrapper}% bracket wrapper
389 {\altsbsp@standardsup}% standard form
390 }%
391 \fi
392 % \end{macrocode}
393 %
394 % \subsection*{User macros}
395 %
396 % \begin{macro}{\SetAltSubscriptCommand}
397 % \begin{macrocode}
398 \def\SetAltSubscriptCommand#1{\let\altsbsp@altsubcmd#1}%
399 % \end{macrocode}
400 % \end{macro}
401 %
402 % \begin{macrocode}
403 \ifaltsbsp@spbmark%
404 \defspbstyle{altsub}{}
405 \def\altsbsp@subwrapper[#1]{\sub[style=altsub]{\altsbsp@altsubcmd{#1}}}%
406 \else
407 \def\altsbsp@subwrapper[#1]{\altsbsp@standardsub{\altsbsp@altsubcmd{#1}}}%
408 \fi
409 % \end{macrocode}
410 %
411 % \begin{macro}{\SetAltSuperscriptCommand}
412 % \begin{macrocode}
413 \def\SetAltSuperscriptCommand#1{\let\altsbsp@altsupcmd#1}%
414 % \end{macrocode}
415 % \end{macro}
416 %
417 % \begin{macrocode}
418 \ifaltsbsp@spbmark%
419 \defspbstyle{altsup}{}
420 \def\altsbsp@supwrapper[#1]{\super[style=altsup]{\altsbsp@altsupcmd{#1}}}%
421 \else
422 \def\altsbsp@supwrapper[#1]{\altsbsp@standardsup{\altsbsp@altsupcmd{#1}}}%
423 \fi
424 % \end{macrocode}
425 %
426 % \begin{macro}{\SetAltSubSupCommands}
427 % \begin{macrocode}
428 \newcommand{\SetAltSubSupCommands}[1]{%
429 \SetAltSubscriptCommand{#1}%
430 \SetAltSuperscriptCommand{#1}%
431 }
432 % \end{macrocode}
433 % \end{macro}
434 %
435 % \subsection*{Set default commands}
436 %
437 % \begin{macrocode}
438 \RequirePackage{amstext}%
439 \SetAltSubSupCommands{\text}%
440 % \end{macrocode}
441 %
442 % \subsection*{Fix prime symbol}
443 %
444 % \begin{macrocode}
445 \ifaltsbsp@superscript%
446 \begingroup \catcode`\^=12%
447 \gdef\altsbsp@pr@m@s{% copy of \@pr@m@s code from latex.ltx
448 \ifx'\@let@token
449 \expandafter\pr@@@s
450 \else
451 \ifx^\@let@token
452 \expandafter\expandafter\expandafter\pr@@@t
453 \else
454 \egroup
455 \fi
456 \fi}
457 \endgroup
458 \let\pr@m@s\altsbsp@pr@m@s
459 \fi
460 % \end{macrocode}
461 %
462 % \subsection*{End of the package}
463 %
464 % \begin{macrocode}
465 \endinput
466 % \end{macrocode}
467 % \Finale
468 \endinput