# Can I get $$…$$ to behave like $…$?

Is it possible to modify LaTeX so that good old $$...$$ behaves like $...$ (and by this I mean that it should be synonymous to it).

If yes: Dear LaTeX3 developer, please integrate this into LaTeX3! (Not that I really believe you will actually do this.)

MWE (even if I don't really see its use in this case):

\documentclass{article}

\begin{document}

I've forgotten we're not in the plain \TeX\ world anymore:
$$x = 3\alpha^2 + \beta = \int f\, d\mu.$$

\end{document}

• If you forget that you're not in plain TeX, you should correct yourself, not the system. No, this is effectively not possible. You could implement it, but it would be too fragile to be usable in general. As well, feature requests don't belong on this site :-) (that's to the "LaTeX3" part of your text); I'm also sure that I know at least two LaTeX3 developers who would never implement this :-) – yo' Apr 23 '15 at 10:23
• Sorry, this isn't plain TeX world. You must specify \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. – wipet Apr 23 '15 at 10:29
• @Gaussler I'm also certainly not trying making it work: I think it's completely useless and even unworthy a try. – egreg Apr 23 '15 at 10:50
• What I find even more strange is that we take Knuth's old TeX, then replace all of the brilliant and simple notations he created with new, much longer and more fragmented commands. $$...$$ 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. – Gaussler Apr 23 '15 at 12:35
• @Gaussler If you don't like the way LaTeX works don't use LaTeX (other people with the same view don't and are quite happy). You can always use plain plus a set of custom macros. (There are reasons for the differences between plain and LaTeX2e syntax.) – Joseph Wright Apr 23 '15 at 12:38

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{%
"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}

• That is probably an exercise I cannot solve, since I don't know Lua, let alone how to use it in LuaTeX. – Gaussler Apr 23 '15 at 15:17
• Does this deal correctly with non-display instances of $$, e.g. I have \$$33.20$? (I checked: it does not. It eats the rest of the paragraph, and is out-of-sync with display start/stops for the rest of the document.) – Kundor Apr 23 '15 at 15:20 • @Gaussler - I've updated the code so that it can handle pairs of $$ directives on one line of input. – Mico Apr 23 '15 at 15:33 • @Kundor - I honestly hadn't thought of cases such as \$$33.20$. (With the update to the code, cases such as $$\33.20$$ are handled correctly...) I suppose one could always write \${}$33.20, i.e., use an empty group -- or something functionally equivalent -- to break up the $$ pattern. – Mico Apr 23 '15 at 15:36 • If you know how to use Lua patterns (I do not), it is probably possible to only match instances of $$ not preceded by \, no? However, problems remain (verbatim environments; any unmatched $$ within a comment will throw off the rest of the document…) A robust solution along these lines would probably take excessive effort. – Kundor Apr 23 '15 at 15:46 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. • This seems more a comment than an answer.... – JPi Apr 24 '15 at 12:25 • @JPi I (and other users of the site) respectfully disagree. It answers the question asked (Can I get $$…$$ to behave like $…$?) by saying: probably, but it would be difficult (explaining why) and would potentially break lots of other stuff and would not provide much benefit. There are many cases on the site where the answer to a question is, "well you probably could, but it's not a great idea". – Paul Gessler Apr 24 '15 at 12:34 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}  • I like this way better than my answer! No need for annoying @gobbles and such! – Mark Apr 24 '15 at 16:22 • Nice work. Thank you very much; it also helps me. – Megadeth Jun 25 '15 at 5:12 This solution is not perfect: it does no error checking, and probably has other issues. It makes$ and active character, which breaks some packages that don't expect that. But if you put the following near the end of your preamble, it may mostly work. I tried it on two of my documents and it seemed ok.

\catcode\$\active \makeatletter \protected\def\ExpectBeginMath{\@ifnextchar${\let$\ExpectEndDisplayMath$\@gobble}{\let\ExpectEndInlineMath$$}} \def\ExpectEndDisplayMath{$\let\ExpectBeginMath\@gobble} \def\ExpectEndInlineMath{$$\let$\ExpectBeginMath}
\makeatother
\let\$\ExpectBeginMath


Or something like that.