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.$$
. That's how.$$
:-) But if$$
affects spacing opf following text do you redefine equation and eqnarray etc which just expand to$$
?\usepackage[all]{onlyamsmath}
in my templates. Then$$
andeqnarray
cause errors. Hard, but effective.