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

% The \cs{fracstyle} command is a switch to go one level down but no
% further than three.
%    \begin{macrocode}
\begingroup\expandafter\expandafter\expandafter\endgroup
\expandafter\ifx\csname directlua\endcsname\relax
  \def\fracstyle{%
    \ifcase\numexpr\mathstyle\relax
          \chardef\mathstyle=0\relax % 0
    \or   \chardef\mathstyle=1\relax % 1
    \or   \chardef\mathstyle=2\relax % 2
    \or   \chardef\mathstyle=3\relax % 3
    \else \chardef\mathstyle=3\relax % 4 or more
    \fi
  }
\else
  \def\fracstyle{}
\fi
%    \end{macrocode}
% \changes{v0.98e}{2016/04/09}{Lua\TeX{} primitive \cs{mathstyle} needs
%   termination}
% The \cs{currentmathstyle} checks the value of \cs{mathstyle} and
% switches to it so it is in essence the opposite of \cs{displaystyle}
% and friends.
%    \begin{macrocode}
\def\currentmathstyle{%
  \ifcase\numexpr\mathstyle\relax
    \@@displaystyle
  \or
    \@@displaystyle
  \or
    \@@textstyle
  \or
    \@@textstyle
  \or
    \@@scriptstyle
  \or
    \@@scriptstyle
  \else
    \@@scriptscriptstyle
  \fi}
%    \end{macrocode}
% Finally, we declare the package options.
% \changes{v0.89}{2010/11/17}{Options should only change catcodes at
% begin document, not straight away}
%    \begin{macrocode}
\DeclareOption{mathactivechars}{%
 %  \catcode`\^=12\relax
 %  \catcode`\_=12\relax
\AtBeginDocument{\catcode`\^=12\relax \catcode`\_=12\relax}%
}
\DeclareOption{activechars}{%
 %  \catcode`\^=13\relax
 %  \catcode`\_=13\relax
\AtBeginDocument{\catcode`\^=13\relax \catcode`\_=13\relax}%
}
\DeclareOption{noactivechars}{%
 %  \catcode`\^=7\relax
 %  \catcode`\_=8\relax
\AtBeginDocument{\catcode`\^=7\relax \catcode`\_=8\relax}%
}
\ExecuteOptions{mathactivechars}
\ProcessOptions\relax
%    \end{macrocode}
% WSPR: Set up the active behaviours: (this is set even in the
% |noactivechars| case but they are never activated. no worries?)
%    \begin{macrocode}
\ifnum\catcode`\^=13\relax
  \let^=\sp \let_=\sb
\else
  \mathcode`\^="8000\relax
  \mathcode`\_="8000\relax
  \begingroup
    \catcode`\^=\active
    \catcode`\_=\active
    \global\let^=\sp
    \global\let_=\sb
  \endgroup
\fi
%</package>
%    \end{macrocode}
%
% \PrintIndex
%
% \Finale