I was trying to find how to number the equations in LaTeX. In this Peter Grill suggested to use


However, I've almost written my entire document using


Is there any simple way that makes all or some equations numbered without changing all the $$...$$ to equation environment?

  • 4
    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, 2018 at 14:26
  • 4
    Do please read the posting Why is \[ … \] preferable to $$ … $$?
    – Mico
    Jun 16, 2018 at 14:42
  • @Sigur - See the answer I just posted. :-)
    – Mico
    Jun 16, 2018 at 16:12
  • 3
    It just take a few minutes to change $$ into alternatively \begin{equation} and \end{equation} with a good editor.
    – egreg
    Jun 16, 2018 at 17:46

2 Answers 2


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 \begin{equation} or \end{equation}, 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 $$.

enter image description here

% !TEX TS-program = lualatex
\usepackage{luacode}    % for 'luacode' environment
in_display_math = false -- initialize a Boolean variable
function replace_ddollar ( line )
   line = line:gsub ( "%$%$(.-)%$%$" , -- pairs of "$$" on 1 line
              "\\begin{equation} %1 \\end{equation}" )
   line = line:gsub ( "%$%$" , function (x) -- found single instance of "$$"
                    if not in_display_math then
                       in_display_math = true
                       return "\\begin{equation}" 
                       in_display_math = false
                       return "\\end{equation}"
                  end )
   return line
%% Set up two LaTeX utility macros:
    \directlua{ luatexbase.add_to_callback( 
    "process_input_buffer", replace_ddollar, "replace_ddollar" )}}
    \directlua{ luatexbase.remove_from_callback( 
    "process_input_buffer", "replace_ddollar" )}}
\ReplaceDoubleDollarsOn % Switch Lua function _on_ by default

\usepackage{url}  % just for this example

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.

% Switch the Lua function off


% Switch the Lua function back on

$$ e^{i\pi}-1=0 $$

  • 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, 2018 at 16:17
  • 1
    @Sigur - Absolutely. :-)
    – Mico
    Jun 16, 2018 at 16:19
  • 1
    This is promoting bad input.
    – egreg
    Jun 17, 2018 at 21:12
  • 2
    @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, 2018 at 21:16
  • 1
    @Mico Hi, and good work.
    – Sebastiano
    Jun 17, 2018 at 21:27

Or you use a simple perl script:

cat yourfile | perl -p0e 's/\$\$(.*?)\$\$/\\begin{equation}\1\\end{equation}/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.

  • 3
    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, 2018 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, 2018 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? :) Jun 18, 2018 at 15:16
  • 1
    @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, 2018 at 15:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.