Can I get $$...$$ to behave like \[...\]?
This will no doubt be a controversial answer, but here goes nothing!1
What you are asking is flawed in its premise: if you wanted to modify $$...$$
to behave like \[...\]
it is not LaTeX, but TeX, that you would have to modify. LaTeX does not change the behavior of $$
at all from the TeX definition.
All that \[...\]
is is a wrapper around $$...$$
to do some mode checks, provide some enhanced error reporting, and change some spacing if the display occurs at the beginning of a paragraph. You can see the complete definition in the sources and/or at the accepted answer to What are the differences between $$, \[, align, equation and displaymath?.
So LaTeX took the Plain TeX basics and extended it to cover the use cases of a presumed majority of users. It's no different than any other macro provided by LaTeX: it's an augmentation of the building blocks provided by TeX.
In my opinion, if you want the behavior of \[...\]
, simply use \[...\]
. Redefining $$
, while probably technically possible, is nontrivial because the very command defining the desired behavior uses it internally. Additionally, there's no telling how many (sometimes badly-designed, but nonetheless) documents rely on the old functionality.
1 Hold my beer!
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 (includingcomment
-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}
This is perhaps the easiest implementation. I still wish to stress not to use code like this. It either goes wrong from the get go, or worse: at one point you wish to change something in the document, you get an error which tells you you're missing an \item
(or something else unrelated to anything math mode), and you have to recode your entire document because everything breaks down.
\documentclass{article}
%\usepackage[fleqn]{amsmath} % also works for instance
\let\dollar=$ \catcode`$=\active
\makeatletter
\protected\def${\@ifnextchar${\@doubledollareqn}{\@singledollareqn}}
\def\@singledollareqn#1${\(#1\)}
\def\@doubledollareqn$#1$${\[#1\]}
\makeatother
\begin{document}
Inline math $x^2+y^2=z^2$.
$$
f(v)=4\pi\left(\frac m{2\pi kT}\right)^{\frac32}v^2e^{-\frac{mv^2}{2kT}}.
$$
\end{document}