Here's a LuaLaTeX-based solution to your challenge.
The Lua function replace_ddollar
doesn't actually redefine the $$
primitive. Instead, it scans the input text at a very early stage of processing (before TeX's "eyes" start doing anything) and replaces all instances of $$
with either \[
or \]
. The Lua code can handle code such as
$$a^2+b^2=c^2$$
i.e., matched pairs of $$
directives on a single line, as well as the usual entry format for equations, i.e.,
$$
E = mc^2
$$
Remark: Precisely because $$
is not being redefined in this approach, things can (and will) go wrong if the document contains instances of $$
that are not used to initiate or terminate display-math mode. Some examples of such instances:
- Unmatched instances of
$$
in a comment (including comment
-like environments)
- Instances of
$$
in a verbatim-like environment
- Instances of
$$
in a URL string encased in a \url{...}
directive
(I'm sure there are still more possibilities for things to go wrong.) Just in case you do have such instances in your document, the code below provides the macro \ReplaceDoubleDollarOff
, which turns off the operation of the Lua function replace_ddollar
. There's also a companion macro, called \ReplaceDoubleDollarOn
, that switches the Lua function back on.
The risk posed by instances of single instances of $$
on an input line that are not meant to initiate or terminate displaymath mode can be greatly reduced if it can be assumed that the only instances of $$
that are supposed to initiate or terminate displaymath mode occur at the very start of a line: If this assumption is valid, simply reduce the search string in the second string.gsub
function, "%$%$"
, with "^%$%$"
. The ^
character indicates that a match can occur only if $$
occurs at the very start of a line. [In case you're curious why the Lua code contains %$%$
rather than just $$
: In Lua, the $
character is "special" and has to be escaped (by prefixing a %
symbol) in order to denote an actual $
symbol.]

% !TEX TS-program = lualatex
\documentclass{article}
\usepackage{luacode} % for 'luacode' environment
\begin{luacode}
in_display_math = false
function replace_ddollar ( line )
line = string.gsub ( line , "%$%$(.-)%$%$" , "\\[ %1 \\]" )
line = string.gsub ( line , "%$%$" , function (x)
if not in_display_math then
in_display_math = true
return "\\["
else
in_display_math = false
return "\\]"
end
end )
return line
end
\end{luacode}
\newcommand\ReplaceDoubleDollarOn{%
\directlua{ luatexbase.add_to_callback(
"process_input_buffer", replace_ddollar, "replace_ddollar" )}}
\newcommand\ReplaceDoubleDollarOff{%
\directlua{ luatexbase.remove_from_callback(
"process_input_buffer", "replace_ddollar" )}}
\ReplaceDoubleDollarOn % Replacement function turned on
\usepackage{url} % just for this example
\begin{document}
$$
E = mc^2
$$
$$a^2+b^2=c^2$$ $$d^2+e^2=f^2$$ % Aside: I do not endorse this coding style!
$$
x = 3\alpha^2 + \beta = \int f\, d\mu.
$$
% Turn off replacement of double-dollars
\ReplaceDoubleDollarOff
\url{A_URL_string_with_a_$$_and_$$$$_and_another_$$}
% Turn replacement of double-dollars back on
\ReplaceDoubleDollarOn
$$ e^{i\pi}-1=0 $$
$$
1+1=2
$$
\end{document}
\eqalign
or\eqalignno
in plain TeX before using&
. The native$$...$$
doesn't support this. If you need to do\eqalign
at every display then you can set\everydisplay
. And my recommendation: forget LaTeX, use simply plain TeX.$$...$$
became\[...\]
,$...$
might one day become\(...\)
, and\bf, \tt
etc. were divided into math and text versions. So instead of a small, logical library of simple commands we got a gigantic, fragmented one. I like LaTeX and the ideas of it and would like to keep using it; I would just have liked if they modified the existing commands.