%%
%% This is file `mathstyle.sty',
%% generated with the docstrip utility.
%%
%% The original source files were:
%%
%% mathstyle.dtx  (with options: `package')
%% 
%% This is a generated file.
%% 
%% 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 breqn.dtx,
%% flexisym.dtx, and mathstyle.dtx.
%% 
\NeedsTeXFormat{LaTeX2e}
\RequirePackage{expl3}
\ProvidesExplPackage{mathstyle}{2021/10/28}{0.98l}{Tracking mathstyle implicitly}
\ExplSyntaxOff
\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
}
\begingroup\expandafter\expandafter\expandafter\endgroup
\expandafter\ifx\csname directlua\endcsname\relax
  \everydisplay=\expandafter{\the\everydisplay\chardef\mathstyle\z@}
\fi
\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
\@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
\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
\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=_
}%
\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
\let\mathsup=\@@superscript
\let\mathsub=\@@subscript
\def\sb#1{\mathsub{\protect\subsupstyle#1}}%
\def\sp#1{\mathsup{\protect\subsupstyle#1}}%
\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
}
\providecommand\@firstoffour[4]{#1}
\providecommand\@secondoffour[4]{#2}
\providecommand\@thirdoffour[4]{#3}
\providecommand\@fourthoffour[4]{#4}
\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
\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}

\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
\def\currentmathstyle{%
  \ifcase\numexpr\mathstyle\relax
    \@@displaystyle
  \or
    \@@displaystyle
  \or
    \@@textstyle
  \or
    \@@textstyle
  \or
    \@@scriptstyle
  \or
    \@@scriptstyle
  \else
    \@@scriptscriptstyle
  \fi}
\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
\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
\endinput
%%
%% End of file `mathstyle.sty'.
