Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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.

share|improve this question
add comment

5 Answers

up vote 17 down vote accepted
\documentclass{article}
\usepackage{amsmath}
\newcommand\addtag{\refstepcounter{equation}\tag{\theequation}}

\begin{document}

\[
  1+1=2  \addtag
\]

\end{document}

See also: Equation numbering question

share|improve this answer
    
Thanks, this is exactly what I wanted! –  Otis Apr 1 '11 at 16:48
add comment

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.)

share|improve this answer
    
This is great! Thanks! –  Otis Apr 1 '11 at 16:50
add comment

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.

share|improve this answer
    
This won't handle the numbering appropriately if the amsmath package is loaded. –  Gonzalo Medina Apr 1 '11 at 13:52
    
@Gonzalo Hmm, weird. What’s the issue? –  Konrad Rudolph Apr 1 '11 at 13:58
    
If you load amsmath and use your suggestion, all the equations written with \[...\] will receive the same numbering: (1). –  Gonzalo Medina Apr 1 '11 at 14:06
    
this should use \begin etc., see the last few lines of amsmath.sty –  Philipp Apr 1 '11 at 19:25
add comment

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).

share|improve this answer
1  
This doesn't work if amsmath is loaded. –  Gonzalo Medina Apr 1 '11 at 13:18
2  
And there's a typo in the definition of \addlabel: it should be \label{#1}, instead of \label{pythagoras}. –  Gonzalo Medina Apr 1 '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 1 '11 at 17:58
add comment

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
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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