% LaTeX package sansmath: typeset sans-serif math using `text' fonts.
%
\ProvidesPackage{sansmath}[2007/02/28 \space ver 1.1  Donald Arseneau]
% 
% Donald Arseneau,  asnd@triumf.ca,  Vancouver, Canada.
%
% This package is released to the public domain by me, Donald Arseneau,
% the original author, who disclaims all rights.  I do not support it, 
% and request that it be adopted and supported by someone who knows 
% more about math font issues in LaTeX.
%
% The point of this package is to allow sans-serif math in the absence
% of proper sans math fonts.
%
% The author was persuaded to use the name `sansmath' in hopes of
% working with various text sans fonts (indicated by \sfdefault)
% but the only good results seen were with Computer Modern and cmss.
% (Helvetica with the `pslatex' package was very poor!)
%
% INSTRUCTIONS
% ~~~~~~~~~~~~
% Load this package by inserting `\usepackage{sansmath}' in the
% document preamble.  This will define a new math version `sans'
% and a command `\sansmath', much like `\boldmath'.  There is also
% an `\unsansmath' defined, but if math is to be sans-serif for
% a region of a document, one should declare `\sansmath' in a local
% group (perhaps with \begin{sansmath}...\end{sansmath}).  
% Within the scope of the \sansmath declaration, math characters
% will be taken from the text sans-serif family as much as possible.
% The actual sans fonts are OT1 encodings of those indicated by the 
% meaning of \sfdefault WHEN THE PACKAGE WAS LOADED -- not the 
% meaning at each math environment!
% 
% Since the (OT1) text fonts do not provide the lower-case greek 
% letters, there is a package option [eulergreek] to take the 
% lowercase greek from the Euler math fonts.
% Since some (many) sans fonts have no uppercase greek letters either
% (missing characters from the OT1 encoding), there is an option 
% [EULERGREEK] to take ALL greek letters from the euler fonts.
% One should also investigate using Euler for ALL MATH in the document,
% using package `euler' instead of this one!
%
% OT1 encoding is normally required to get the uppercase greek letters,
% but if you use the [EULERGREEK] option or don't use any uppercase greek
% letters, then you are welcome to define \sansmathencoding *before*
% loading this package.  There is also a package option [T1] to perform 
% that particuler definition.  Note the comment above about only cmms
% being good; even the T1-encoded ec fonts are poor substitutes.
% 
% This package achieves math-italic by reloading the slanted version
% of the text sans-serif font, and changing a fontdimen parameter 
% (spaceskip).  This causes the italic correction to be applied between
% letters (good) but does not break up the `fi' and `fl' ligatures (bad)
% (Why does a sans font have these ligatures anyway?)  At this point,
% nothing is done about this bug.

\providecommand\sansmathencoding{OT1}
\DeclareOption{T1}{
 \def\sansmathencoding{T1}
}

\let\EulGreek\relax
\let\EuUCGreek\relax
\let\NonEulGreek\relax
\DeclareOption{EULERGREEK}{
  \ExecuteOptions{eulergreek}
  \def\EuUCGreek{%
    \@euG\Gamma \@euG\Delta \@euG\Theta \@euG\Lambda \@euG\Xi \@euG\Pi
    \@euG\Sigma \@euG\Upsilon \@euG\Phi \@euG\Psi \@euG\Omega
}}
\DeclareOption{eulergreek}{
 \def\EulGreek{%
  \ifnum\alpha<\@eugreekOffset % avoid multi-mapping when repeated
    \EulGreekList
    \EuUCGreek
    \let\varrho\rho \let\varsigma\sigma % Euler's missing variants
  \fi
}
\def\EulGreekList{\@eug\alpha \@eug\beta \@eug\gamma \@eug\delta 
    \@eug\epsilon \@eug\zeta \@eug\eta \@eug\theta \@eug\iota \@eug\kappa 
    \@eug\lambda
    \@eug\mu \@eug\nu \@eug\xi \@eug\pi \@eug\rho \@eug\sigma \@eug\tau
    \@eug\upsilon \@eug\phi \@eug\chi \@eug\psi \@eug\omega
    \@eug\varepsilon \@eug\vartheta \@eug\varpi \@eug\varphi
}
\def\NonEulGreek{% Reverse previous mapping
  \ifnum\alpha>\@eugreekOffset % avoid multi-mapping when repeated
    \@eugreekOffset=-\@eugreekOffset \@euGREEKOffset=-\@euGREEKOffset
    \EulGreekList
    \EuUCGreek
    \@eugreekOffset=-\@eugreekOffset \@euGREEKOffset=-\@euGREEKOffset
  \fi
}}

\ProcessOptions

\DeclareMathAlphabet{\mathsfsl}{\sansmathencoding}{\sfdefault}{m}{sl}
\DeclareOldFontCommand{\sfsl}{\normalfont\sffamily\slshape}{\mathsfsl}
\DeclareMathAlphabet{\mathsfbf}{\sansmathencoding}{\sfdefault}{bx}{sl}
\DeclareOldFontCommand{\sfbf}{\normalfont\sffamily\bfseries}{\mathsfbf}

\DeclareMathVersion{sans}
\SetMathAlphabet{\mathrm} {sans}{\sansmathencoding}{\sfdefault}{m}{n}
\SetMathAlphabet{\mathsfsl}{sans}{\sansmathencoding}{\sfdefault}{m}{sl}
\SetSymbolFont {operators}{sans}{OT1}{\sfdefault}{m}{n}

% The cmm and cmsy are too light for cmss.  I tried {b} for the next
% two lines, but it did not work well (unbold +, bold greek, bad \neq)
\SetSymbolFont {letters}  {sans}{OML}{cmm} {m}{it}
\SetSymbolFont {symbols}  {sans}{OMS}{cmsy}{m}{n}

\SetMathAlphabet{\mathsfbf}{sans}{\sansmathencoding}{\sfdefault}{bx}{n}

\DeclareMathSymbol{0}{\mathord}{operators}{`0}
\DeclareMathSymbol{1}{\mathord}{operators}{`1}
\DeclareMathSymbol{2}{\mathord}{operators}{`2}
\DeclareMathSymbol{3}{\mathord}{operators}{`3}
\DeclareMathSymbol{4}{\mathord}{operators}{`4}
\DeclareMathSymbol{5}{\mathord}{operators}{`5}
\DeclareMathSymbol{6}{\mathord}{operators}{`6}
\DeclareMathSymbol{7}{\mathord}{operators}{`7}
\DeclareMathSymbol{8}{\mathord}{operators}{`8}
\DeclareMathSymbol{9}{\mathord}{operators}{`9}
\DeclareMathSymbol{!}{\mathclose}{operators}{"21}
\DeclareMathSymbol{,}{\mathpunct}{operators}{`\,}
\DeclareMathSymbol{.}{\mathord}{operators}{`\.}
\DeclareMathSymbol{:}{\mathrel}{operators}{`\:}
\DeclareMathSymbol{;}{\mathpunct}{operators}{`\;}
\DeclareMathSymbol{?}{\mathclose}{operators}{`\?}
%\DeclareMathSymbol{+}{\mathbin}{operators}{"2B}
%\DeclareMathSymbol{=}{\mathrel}{operators}{`\=}


\ifx\EulGreek\relax\else
 \DeclareSymbolFont{EulerGreek}{U}{eur}{m}{n}
 \SetSymbolFont{EulerGreek}{sans}{U}{eur}{m}{n}
 \DeclareSymbolFontAlphabet\matheug{EulerGreek}

 \newcount\@eugreekOffset
 \let\@tempa\relax
 \DeclareMathSymbol{\@tempa}{\mathord}{EulerGreek}{11}
 \@eugreekOffset\@tempa
 \advance\@eugreekOffset-\alpha

 \def\@eug#1{\@tempcnta#1\advance\@tempcnta\@eugreekOffset
    %\message{Change \string#1 from \number#1 to \number\@tempcnta}%
    \mathchardef#1\@tempcnta}

 \ifx\EuUCGreek\relax\else

  \newcount\@euGREEKOffset
  \let\@tempa\relax
  \DeclareMathSymbol{\@tempa}{\mathord}{EulerGreek}{1}
  \@euGREEKOffset\@tempa
  \advance\@euGREEKOffset-\Delta

  \def\@euG#1{\@tempcnta#1\advance\@tempcnta\@euGREEKOffset
     \mathchardef#1\@tempcnta}
 \fi
\fi

\def\sfMathSwitch{\sfsl
  \make@math@version@font \textfont \fam \tf@size
  \make@math@version@font \scriptfont \fam \sf@size
  \make@math@version@font \scriptscriptfont \fam \ssf@size
}

\def\make@math@version@font #1#2#3{%
  \begingroup
    \escapechar\m@ne
    \xdef\sansm@name{\expandafter\string\the#1#2+}%
  \endgroup
  \@ifundefined{\sansm@name}{%
      \@load@variant@font \sansm@name{#1#2}{#3\p@}}{}%
  #1#2\csname \sansm@name\endcsname
}

\begingroup
\catcode`\A=12 \catcode`\T=12
\lowercase{% necessary because \fontname\font produces "at" in catcode 12.
\endgroup
%
\def\@load@variant@font #1#2#3{\expandafter\@do@load@varfont
     \csname #1\expandafter\endcsname \fontname#2 AT #3 AT \relax }
%
\def\@do@load@varfont #1#2 AT #3 AT #4\relax{%
  \dimen@#3\advance\dimen@ 1sp
  \global\font #1=#2 at \dimen@
  \fontdimen\tw@#1=\z@}
}

\def\sansmath{%
  \ifx\mnorm@mathbf\mathbf\else \let\mnorm@mathbf\mathbf \fi
  \mathversion{sans}%
  \let\mathbf\mathsfbf
  \let\sansm@every@math\sfMathSwitch
  \EulGreek
}

\def\unsansmath{%
  \mathversion{normal}%
  \let\mathbf\mnorm@mathbf
  \let\sansm@every@math\relax
  \NonEulGreek
}

\expandafter\def\expandafter\check@mathfonts\expandafter{\check@mathfonts
   \sansm@every@math}

\let\sansm@every@math\relax
\let\mnorm@mathbf\mathbf

% Repair of a LaTeX bug that has not been fixed in 6 years.
% Latex bug: latex/3357

\def \@fontswitch #1#2{%
  \ifmmode
     \let \math@bgroup \relax
     \def \math@egroup ##1{%
         \let \math@bgroup \@@math@bgroup
         \let \math@egroup \@@math@egroup
         ##1}%
     #2\relax
  \else
     #1%
  \fi
}