# formatting equations - numbered equations using $$I was trying to find how to number the equations in LaTeX. In this Peter Grill suggested to use $$%equation...$$  However, I've almost written my entire document using $$
%equation...
$$ Is there any simple way that makes all or some equations numbered without changing all the $$...$$ to equation environment? • I'm afraid that not. This is one reason why $$ is not good. You don't know if it is opening or closing. Consider use  instead. – Sigur Jun 16 '18 at 14:26
• Do please read the posting Why is $…$ preferable to $$…$$? – Mico Jun 16 '18 at 14:42
• @Sigur - See the answer I just posted. :-) – Mico Jun 16 '18 at 16:12
• It just take a few minutes to change $$ into alternatively and with a good editor. – egreg Jun 16 '18 at 17:46 ## 2 Answers Is there any simple way that makes all ... equations numbered without changing all the $$...$$? In case you're free to use LuaLaTeX to compile your document, the following solution -- which is a variation on this answer -- should be of interest to you. It sets up a Lua function that acts as pre-processor, automatically replacing all instances of $$ with either or , as appropriate, before LaTeX starts its usual processing. The Lua function also knows what to do if it comes across pairs of $$ symbols on a single line, e.g., $$a^2+b^2=c^2$$ or $$ e^{i\pi}-1=0 $$. The answer shown below also sets up two utility macros: \ReplaceDoubleDollarsOn and \ReplaceDoubleDollarsOff. The former switches the Lua function on, and the latter switches it back off. Having the ability to switch off the Lua function should be useful, e.g., if your document contains URL strings that feature instances of $$, or if it contains verbatim material that contains instances of $$. % !TEX TS-program = lualatex \documentclass{article} \usepackage{luacode} % for 'luacode' environment \begin{luacode}  in_display_math = false -- initialize a Boolean variable function replace_ddollar ( line ) line = line:gsub ( "%%(.-)%%" , -- pairs of "$$" on 1 line
"\$$%1 \$$" )
E = mc^2
a^2+b^2=c^2d^2+e^2=f^2$$% Aside: I do not endorse this coding style!$$
x = 3\alpha^2 + \beta = \int f\, d\mu.
$$% Switch the Lua function off \ReplaceDoubleDollarsOff \url{A_URL_string_with_a_$$_and__and_another_$$} % Switch the Lua function back on \ReplaceDoubleDollarsOn$$ e^{i\pi}-1=0 
1+1=2
$$\end{document}  • Very nice. So, even if the opening $$ is in line n and the rest of contents, including the closing $$ in another line m, it works?! – Sigur Jun 16 '18 at 16:17 • @Sigur - Absolutely. :-) – Mico Jun 16 '18 at 16:19 • This is promoting bad input. – egreg Jun 17 '18 at 21:12 • @egreg - I did write, "I do not endorse this coding style!" inside the code. But I'll admit that this warning wasn't sufficiently prominent. About this answer actually promoting bad input -- I would hope that, at least indirectly, this answer actually might prompt at least some would-be adopters of the proposed solution to take a step back and think, "hmm, maybe this involves way too much overhead -- maybe I shouldn't be using $$ in a LaTeX document to begin with?". If that's the outcome, I would rejoice heartily. – Mico Jun 17 '18 at 21:16
• @Mico Hi, and good work. – Sebastiano Jun 17 '18 at 21:27

Or you use a simple perl script:

cat yourfile | perl -p0e 's/\$\$(.*?)\$\$/\$$\1\$$/gs'


This will change all your $$ "environments" to equations. You could use the additional arguments -i and yourfile (as last one) for perl to replace a file in place instead. g makes this substitution global, s makes the . match also new lines. • For the sake of completeness, you may want to mention explicitly that this perl script operates on the entire contents of the file, including any instances of $$ that may occur in comments, in verbatim-like settings, and in URL strings. – Mico Jun 16 '18 at 17:07
• @Mico Of course you are right, I think usually this should be the desired behaviour. Except for verbatims maybe. – nox Jun 17 '18 at 19:23
• Hey, I don't know how to use Perl. Can you please refer me to some source where I can know how to use this code? :) – Harshvardhan Jun 18 '18 at 15:16
• @Harshvardhan If you are using Linux, just type it into a terminal. In the (rare) case perl is not installed on your system, search for it in your package manager. If you are running Windows, you highly likely have to install perl first. Have a look at strawberryperl.com. Since I do not use Windows I am not sure about how to continue from there. Either you will find a terminal app that allows you to use this code or you can just use the Windows shell. Probably use just perl -p0e '...' yourfile. If the output is what you want, you can copy/paste it or run perl -ip0e ... – nox Jun 18 '18 at 15:46