2

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
3

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

enter image description here

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.