I usually use \[ ... \] to typeset equations in latex and sometimes I come back and realize that I want to have an equation number and label. I have been coming back and editing it to \begin{equation} ... \end{equation} but this is annoying and time consuming. Is there a command to do something like \[ \addeqnumber{} \] which will work the same way as the equation environment? I am aware this probably isn't the best practice, but it would save me some annoyance.
- 262,582
- 628
- 4
- 9
- 17
5 Answers
\documentclass{article}
\usepackage{amsmath}
\newcommand\addtag{\refstepcounter{equation}\tag{\theequation}}
\begin{document}
\[
1+1=2 \addtag
\]
\end{document}
See also: How can I add numbers to specific equations in an eqnarray*?
If you find yourself doing this for all equations I would use search & replace but if you’re reluctant you can do the following:
\let\[\equation
\let\]\endequation
After that, \[ … \] will behave identical to \begin{equation} … \end{equation}.
You can also wrap this into a macro, and create another one to switch back so that you can switch between the two styles via two macros.
- 39,394
- 22
- 107
- 160
Not answering your question but maybe solving your problem: mathtools has a mechanism that suppresses equation numbers for unreferenced equations. (So you'd always use equation and friends and only get numbers if you ref the equation.)
- 9,830
As you're aware that this isn't best practice, I don't mind answering! The following seems to work:
\newcommand{\addlabel}[1]{%
\refstepcounter{equation}%
\label{#1}%
\let\]\endequation}
Use it as:
\[
\addlabel{pythagoras}
x^2 + y^2 = z^2
\]
The start of \equation simply steps the counter, so we do that and don't have to worry about doing anything else at the begining. Then we assign the label (since there's no point in having one command to add the number and then another to assign a label to it). To place the label, we use the \endequation command. As we're doing this inside the mathematics, the assignment \let\]\endequation only holds until the end of the mathematics and so it doesn't affect future mathematics (which is a bit lucky for future mathematics).
- 628
- 4
- 9
- 17
- 153,724
- 43
- 389
- 751
-
1
-
2And there's a typo in the definition of
\addlabel: it should be\label{#1}, instead of\label{pythagoras}. – Gonzalo Medina Apr 01 '11 at 13:27 -
@Gonzalo: (amsmath) Probably not, but the question didn't specify a package and was clearly not looking for a "robust" solution so I didn't spend much effort on it. (pythagoras) Whoops! That was my test case. Still, Leo's looks simplest so I'll not bother sorting out the problems with mine! – Andrew Stacey Apr 01 '11 at 17:58
To build on Konrad's answer and make it work with amsmath, I'd use:
\newcommand{\myequation}{\begin{equation}}
\newcommand{\myendequation}{\end{equation}}
\let\[\myequation
\let\]\myendequation
- 756
-
-
Note that there's a pitfall if you use this for
align: environments - What is wrong with defining \bal as \begin{align}? - TeX - LaTeX Stack Exchange – user202729 May 31 '23 at 13:08
amsmathpackage is loaded. – Gonzalo Medina Apr 01 '11 at 13:52amsmathand use your suggestion, all the equations written with\[...\]will receive the same numbering: (1). – Gonzalo Medina Apr 01 '11 at 14:06\beginetc., see the last few lines ofamsmath.sty– Philipp Apr 01 '11 at 19:25amsmathand this is working just fine... – Alec Jacobson Dec 02 '19 at 17:02