Debian Codegraph @

texmf-dist/source/latex/breqn/mathstyle.dtx

raw
1 % \iffalse meta-comment
2 %
3 % Copyright (C) 1997-2003 by Michael J. Downes
4 % Copyright (C) 2007-2008 by Morten Hoegholm
5 % Copyright (C) 2007-2014 by Lars Madsen
6 % Copyright (C) 2007-2020 by Will Robertson
7 % Copyright (C) 2010-2017 by Joseph Wright
8 % Copyright (C) 2020-2020 by Ulrike Fischer
9 %
10 % This work may be distributed and/or modified under the
11 % conditions of the LaTeX Project Public License, either
12 % version 1.3 of this license or (at your option) any later
13 % version. The latest version of this license is in
14 % http://www.latex-project.org/lppl.txt
15 % and version 1.3 or later is part of all distributions of
16 % LaTeX version 2005/12/01 or later.
17 %
18 % This work has the LPPL maintenance status "maintained".
19 %
20 % The Current Maintainer of this work is Will Robertson.
21 %
22 % This work consists of the main source file mathstyle.dtx
23 % and the derived files
24 % mathstyle.sty, mathstyle.pdf, mathstyle.ins, mathstyle.drv.
25 %
26 % Distribution:
27 % CTAN:macros/latex/contrib/mh/mathstyle.dtx
28 % CTAN:macros/latex/contrib/mh/mathstyle.pdf
29 %
30 % Unpacking:
31 % tex breqnbundle.ins
32 %
33 % Documentation:
34 % The class ltxdoc loads the configuration file ltxdoc.cfg
35 % if available. Here you can specify further options, e.g.
36 % use A4 as paper format:
37 % \PassOptionsToClass{a4paper}{article}
38 %
39 % Programm calls to get the documentation (example):
40 % pdflatex mathstyle.dtx
41 % makeindex -s gind.ist mathstyle.idx
42 % pdflatex mathstyle.dtx
43 % makeindex -s gind.ist mathstyle.idx
44 % pdflatex mathstyle.dtx
45 %
46 % Installation:
47 % TDS:tex/latex/breqn/mathstyle.sty
48 % TDS:doc/latex/breqn/mathstyle.pdf
49 % TDS:source/latex/breqn/mathstyle.dtx
50 %
51 %<*driver>
52 \documentclass{ltxdoc}
53 \providecommand*\pkg[1]{\textsf{#1}}
54 \begin{document}
55 \DocInput{mathstyle.dtx}
56 \end{document}
57 %</driver>
58 % \fi
59 %
60 % \title{The \textsf{mathstyle} package}
61 % \def\fileversion{0.98l}
62 % \def\filedate{2021/10/28}
63 % \date{\filedate\quad\fileversion}
64 % \author{Authors: Michael J. Downes, Morten H\o gholm\\ Maintained by Morten H\o gholm, Will Robertson\\ Feedback: \texttt{https://github.com/wspr/breqn/issues}}
65 %
66 %
67 % \maketitle
68 %
69 % \part*{User's guide}
70 %
71 % This package exists for two reasons:
72 % \begin{itemize}
73 % \item The primitive operations for creating a super- or subscript in
74 % \TeX\ work almost as if \verb|^| and \verb|_| are macros taking an
75 % argument. However, that is not quite the case, and
76 % some things that you'd expect to work don't (e.g., \verb|^\cong|)
77 % whereas others which you'd think shouldn't work actually
78 % do (such as |^\mathsf{s}|). We do everyone a favor if it behaves
79 % consistently, i.e., if the superscript and subscript operations
80 % act as if they are macros taking exactly one argument.
81 %
82 % \item Because the \TeX\ math typesetting engine uses infix notation
83 % for fractions, one has to use \cs{mathchoice} or \cs{mathpalette}
84 % whenever trying to do anything requiring boxing or measuring
85 % math. This creates problems for loading fonts on demand as the
86 % font loading mechanism has to load fonts for all styles without
87 % even knowing if the font is going to be used. Getting the timing
88 % of \cs{mathchoice} right can be tricky as well. Since \LaTeX\ does
89 % not promote the primitive infix notation, this package keeps track
90 % of a current mathstyle parameter.
91 % \end{itemize}
92 %
93 %
94 % \section{Some usage tips}
95 %
96 % If you want to use this package with \pkg{amsmath}, it is important
97 % \pkg{mathstyle} is loaded \emph{after} \pkg{amsmath}.
98 %
99 % The current mathstyle is stored in the variable \cs{mathstyle}. The
100 % command \cs{currentmathstyle} can be used to switch to the mode
101 % currently active. Below is shown how the macro \cs{mathrlap} from
102 % \pkg{mathtools} is implemented without knowing about the current
103 % mathstyle using \cs{mathpalette}.
104 % \begin{verbatim}
105 % \providecommand*\mathrlap[1][]{%
106 % \ifx\@empty#1\@empty
107 % \expandafter \mathpalette \expandafter \@mathrlap
108 % \else
109 % \expandafter \@mathrlap \expandafter #1%
110 % \fi}
111 % \providecommand*\@mathrlap #1#2{{}\rlap{$\m@th#1{#2}$}}
112 % \end{verbatim}
113 % The same definition using \cs{currentmathstyle} from this package.
114 % \begin{verbatim}
115 % \providecommand*\mathrlap[2][]{%
116 % #1 {}\rlap{$\m@th \currentmathstyle {#2}$}}
117 % \end{verbatim}
118 %
119 % \subsection{Package options}
120 %
121 % This package has one set of options affecting the \verb|_| and \verb|^| characters:
122 %
123 % \begin{itemize}
124 % \item\verb|\usepackage[mathactivechars]{mathstyle}|
125 %
126 % This is the default behaviour. Here, \verb|_| and \verb|^| are made into harmless
127 % characters in text mode and behave as expected (for entering sub/superscript) when
128 % inside math mode.
129 % Certain code that assumes the catcodes of these characters may get confused about
130 % this; see below for a possible fix.
131 %
132 % \item\verb|\usepackage[activechars]{mathstyle}|
133 %
134 % With this option, \verb|_| and \verb|^| are made into active characters for
135 % entering sub/superscript mode in all cases---therefore, in text mode they will
136 % produce a regular error (`Missing \$ inserted') indicating they are being used
137 % out of place.
138 %
139 % \item\verb|\usepackage[noactivechars]{mathstyle}|
140 %
141 % This is the option most like to solve any compatibility problems. Here,
142 % \verb|_| and \verb|^| retain their regular catcodes at all times and behave
143 % in their default fashion. \textbf{However}, certain other features of this
144 % package (such as \cs{currentmathstyle} inside a subscript) will then fail
145 % to work, so only use this option as a last resort.
146 % \end{itemize}
147 %
148 % \StopEventually{}
149 % \part*{Implementation}
150 %
151 % \begin{macrocode}
152 %<*package>
153 \NeedsTeXFormat{LaTeX2e}
154 \RequirePackage{expl3}
155 \ProvidesExplPackage{mathstyle}{2021/10/28}{0.98l}{Tracking mathstyle implicitly}
156 \ExplSyntaxOff
157 % \end{macrocode}
158 %
159 % \begin{macro}{\@saveprimitive}
160 % A straight copy from \pkg{breqn}, see implementation details
161 % there. Of course, with a recent pdf\TeX\ (v1.40+), one can just
162 % use \cs{primitive} to get the original. We will implement that
163 % some day.
164 % \begin{macrocode}
165 \providecommand\@saveprimitive[2]{%
166 \begingroup
167 \edef\@tempa{\string#1}\edef\@tempb{\meaning#1}%
168 \ifx\@tempa\@tempb \global\let#2#1%
169 \else
170 \edef\@tempb{\meaning#2}%
171 \ifx\@tempa\@tempb
172 \else \@saveprimitive@a#1#2%
173 \fi
174 \fi
175 \endgroup
176 }
177 \providecommand\@saveprimitive@a[2]{%
178 \begingroup
179 \def\@tempb##1#1##2{\edef\@tempb{##2}\@car{}}%
180 \@tempb\nullfont{select font nullfont}%
181 \topmark{\string\topmark:}%
182 \firstmark{\string\firstmark:}%
183 \botmark{\string\botmark:}%
184 \splitfirstmark{\string\splitfirstmark:}%
185 \splitbotmark{\string\splitbotmark:}%
186 #1{\string#1}%
187 \@nil % for the \@car
188 \edef\@tempa{\expandafter\strip@prefix\meaning\@tempb}%
189 \edef\@tempb{\meaning#1}%
190 \ifx\@tempa\@tempb \global\let#2#1%
191 \else
192 \PackageError{mathstyle}%
193 {Unable to properly define \string#2; primitive
194 \noexpand#1no longer primitive}\@eha
195 \fi
196 \fi
197 \endgroup
198 }
199 % \end{macrocode}
200 % \end{macro}
201 %
202 % \begin{macro}{\everydisplay}
203 % We need to keep track of whether we're in inline or display maths, and the only
204 % way to do that is to add a switch inside \verb|\everydisplay|.
205 % We act sensibly and preserve any of the previous contents of that token register
206 % before adding our own code here. As we'll see in a second, Lua\TeX{}
207 % provides a native mechanism for this so we don't need any action in that
208 % case. (Various other parts of the code also need to have different paths
209 % for Lua\TeX{} use.)
210 % \begin{macrocode}
211 \begingroup\expandafter\expandafter\expandafter\endgroup
212 \expandafter\ifx\csname directlua\endcsname\relax
213 \everydisplay=\expandafter{\the\everydisplay\chardef\mathstyle\z@}
214 \fi
215 % \end{macrocode}
216 % \end{macro}
217 %
218 % \begin{macro}{\mathstyle}
219 % \begin{macro}{\mathstyledenom}
220 % A counter for the math style: 0--display, 2--text, 4--script, 6--scriptscript.
221 % The logic is that display maths will explicitly
222 % set \verb|\mathstyle| to zero (see above), so by default it is set to the
223 % `text' maths style. With Lua\TeX{} there is a primitive to do the same
224 % so it just has to be enabled. Note that in all cases we use Lua\TeX{}-like
225 % numbering for the states.
226 % \begin{macrocode}
227 \begingroup\expandafter\expandafter\expandafter\endgroup
228 \expandafter\ifx\csname directlua\endcsname\relax
229 \chardef\mathstyle=2\relax
230 \chardef\mathstyledenom=0\relax
231 \else
232 \directlua{tex.enableprimitives("", {"mathstyle"})}
233 \fi
234 % \end{macrocode}
235 % \end{macro}
236 % \end{macro}
237 %
238 % Save the four style changing primitives, \cs{mathchoice} and the
239 % fraction commands.
240 % \begin{macrocode}
241 \@saveprimitive\displaystyle\@@displaystyle
242 \@saveprimitive\textstyle\@@textstyle
243 \@saveprimitive\scriptstyle\@@scriptstyle
244 \@saveprimitive\scriptscriptstyle\@@scriptscriptstyle
245 \@saveprimitive\mathchoice\@@mathchoice
246 \@saveprimitive\over\@@over
247 \@saveprimitive\atop\@@atop
248 \@saveprimitive\above\@@above
249 \@saveprimitive\overwithdelims\@@overwithdelims
250 \@saveprimitive\atopwithdelims\@@atopwithdelims
251 \@saveprimitive\abovewithdelims\@@abovewithdelims
252 % \end{macrocode}
253 % Then we redeclare the four style changing primitives: set the value of
254 % \cs{mathstyle} if Lua\TeX{} is not in use.
255 % \begin{macrocode}
256 \begingroup\expandafter\expandafter\expandafter\endgroup
257 \expandafter\ifx\csname directlua\endcsname\relax
258 \DeclareRobustCommand{\displaystyle}{%
259 \@@displaystyle \chardef\mathstyle0\relax}
260 \DeclareRobustCommand{\textstyle}{%
261 \@@textstyle \chardef\mathstyle2\relax}
262 \DeclareRobustCommand{\scriptstyle}{%
263 \@@scriptstyle \chardef\mathstyle4\relax}
264 \DeclareRobustCommand{\scriptscriptstyle}{%
265 \@@scriptscriptstyle \chardef\mathstyle6\relax}
266 \fi
267 % \end{macrocode}
268 % First we get the primitive operations. These should have been
269 % control sequences in \TeX\ just like operations for begin math, end
270 % math, begin display, end display.
271 % \begin{macrocode}
272 \begingroup \catcode`\^=7\relax \catcode`\_=8\relax % just in case
273 \lowercase{\endgroup
274 \let\@@superscript=^ \let\@@subscript=_
275 }%
276 \begingroup \catcode`\^=12\relax \catcode`\_=12\relax % just in case
277 \lowercase{\endgroup
278 \let\@@superscript@other=^ \let\@@subscript@other=_
279 }%
280 % \end{macrocode}
281 % If we enter a sub- or superscript the \cs{mathstyle} must be
282 % adjusted. Since all is happening in a group, we do not have to worry
283 % about resetting. We can't tell the difference between cramped and
284 % non-cramped styles unless Lua\TeX{} is in use, in which case this command
285 % is a no-op.
286 % \begin{macrocode}
287 \begingroup\expandafter\expandafter\expandafter\endgroup
288 \expandafter\ifx\csname directlua\endcsname\relax
289 \def\subsupstyle{%
290 \ifnum\mathstyle<4\relax
291 \chardef\mathstyle\numexpr4+\mathstyledenom\relax
292 \else
293 \chardef\mathstyle\numexpr6+\mathstyledenom\relax
294 \fi
295 }
296 \else
297 \def\subsupstyle{}
298 \fi
299 % \end{macrocode}
300 % Provide commands with meaningful names for the two primitives, cf.\
301 % \cs{mathrel}.
302 % \begin{macrocode}
303 \let\mathsup=\@@superscript
304 \let\mathsub=\@@subscript
305 % \end{macrocode}
306 % \cs{sb} and \cs{sp} are then defined as macros.
307 % \begin{macrocode}
308 \def\sb#1{\mathsub{\protect\subsupstyle#1}}%
309 \def\sp#1{\mathsup{\protect\subsupstyle#1}}%
310 % \end{macrocode}
311 %
312 % \begin{macro}{\mathchoice}
313 % \changes{v0.98e}{2016/04/09}{Lua\TeX{} primitive \cs{mathstyle} needs
314 % termination}
315 % \cs{mathchoice} is now just a switch. Note that this redefinition
316 % does not allow the arbitrary \meta{filler} of the \TeX\
317 % primitive. Very rarely used anyway.
318 % \begin{macrocode}
319 \def\mathchoice{%
320 \relax\ifcase\numexpr\mathstyle\relax
321 \expandafter\@firstoffour % Display
322 \or
323 \expandafter\@firstoffour % Cramped display
324 \or
325 \expandafter\@secondoffour % Text
326 \or
327 \expandafter\@secondoffour % Cramped text
328 \or
329 \expandafter\@thirdoffour % Script
330 \or
331 \expandafter\@thirdoffour % Cramped script
332 \else
333 \expandafter\@fourthoffour % (Cramped) Scriptscript
334 \fi
335 }
336 % \end{macrocode}
337 % Helper macros.
338 % \begin{macrocode}
339 \providecommand\@firstoffour[4]{#1}
340 \providecommand\@secondoffour[4]{#2}
341 \providecommand\@thirdoffour[4]{#3}
342 \providecommand\@fourthoffour[4]{#4}
343 % \end{macrocode}
344 % \end{macro}
345 %
346 % \begin{macro}{\genfrac}
347 % The amsmath definition:
348 % \begin{verbatim}
349 % \DeclareRobustCommand{\genfrac}[4]{%
350 % \def\@tempa{#1#2}%
351 % \edef\@tempb{\@nx\@genfrac\@mathstyle{#4}%
352 % \csname @@\ifx @#3@over\else above\fi
353 % \ifx\@tempa\@empty \else withdelims\fi\endcsname}
354 % \@tempb{#1#2#3}}
355 % \end{verbatim}
356 % with arguments:
357 % \begin{itemize}
358 % \item left-delim
359 % \item right-delim
360 % \item line thickness (default if empty)
361 % \item mathstyle override
362 % \item numerator
363 % \item denominator
364 % \end{itemize}
365 % The fractions. Note that this uses the same names as in
366 % \pkg{amsmath}. Much the same except here they call \cs{fracstyle}.
367 % \begin{macrocode}
368 \DeclareRobustCommand\genfrac[6]{%
369 {%
370 % emulate old amsmath syntax:
371 \if 0#4\relax\displaystyle\else
372 \if 1#4\relax\textstyle\else
373 \if 2#4\relax\scriptstyle\else
374 \if 3#4\relax\scriptscriptstyle\else
375 #4%
376 \fi\fi\fi\fi
377 \fracstyle
378 {\begingroup #5\endgroup
379 \csname @@\ifx\maxdimen#3\maxdimen over\else above\fi
380 \if @#1@\else withdelims\fi\endcsname #1 #2 #3\relax
381 \ifnum\mathstyledenom=0\relax
382 \chardef\mathstyledenom=1\relax
383 \edef\mathstyle@tempa{\number\mathstyle}%
384 \chardef\mathstyle=\numexpr\mathstyle@tempa+1\relax
385 \fi
386 #6%
387 \chardef\mathstyledenom=0\relax}%
388 }%
389 }
390 \begingroup\expandafter\expandafter\expandafter\endgroup
391 \expandafter\ifx\csname directlua\endcsname\relax\else
392 \DeclareRobustCommand\genfrac[6]{%
393 {%
394 % emulate old amsmath syntax:
395 \if 0#4\relax\displaystyle\else
396 \if 1#4\relax\textstyle\else
397 \if 2#4\relax\scriptstyle\else
398 \if 3#4\relax\scriptscriptstyle\else
399 #4%
400 \fi\fi\fi\fi
401 \fracstyle
402 {\begingroup #5\endgroup
403 \csname @@\ifx\maxdimen#3\maxdimen over\else above\fi
404 \if @#1@\else withdelims\fi\endcsname #1 #2 #3\relax
405 #6%
406 }%
407 }%
408 }
409 \fi
410 % \end{macrocode}
411 % \changes{v0.90}{2011/08/03}{\cs{fracstyle} must be called \emph{after}
412 % changing to the required style}
413 % \changes{v0.98e}{2016/04/09}{Lua\TeX{} primitive \cs{mathstyle} needs
414 % termination}
415 % \end{macro}
416 %
417 % \begin{macrocode}
418 \renewcommand{\frac}{\genfrac{}{}{}{}}
419 \providecommand{\dfrac}{}
420 \providecommand{\tfrac}{}
421 \renewcommand{\dfrac}{\genfrac{}{}{}\displaystyle}
422 \renewcommand{\tfrac}{\genfrac{}{}{}\displaystyle}
423 \providecommand{\binom}{}
424 \providecommand{\tbinom}{}
425 \providecommand{\dbinom}{}
426 \renewcommand{\binom}{\genfrac(){0pt}{}}
427 \renewcommand{\dbinom}{\genfrac(){0pt}\displaystyle}
428 \renewcommand{\tbinom}{\genfrac(){0pt}\textstyle}
429 % \end{macrocode}
430
431 % The \cs{fracstyle} command is a switch to go one level down but no
432 % further than three.
433 % \begin{macrocode}
434 \begingroup\expandafter\expandafter\expandafter\endgroup
435 \expandafter\ifx\csname directlua\endcsname\relax
436 \def\fracstyle{%
437 \ifcase\numexpr\mathstyle\relax
438 \chardef\mathstyle=0\relax % 0
439 \or \chardef\mathstyle=1\relax % 1
440 \or \chardef\mathstyle=2\relax % 2
441 \or \chardef\mathstyle=3\relax % 3
442 \else \chardef\mathstyle=3\relax % 4 or more
443 \fi
444 }
445 \else
446 \def\fracstyle{}
447 \fi
448 % \end{macrocode}
449 % \changes{v0.98e}{2016/04/09}{Lua\TeX{} primitive \cs{mathstyle} needs
450 % termination}
451 % The \cs{currentmathstyle} checks the value of \cs{mathstyle} and
452 % switches to it so it is in essence the opposite of \cs{displaystyle}
453 % and friends.
454 % \begin{macrocode}
455 \def\currentmathstyle{%
456 \ifcase\numexpr\mathstyle\relax
457 \@@displaystyle
458 \or
459 \@@displaystyle
460 \or
461 \@@textstyle
462 \or
463 \@@textstyle
464 \or
465 \@@scriptstyle
466 \or
467 \@@scriptstyle
468 \else
469 \@@scriptscriptstyle
470 \fi}
471 % \end{macrocode}
472 % Finally, we declare the package options.
473 % \changes{v0.89}{2010/11/17}{Options should only change catcodes at
474 % begin document, not straight away}
475 % \begin{macrocode}
476 \DeclareOption{mathactivechars}{%
477 % \catcode`\^=12\relax
478 % \catcode`\_=12\relax
479 \AtBeginDocument{\catcode`\^=12\relax \catcode`\_=12\relax}%
480 }
481 \DeclareOption{activechars}{%
482 % \catcode`\^=13\relax
483 % \catcode`\_=13\relax
484 \AtBeginDocument{\catcode`\^=13\relax \catcode`\_=13\relax}%
485 }
486 \DeclareOption{noactivechars}{%
487 % \catcode`\^=7\relax
488 % \catcode`\_=8\relax
489 \AtBeginDocument{\catcode`\^=7\relax \catcode`\_=8\relax}%
490 }
491 \ExecuteOptions{mathactivechars}
492 \ProcessOptions\relax
493 % \end{macrocode}
494 % WSPR: Set up the active behaviours: (this is set even in the
495 % |noactivechars| case but they are never activated. no worries?)
496 % \begin{macrocode}
497 \ifnum\catcode`\^=13\relax
498 \let^=\sp \let_=\sb
499 \else
500 \mathcode`\^="8000\relax
501 \mathcode`\_="8000\relax
502 \begingroup
503 \catcode`\^=\active
504 \catcode`\_=\active
505 \global\let^=\sp
506 \global\let_=\sb
507 \endgroup
508 \fi
509 %</package>
510 % \end{macrocode}
511 %
512 % \PrintIndex
513 %
514 % \Finale