My question was inspired by this question in which it is described how to map "#1" in maths mode to \text{#1}:
\begingroup\lccode`~=`"
\lowercase{\endgroup\def~}#1"{\text{#1}}
\mathcode`"="8000
As an exercise I decided to make a function DefineMathText such that \DefineMathText{\~} would turn all occurences of ~#1~ within maths mode into \text{#1}:
\documentclass{article}
\usepackage{amsmath}
\def\DefineMathText#1{
\begingroup
\lccode`~=`#1\lowercase{\endgroup%
% do \def~#1~{\text{#1}}
\def~##1~{\text{##1}}}
% make #1 active in maths mode
\mathcode`#1="8000
}
\begin{document}
\DefineMathText{\~}
This is a test: $a+b+c ~ test~$.
\end{document}
It works.
But, when I try to make the special character a double quote ", it fails:
\DefineMathText{"}
This is a test: $a+b+c " test"$.
% Runaway argument?
% test"$ \end {document}
% File ended while scanning use of ".
Attempt 2 suffers from the same error:
\DefineMathText{\"}
I think it may have something to do with " being matched to the right quote only. Yet if I do the definition directly (the first 3-liner) everything compiles and works.
Is there some way I can get \DefineMathText to work with a double quote " ?
Possibly related: I guess this is the same question as asking how to do \DefineShortVerb{"} in the fancyvrb package (say for some odd reason I wanted to map "xxxx" to \verb!xxxx!).
P.S. - this question is independent of how use(ful|less) the functionality is. \text{...} is not that hard to type and possibly clearer in the source.
\mathcode`#1=\string"8000is a little confusing. I had issues understanding it as well. See my comments to egreg's answer to the above linked question. – Martin Scharrer Apr 02 '12 at 08:11