I used Werner's comment for the $...$
form, but added forms that work for equation
and align
.
EDITED to handle the OP's requirements:
1) arguments to \mathmacro
;
2) multiple calls to \mathmacro
in a single environment;
3) works when embedded within \ensuremath
argument.
REVISED SOLUTION (can handle multiple \mathmacro
calls)
In this approach, to allow for multiple calls, I use \mathmacro
to build a semicolon separated list (disallows the ;
therefore in your \marginpar
s, but you can manually change the separator) of the \mathmacro
arguments.
Then, upon completion of the environment, I call on a routine to parse that list, and issue a \textmacro
call to \marginpar
for each argument in the list. Along the way, I had to handle a quirk of align
which makes a double pass, originally resulting in doubled calls to \mathmacro
.
\documentclass[11pt]{article}
\usepackage{amsmath}
\newcommand\textmacro[1]{\marginpar{\textbf{\sffamily #1}}}
% FIX FOR $...$ PER WERNER'S USAGE
\def\mathmacro#1#2${#2$\textmacro{#1}}
\makeatletter
% FIX FOR EQUATION
\let\svequation\equation
\let\svendequation\endequation
\def\eqfinishmacro{\expandafter\eqfinishhelpA\eqfinishdata\relax;\relax}
\def\eqfinishhelpA#1;#2\relax{%
\ifx\relax#1\else\textmacro{#1}\if\relax#2\relax\else\eqfinishhelpA#2\relax\fi\fi%
\@gobble}
\def\eqfinishdata{}
\newcommand\equationmacro[1]{%
\xdef\eqfinishdata{\eqfinishdata#1;}}
\def\equation{\let\mathmacro\equationmacro\svequation}
\def\endequation{\svendequation\eqfinishmacro\gdef\eqfinishdata{}}
% FIX FOR ALIGN
\let\svalign\align
\let\svendalign\endalign
\def\alfinishmacro{\expandafter\alfinishhelpA\alfinishdata\relax;\relax}
\def\alfinishhelpA#1;#2\relax{%
\ifx\relax#1\else\textmacro{#1}\if\relax#2\relax\else\alfinishhelpA#2\relax\fi\fi%
\@gobble}
\def\alfinishdata{}
\newcommand\alignmacro[1]{%
\xdef\alfinishdata{\alfinishdata#1;}}
\def\align{\let\mathmacro\alignmacro\svalign\def\alfinishdata{}}
\def\endalign{\svendalign\alfinishmacro\gdef\alfinishdata{}}
\makeatother
\begin{document}
Some text $a^b\mathmacro{MP} c^d$ more text\par
Some more text without a macro $a^b c^d$ more text\par
An equation using mathmacro
\begin{equation}
a^b\mathmacro{EQ XYZ} c^d
\end{equation}%
to see if it works.\par
An equation using mathmacro
\begin{equation}
a^b\mathmacro{EQ ARG.} c^d\mathmacro{2nd eq test}
\end{equation}%
to see if it works.\par
Here we have an equation without the mathmacro
\begin{equation}
a^b =c^d
\end{equation}
but some following text.\par
Align with the mathmacro
\begin{align}
a^b \mathmacro{AL ARG.} &= c^d &x &= y\mathmacro{2nd test}\\
A^B \mathmacro{3rd test} &= C^D & X &= y
\end{align}%
to also see if it works.\par
Here we us align without the mathmacro
\begin{align}
a^b &= c^d
\end{align}
but some following text.\par
Some text $a^b\mathmacro{ABC} c^d$ more text to see if original definition active.
\end{document}
ORIGINAL SOLUTION (allows only one \mathmacro
call per environment)
\documentclass[11pt]{article}
\usepackage{amsmath}
\newcommand{\textmacro}[1][ABC]{\marginpar{\textbf{\sffamily #1}}}
% FIX FOR $...$ PER WERNER'S USAGE
\def\mathmacro#1#2${#2$\textmacro[#1]}
% FIX FOR EQUATION
\let\svequation\equation
\let\svendequation\endequation
\def\eqfinishmacro{}
\newcommand\equationmacro[1]{\gdef\eqfinishmacro{\textmacro[#1]}}
\def\equation{\let\mathmacro\equationmacro\svequation}
\def\endequation{\svendequation\eqfinishmacro\gdef\eqfinishmacro{}}
% FIX FOR ALIGN
\let\svalign\align
\let\svendalign\endalign
\def\alfinishmacro{}
\newcommand\alignmacro[1]{\gdef\alfinishmacro{\textmacro[#1]}}
\def\align{\let\mathmacro\alignmacro\svalign}
\def\endalign{\svendalign\alfinishmacro\gdef\alfinishmacro{}}
\begin{document}
Some text $a^b\mathmacro{MP} c^d$ more text\par
Some more text without a macro $a^b c^d$ more text\par
An equation using mathmacro
\begin{equation}
a^b\mathmacro{EQ ARG.} c^d%\mathmacro
\end{equation}
to see if it works.\par
Here we have an equation without the mathmacro
\begin{equation}
a^b =c^d
\end{equation}
Align with the mathmacro
\begin{align}
a^b \mathmacro{AL ARG.} &= c^d
\end{align}
to also see if it works.\par
Here we us align without the mathmacro.
\begin{align}
a^b &= c^d
\end{align}
Some text $a^b\mathmacro{ABC} c^d$ more text to see if original definition active.
\end{document}
$..$
) is easy:\newcommand{\textmacro}{abc} \def\mathmacro#1${#1$\textmacro}
The others aren't. What is this "important macro" that can only be invoked in text mode? Using\mbox
initiates text mode. Shouldn't that be sufficient?\textmacro
command puts a note in the margin using\marginpar
. For more context see tex.stackexchange.com/questions/215322/….