TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

My question was inspired by this question in which it is described how to map "#1" in maths mode to \text{#1}:


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}:

      % do \def~#1~{\text{#1}}
    % make #1 active in maths mode
This is a test: $a+b+c ~ test~$.

It works.

But, when I try to make the special character a double quote ", it fails:

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:


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.

share|improve this question
up vote 12 down vote accepted

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


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:

share|improve this answer
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

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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