TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

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

I want to capture the math mode through a redefinition of the $. I thought about something like

\def$#1${here was a formula}
% to test

I would expect "here was a formula" as output. Is it possible ?

EDIT : egreg's solution works for me :


voila le calcul $3+4=5$


But i would like also to redefine the double dollar $$ ...$$. If i define additionally :


then the following works :

voila un calcul centré $$x+y=z$$

but I can't use the single dollar anymore: $3+4=5$ fails now to compile.

Question : how can I have both $...$ and $$...$$ available ?

share|improve this question
Possible, but slightly dangerous: \catcode`$=\active and then \protected\def$#1${foo} – egreg Jun 15 '12 at 12:45
Why do you want this? There may be an easier way to achieve what you want. – Seamus Jun 15 '12 at 13:01
@Seamus: I actually did it in this answer (because I prefer $...$ to \(...\)). – Hendrik Vogt Jun 15 '12 at 13:23
@egreg. I know, I can put the \catcode in a group, and this will have no effect outside. But if i don't, how can I cancel the effect of '\catcode$=\active' later on in the document ? And for which reason did you put a \protected` ? – nicolas roy Jun 15 '12 at 19:19
Well, my question about how to cancel the effect, was just a subsidiary question, because to frightened me with "dangerous". But your answer is perfect for me. What i want to do : i want to export a tex document into some xml file. For this, i need to capture the maths formulas, a make \write of the \detokenize of the content of the math environment. With your catcode trick, it works. – nicolas roy Jun 15 '12 at 19:41
up vote 12 down vote accepted

You can, but there's no guarantee that it will work in all situations:


For $$...$$, with even less guarantees:




$$a-b\ne b-a$$

share|improve this answer

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.