# Substitute characters in text-mode with their math equivalent

I'm looking for a TeXway to substitute - (or other characters) while in text-mode with their math equivalent (i.e. $-$) in one environment/group. I'm looking for a 'switch' (\foo # and not \foo{#}).

The code below does essentially what I want, but only works in math-mode.

\def\foo{%
\bgroup\uccode\~\expandafter\string-%
\uppercase{\egroup\edef~{\noexpand\text{$-$\relax}}}%
\mathcode\expandafter\string-"8000 }
\def\textsymbols{\foo}


I've been unable to come up with a solution. The closest I've come is the following command that takes one argument (which I don't want) and it does not work with $-$ (but $+$ works).

\begingroup
\catcode-=\active\gdef\foo{%
\begingroup\catcode-=\active\gdef-{$+$}%
\fooaux
}\gdef\fooaux#1{\endgroup #1}
\endgroup


A usecase would be:

\begin{table}
\foo
- is now set in math.
\end{table}

• It appears that I am unable to spell equivalent. – Jörg Mar 28 '14 at 10:35

You should use a non active - in the replacement text:

\def\foo{\catcode\-=\active
\begingroup\lccode~=-
\lowercase{\endgroup\def~}{$-$}%
}

\begingroup\foo
This is a - sign and not a hyphen.
\endgroup

Now - is a hyphen.

\bye


In a LaTeX context you probably want to use \ensuremath{-} instead of $-$.