% \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-2014 by Will Robertson
%
% 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:
%    (a) If mathstyle.ins is present:
%           tex mathstyle.ins
%    (b) Without mathstyle.ins:
%           tex mathstyle.dtx
%    (c) If you insist on using LaTeX
%           latex \let\install=y\input{mathstyle.dtx}
%        (quote the arguments according to the demands of your shell)
%
% 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
%
%<*ignore>
\begingroup
  \def\x{LaTeX2e}
\expandafter\endgroup
\ifcase 0\ifx\install y1\fi\expandafter
         \ifx\csname processbatchFile\endcsname\relax\else1\fi
         \ifx\fmtname\x\else 1\fi\relax
\else\csname fi\endcsname
%</ignore>
%<*install>
\input docstrip.tex
\Msg{************************************************************************}
\Msg{* Installation for package: mathstyle}
\Msg{************************************************************************}

\keepsilent
\askforoverwritefalse

\preamble

This is a generated file.

Copyright (C) 1997-2003 by Michael J. Downes
Copyright (C) 2007-2011 by Morten Hoegholm et al
Copyright (C) 2007-2014 by Lars Madsen
Copyright (C) 2007-2014 by Will Robertson

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.

\endpreamble

\generate{%
  \file{mathstyle.ins}{\from{mathstyle.dtx}{install}}%
  \usedir{tex/latex/breqn}%
  \file{mathstyle.sty}{\from{mathstyle.dtx}{package}}%
}

\obeyspaces
\Msg{************************************************************************}
\Msg{*}
\Msg{* To finish the installation you have to move the following}
\Msg{* file into a directory searched by TeX:}
\Msg{*}
\Msg{*     mathstyle.sty}
\Msg{*}
\Msg{* Happy TeXing!}
\Msg{*}
\Msg{************************************************************************}

\endbatchfile
%</install>
%<*ignore>
\fi
%</ignore>
%
%<*driver>
\ProvidesFile{mathstyle.drv}
%</driver>
%<package>\NeedsTeXFormat{LaTeX2e}
%<package>\ProvidesPackage{mathstyle}
%<*package|driver>
  [2014/06/10 v0.90a Tracking mathstyle implicitly]
%</package|driver>
%<*driver>
\documentclass{ltxdoc}
\CodelineIndex
\EnableCrossrefs
\setcounter{IndexColumns}{2}
\providecommand*\pkg[1]{\textsf{#1}}
\begin{document}
  \DocInput{mathstyle.dtx}
\end{document}
%</driver>
% \fi
%
% \GetFileInfo{mathstyle.drv}
% \title{The \textsf{mathstyle} package}
% \date{\filedate\quad\fileversion}
% \author{Author: Morten H\o gholm\\ Inactively maintained by 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>
%    \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.
%    \begin{macrocode}
\everydisplay=\expandafter{\the\everydisplay\chardef\mathstyle\z@}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\mathstyle}
% A counter for the math style: 0--display, 1--text, 2--script, 3--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.
%    \begin{macrocode}
\chardef\mathstyle\@ne
%    \end{macrocode}
% \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.
%    \begin{macrocode}
\DeclareRobustCommand{\displaystyle}{%
  \@@displaystyle \chardef\mathstyle\z@}
\DeclareRobustCommand{\textstyle}{%
  \@@textstyle \chardef\mathstyle\@ne}
\DeclareRobustCommand{\scriptstyle}{%
  \@@scriptstyle \chardef\mathstyle\tw@}
\DeclareRobustCommand{\scriptscriptstyle}{%
  \@@scriptscriptstyle \chardef\mathstyle\thr@@}
%    \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.
%    \begin{macrocode}
\def\subsupstyle{%
  \ifnum\mathstyle<\tw@ \chardef\mathstyle\tw@
  \else \chardef\mathstyle\thr@@   
  \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}
% \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\mathstyle
    \expandafter\@firstoffour
  \or
    \expandafter\@secondoffour
  \or
    \expandafter\@thirdoffour
  \else
    \expandafter\@fourthoffour
  \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 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]{%
  {#1\fracstyle
    {\begingroup #5\endgroup
      \csname @@\ifx\maxdimen#4\maxdimen over\else above\fi
        \if @#2@\else withdelims\fi\endcsname #2#3#4\relax
     #6}%
  }%
}
%    \end{macrocode}
% \changes{v0.90}{2011/08/03}{\cs{fracstyle} must be called \emph{after}
%   changing to the required style}
% \end{macro}
%
%    \begin{macrocode}
\renewcommand{\frac}{\genfrac{}{}{}{}}
\providecommand{\dfrac}{}
\providecommand{\tfrac}{}
\renewcommand{\dfrac}{\genfrac\displaystyle{}{}{}}
\renewcommand{\tfrac}{\genfrac\textstyle{}{}{}}
\providecommand{\binom}{}
\providecommand{\tbinom}{}
\providecommand{\dbinom}{}
\renewcommand{\binom}{\genfrac{}(){0pt}}
\renewcommand{\dbinom}{\genfrac\displaystyle(){0pt}}
\renewcommand{\tbinom}{\genfrac\textstyle(){0pt}}
%    \end{macrocode}
% The \cs{fracstyle} command is a switch to go one level down but no
% further than three.
%    \begin{macrocode}
\def\fracstyle{\ifcase\mathstyle
    \chardef\mathstyle=\@ne
  \or 
    \chardef\mathstyle=\tw@
  \else 
    \chardef\mathstyle=\thr@@
  \fi
}
%    \end{macrocode}
% 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\mathstyle
    \@@displaystyle
  \or
    \@@textstyle
  \or
    \@@scriptstyle
  \or
    \@@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
