0

In LaTeX, when I need to create an equation without a number, I write

\[ x=y \]

(or using equation*) and when I need an equation with a number/label I do

\begin{equation}
    x=y \label{eq:myeq}
\end{equation}

My question is: is there a way to somehow combine both modes, so that only the label is required to have an equation number?

In other words, I want to somehow make it so that writing something like this

 \[ x=y \label{eq:myeq} \]

produces an equation with a number and a label, but writing \[ x=y \] still produces just an equation. This seems to me to be a more elegant way of doing things.

  • 2
    Laziness isn't the best advisor ;-) I think it is possible but it will bite you in the end – user31729 Jun 5 '17 at 13:06
  • I don't particularly advise you to change anything. When you are writing a thesis and you want to rehearse at a certain point with a hypertext reference the equation environment very helpful. If you mess around with any re-definitions chances are that you will end up with some error. This will be particularly annoying if you are writing a long thesis. You don't want to correct a source code for 30+ pages, do you. Stick with equation. – Gregor Perčič Jun 5 '17 at 13:13
  • The point of this method is two fold: the first is laziness, or as I like to call it "code brevity". The second is more significant. Most of the time when writing manuscripts, I would come across an equation that I need to reference and which I used \[\] to typeset. Making it reference-able requires not only adding the label, but also changing the environment (the same is true if I had been using equation*). Which is both annoying and wasteful. I guess just don't the reason behind the original design requiring two modification to the script to have a label, instead of an obvious one. – Tohiko Jun 5 '17 at 13:21
  • 2
    @Tohiko: \[...\] is the better LaTeX form of $$...$$, which is plain TeX, which does not not know of labels as you want to have them. – user31729 Jun 5 '17 at 13:24
2

I don't recommend this really, but here's a way for \[...\] being the same like \begin{equation}...\end{equation} (which basically is true anyway, since both environments use $$...$$ in the end again)

It does not work with amsmath, however!

\documentclass{article}

\usepackage{xpatch}
\let\[\equation
\let\]\endequation


\newif\iflabelused

\makeatletter
\xpretocmd{\[}{\global\labelusedfalse}{}{}
\xpretocmd{\label}{\global\labelusedtrue}{}{}
\xpretocmd{\]}{%
  \iflabelused
  \global\labelusedfalse%
  \else
  \let\eqno\relax
  \let\@eqnnum\relax
  \global\labelusedfalse
  \addtocounter{equation}{-1}% Correct the value of the equation counter!
  \fi
}{}{}
\makeatother

\begin{document}

\[ 
E = mc^{2} \label{foo}
\]

\[ 
E^{2} = (mc^{2})^{2} + (pc)^{2} 
\]


\[ 
E = mc^{2} \label{fooagain}
\]

\[ 
E^{2} = (mc^{2})^{2} + (pc)^{2} 
\]

\end{document}
  • Shame that it doesn't work with amsmath, but this looks very interesting. I thought using \notag instead of playing with counters might fix it, but it doesn't it. I will try to play around with this code. Thanks! – Tohiko Jun 5 '17 at 13:59
  • @Tohiko: I'll try to fix it for amsmath... – user31729 Jun 5 '17 at 14:14

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.