# Using \lccode / \lowercase{…} trick with double quote mark

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. - ## 1 Answer You're defining the active " with a delimited argument; the delimiter you're asking for is an active ", which TeX will never find, because the " still has category code 12 when it's scanned in $a+b+c " test"\$.

A correct definition would be

\def\DefineMathText#1{%
\begingroup
\lccode~=#1\lowercase{\endgroup
\def~##1}#1{\text{##1}}%
\mathcode#1=\string"8000
}


and with this your second example will work. The first won't, however, because ~ is active. You can't do nothing about this, other than defining ~ to behave differently in text mode than in math mode. Note that the \mathcode<char>="8000 works only when <char> has category code 11 or 12. The fact that your first example worked with ~, is just because you redefined the active ~, not because of the special math code.

Notice also that this trick won't work when some babel language module that activates " is loaded (for example german, ngerman or italian), unless some precautions are taken for reverting " to category code 12 in math mode.

The problem of mapping "xxx" to \verb!xxx! is different: here the delimiter must be active (and the same caution related to babel applies). But it's easier:

\catcode"=\active
\def"{\verb"}

-
Oh, I get it. The rabbit hole is deeper than I thought... thanks for that. – mathematical.coffee Apr 2 '12 at 5:47
@mathematical.coffee There's always some Cheshire cat that can help you find the way. – egreg Apr 2 '12 at 5:51
@mathematical.coffee: The handling of characters in math-mode using \mathcode#1=\string"8000 is 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 2 '12 at 8:11