\ProvidesPackage{maybemath}[2005/2/22]
\RequirePackage{amsmath}
\RequirePackage{bm}
%\RequirePackage{xspace}

%% A few commands for making math fonts bold, upright, italic
%% or sans-serif according to the context of surrounding text.
%% It is particularly useful for avoiding problems with math
%% boldness in section titles causing unwanted boldness
%% in e.g. the table of contents.
%%
%% Thanks to Viet-Trung Luu on comp.text.tex for providing 
%% the first steps to solving this problem.
%%
%% This material is subject to the LaTeX Project Public License. 
%% See http://www.ctan.org/tex-archive/help/Catalogue/licenses.lppl.html
%% for the details of that license.
%%
%% Author: Andy Buckley <andy@insectnation.org>


%% Mode names for bold, ext-bold, italic and sans-serif contexts
\def\@boldname{b}%
\def\@boldexname{bx}%
\def\@italname{it}%
\def\@sfname{cmss}%


%% Use the bold symbol if reqd for math fonts
\DeclareRobustCommand{\maybebm}[1]{\ensuremath{%
    \ifx\f@series\@boldname%
        {{\bm{#1}}}%
    \else\ifx\f@series\@boldexname%
        {{\bm{#1}}}%
    \else%
        {{#1}}%
    \fi\fi%
}}

%% Use \mathrm if not in italic context
\DeclareRobustCommand{\mayberm}[1]{\ensuremath{%
  \ifx\f@shape\@italname%
    {#1}%
  \else%
    {\mathrm{#1}}%
  \fi%
}}

%% Use \mathit if in italic context
\DeclareRobustCommand{\maybeit}[1]{\ensuremath{%
  \ifx\f@shape\@italname%
    {\mathit{#1}}%
  \else%
    {#1}%
  \fi%
}}

%% Use \mathrm if not in italic context
%% and \mathit if in italic context
\DeclareRobustCommand{\maybeitrm}[1]{\ensuremath{%
  \ifx\f@shape\@italname%
    {\mathit{#1}}%
  \else%
    {\mathrm{#1}}%
  \fi%
}}

%% Maybe-italic subscript shifting
\DeclareRobustCommand{\maybeitsubscript}[1]{\ensuremath{%
  \ifx\f@shape\@italname%
    {\!\mspace{1mu}#1}%
  \else%
    {#1}%
  \fi%
}}

%% Maybe-sans font change
\DeclareRobustCommand{\maybesf}[1]{\ensuremath{%
  \ifx\f@family\@sfname%
    {\mathsf{#1}}%
  \else%
    {#1}%
  \fi%
}}

%% Convenience command for following bold and sans-serif contexts
\DeclareRobustCommand{\maybebmsf}[1]{%
  {\maybebm{\maybesf{#1}}}%
}

%% For reference purposes :-)
%\DeclareRobustCommand{\maybemath}{\texttt{maybemath}\xspace}
