\documentclass[12pt]{article}
%\usepackage{setspace}
%\usepackage{booktabs}
\usepackage{maybemath, xspace}
\newcommand{\maybemath}{\texttt{maybemath}\xspace}

\author{Andy Buckley, \texttt{andy@insectnation.org}}
\title{The \maybemath package for \LaTeX}

\newcommand{\manifestsAs}{\ensuremath{\Rightarrow\quad}\xspace}
\newcommand{\texcommand}[1]{\texttt{\ensuremath{\backslash{}}{#1}}\xspace}
\newcommand{\texarg}[1]{\ensuremath{\!}\texttt{\{}\textit{#1}\texttt{\}}\xspace}
\newcommand{\maybebmName}{\texcommand{maybebm}}
\newcommand{\maybermName}{\texcommand{mayberm}}
\newcommand{\maybeitName}{\texcommand{maybeit}}
\newcommand{\maybeitrmName}{\texcommand{maybeitrm}}
\newcommand{\maybeitsubscriptName}{\texcommand{maybeitsubscript}}
\newcommand{\maybesfName}{\texcommand{maybesf}}
\newcommand{\maybebmsfName}{\texcommand{maybebmsf}}
\newcommand{\maybemathName}{\texcommand{maybemath}}

\begin{document}
\maketitle

The \maybemath package provides a set of commands for adjusting math-mode
typesetting to match the context of the surrounding paragraph. This includes
making the math-mode text bold, upright, forced italic or sans-serif according
to context, or any combination of these. It can be particularly useful when math
terms must appear in section headings, as this implies the same expression
appearing in several boldness contexts: the heading itself, the table of
contents and perhaps a page header or footer. Typically this has been resolved
manually by use of the optional second argument of the sectioning commands:
\maybemath provides a more seamless solution.

This package was developed in order to implement the \texttt{hepparticles}
package, used for typesetting high-energy physics particle names. 
% The commands are intended more for use in flexible macro definitions than 
% for direct use in documents, hence the command names are quite verbose.

The commands available are defined below. In general they may be nested and can
take arbitrarily large arguments within a single math expression. The term to
watch in the expressions below is the $x^3$:

\section{Bold contexts}
\begin{itemize}
\item For context-sensitive boldness use \maybebmName\texarg{expr}:\\
  {\verb|\dots foo bar, $x^2 + \maybebm{x^3} + \dotsb$|}\\ \manifestsAs {\dots foo bar, $x^2 + \maybebm{x^3} + \dotsb$}\\
  {\verb|\textbf{\dots foo bar, $x^2 + \maybebm{x^3} + \dotsb$}|}\\ \manifestsAs \textbf{\dots foo bar, $x^2 + \maybebm{x^3} + \dotsb$}
\end{itemize}


\section{Upright and italic contexts}
\begin{itemize}
\item For context-sensitive upright math typesetting use \maybermName\texarg{expr}:\\
  {\verb|\dots foo bar, $x^2 + \mayberm{x^3} + \dotsb$|}\\ \manifestsAs {\dots foo bar, $x^2 + \mayberm{x^3} + \dotsb$}\\
  {\verb|\textit{\dots foo bar, $x^2 + \mayberm{x^3} + \dotsb$}|}\\ \manifestsAs \textit{\dots foo bar, $x^2 + \mayberm{x^3} + \dotsb$}

\item Alternatively, to force \texcommand{mathit} in italic contexts use \maybeitName\texarg{expr}:\\
  {\verb|\dots foo bar, $x^2 + \maybeit{x^3} + \dotsb$|}\\ \manifestsAs {\dots foo bar, $x^2 + \maybeit{x^3} + \dotsb$}\\
  {\verb|\textit{\dots foo bar, $x^2 + \maybeit{x^3} + \dotsb$}|}\\ \manifestsAs \textit{\dots foo bar, $x^2 + \maybeit{x^3} + \dotsb$}

\item The functionality of both \maybermName and \maybeitName is combined for convenience in the command \maybeitrmName\texarg{expr}:\\
  {\verb|\dots foo bar, $x^2 + \maybeitrm{x^3} + \dotsb$|}\\ \manifestsAs {\dots foo bar, $x^2 + \maybeitrm{x^3} + \dotsb$}\\
  {\verb|\textit{\dots foo bar, $x^2 + \maybeitrm{x^3} + \dotsb$}|}\\ \manifestsAs \textit{\dots foo bar, $x^2 + \maybeitrm{x^3} + \dotsb$}

\item An extra command is available for adjusting the spacing of subscripts in italic contexts: they are shifted
      to the left if wrapped with the command \maybeitsubscriptName\texarg{expr}:\\
  {\verb|\dots foo bar, $\mayberm{B_d^0} \to \|\\\verb|\mayberm{B_\maybeitsubscript{d}^0}$|}\\ \manifestsAs {\dots foo bar, $\mayberm{B_d^0} \to \mayberm{B_\maybeitsubscript{d}^0}$}\\
  {\verb|\textit{\dots foo bar, $\mayberm{B_d^0} \to \|\\\verb|\mayberm{B_\maybeitsubscript{d}^0}$}|}\\ \manifestsAs \textit{\dots foo bar, $\mayberm{B_d^0} \to \mayberm{B_\maybeitsubscript{d}^0}$}\\
  I'm not aware if there are particular uses for this outside the pickinesses of particle-name typesetting but it \emph{may} be useful!
\end{itemize}


\section{Sans-serif contexts}
\begin{itemize}
\item For context-sensitive sans-serif math typesetting use \maybesfName\texarg{expr}:\\
  {\verb|\dots foo bar, $x^2 + \maybesf{x^3} + \dotsb$|}\\ \manifestsAs {\dots foo bar, $x^2 + \maybesf{x^3} + \dotsb$}\\
  {\verb|\textsf{\dots foo bar, $x^2 + \maybesf{x^3} + \dotsb$}|}\\ \manifestsAs \textsf{\dots foo bar, $x^2 + \maybesf{x^3} + \dotsb$}\\
  Note that there is no italic version of the sans-serif math font, so use of \maybesfName eliminates italic context handling.
\end{itemize}


\section{Combined contexts}
\begin{itemize}
\item For combined bold-and-sans-serif context handling, a \maybebmsfName\texarg{expr} command is provided:\\
  {\verb|\dots foo bar, $x^2 + \maybebmsf{x^3} + \dotsb$|}\\ \manifestsAs {\dots foo bar, $x^2 + \maybebmsf{x^3} + \dotsb$}\\
  {\verb|\textbf{\dots foo bar, $x^2 + \maybebmsf{x^3} + \dotsb$}|}\\ \manifestsAs \textbf{\dots foo bar, $x^2 + \maybebmsf{x^3} + \dotsb$}\\
  {\verb|\textsf{\dots foo bar, $x^2 + \maybebmsf{x^3} + \dotsb$}|}\\ \manifestsAs \textsf{\dots foo bar, $x^2 + \maybebmsf{x^3} + \dotsb$}\\
  {\verb|\textbf{\textsf{\dots foo bar, $x^2 + \maybebmsf{x^3} + \dotsb$}}|}\\ \manifestsAs \textbf{\textsf{\dots foo bar, $x^2 + \maybebmsf{x^3} + \dotsb$}}\\
  This is assumed to be the most commonly required combination of context-sensitive math typesetting: most other combinations can be achieved by nesting the primitive \maybemath commands.
\end{itemize}

%Finally, a \maybemathName command is provided for typesetting the name of the package:
%{\verb|\maybemath|} \manifestsAs \maybemath.\\

\noindent Any feedback is appreciated! Email to \texttt{andy@insectnation.org}, please.

\end{document}