# How can I make every occurrence of + and - be replaced by a macro, but only in math mode?

I decided to hack together an answer by using an existing answer: https://tex.stackexchange.com/a/127507/49339

So, my MnotWE looks like this, after much simplification:

\documentclass[12pt,letterpaper]{article}

\usepackage{lmodern}
\usepackage[T1]{fontenc}
\usepackage{amsmath}

\makeatletter
% LaTeX's \@ifnextchar gobbles spaces, therefore
% \msh@ifnextchar is defined that keeps spaces
\newcommand*{\msh@ifnextchar}[3]{%
\def\msh@temp{\msh@@ifnextchar{#1}{#2}{#3}}%
\futurelet\msh@token\msh@temp
}
\newcommand*{\msh@@ifnextchar}[1]{%
\ifx\msh@token#1%
\expandafter\@firstoftwo
\else
\expandafter\@secondoftwo
\fi
}

% Commands that take the original meanings of the special characters
\mathchardef\msh@code@minus=\mathcode\-\relax
\mathchardef\msh@code@plus=\mathcode\+\relax

% Macro \setmathshorthands activates and defines the special
% characters
\begingroup
\@ifdefinable{\setmathshorthands}{%
\xdef\setmathshorthands{%
\let\noexpand-\noexpand\msh@minus
\let\noexpand+\noexpand\msh@plus
}%
}%
\endgroup

\newcommand*{\msh@minus}{\&}


\makeatother

% Activate math shorthands in the math modes
\everymath{\setmathshorthands}
\everydisplay{\setmathshorthands}

\begin{document}
A-b--c---d dddddddddddd dddddddddddd ddddddddddddd dddddddd fffff\-ggg
\begin{equation*}
a + b - c = 0 \quad \text{ravings of a mad-man}
\end{equation*}
\end{document}


• Thank you for this. Would you mind explaining what your additions do? – user89 Sep 19 '17 at 2:11
• Check out the answer to this question: tex.stackexchange.com/a/109440/49339 -- it says that: > \mathcode"8000 is a special code that is not looked up in the usual way. If a character has that mathcode, the definition of the active (catcode 13) token is used instead, even though the character itself is not active. this is used in plain and LaTeX to allow ' to work as a normal non-active apostrophe in text but in math it has catcode hex 8000 so the active definition is used, which expands to ^{\prime} – user89 Sep 19 '17 at 2:16
• So maybe the  \catcode\<+, ->=\active is not needed because we are doing \mathcode"8000? edit: testing shows that it is needed, not sure I understand why – user89 Sep 19 '17 at 2:18
• @user89 That would be correct. The \catcodes are made active inside a group in the preamble, meaning they are no longer active once the group is closed. But in that group, their "math-active" definitions are set, pointing to \msh@minus and \msh@plus. – Steven B. Segletes Sep 19 '17 at 2:29
• @user89 -- define a word with marked hyphenation points in the \hyphenation{...} and then put that word in the middle of text, unhyphenated. keep adding words (or spacers of some kind, like "xxxx" or even \hspace`s) before it until the test word is forced to the right-hand edge of the line where it can't avoid being hyphenated. sorry i can't think offhand of a good word to test; my laptop does not have a usable tex system installed. – barbara beeton Sep 19 '17 at 2:46