I am trying to set things up so that occurrences of $$ are replaced by \] if in math mode, \[ otherwise. My strategy is to make $ active and have it behave differently depending on whether the following token is also a $. However, I cannot seem to make this work. Here is my attempt:
\documentclass{article}
\usepackage{lipsum}
\def\unactiveDollar{$}
\catcode`\$=\active
\def\activeDollar{$}
\def\executedollar{%
\if\isDollar\activeDollar%
\let\next=\executeDDollar%
\else%
\let\next=\unactiveDollar%
\fi%
\next%
}
\def\executeDDollar{%
\ifmmode%
\let\next=\]%
\else%
\let\next=\[%
\fi%
\next%
\let\absorbnext=
}
\def${\futurelet\isDollar\executedollar}
\begin{document}
\lipsum*[7]
$$\int x^2 \; dx = \frac{1}{3}x^3 + C$$
\lipsum[9]
%Make sure $ $ is not mistaken for $$:
It follows that $E = m c^2$ $\forall x \in X$.
\end{document}
The resulting error is
./doubledollar.tex:29: TeX capacity exceeded, sorry [input stack size=5000].
\isDollar ->\if \isDollar
\activeDollar \let \next =\executeDDollar \else \l...
l.29 $$
\int x^2 \; dx = \frac{1}{3}x^3 + C$$
./doubledollar.tex:29: ==> Fatal error occurred, no output PDF file produced!
Other variations (e.g., substituting \ifx for \if) give different errors or simply incorrect output, but nothing gets it right. And as far as I can tell, the key problem is that my method for determining whether the following token is a $ (and absorbing it if so) simply does not work.
What am I doing wrong, and how can I fix it?
Additional note: An answer that throws an error whenever $$ is used instead of other macros/environments (which must be able to use it internally) would also be acceptable, since it answers the titular question.
$$changes the interline spacing of all subsequent text. So it's a much worse practice than usual. – Charles Staats Jul 30 '14 at 18:21$$. That's how. – egreg Jul 30 '14 at 18:21$$or you'll be absorbed”. – egreg Jul 30 '14 at 19:28$$:-) But if$$affects spacing opf following text do you redefine equation and eqnarray etc which just expand to$$? – David Carlisle Jul 30 '14 at 20:18\equation, etc. works (as much as it ever does), as does{$$ ... $$}. (There's an\aftergroupinside an\everydisplay.) – Charles Staats Jul 30 '14 at 21:43\usepackage[all]{onlyamsmath}in my templates. Then$$andeqnarraycause errors. Hard, but effective. – daleif Jul 31 '14 at 11:06\equationwhen I meanteqnarray; in particular, I did not mean to imply that theequationenvironment is somehow flawed. – Charles Staats Jul 31 '14 at 17:48