I once learnt, that it is uncool to use $$
, but why is that? Why does \[
not act as a abbreviation to \begin{align}
? I noticed, that there is a difference, since one cannot use &
and \\
inside a block started with \[
.
5 Answers
As I understand your question, you'd like to know what the differences are between constructing display-math expressions using the following five methods:
$$
...$$
\[
...\]
\begin{displaymath}
...\end{displaymath}
\begin{equation}
...\end{equation}
\begin{gather*}
...\end{gather*}
.
Actually, you asked about the align
environment. However, I think that for the sake of providing a straightforward comparison among the display-math methods, it's better to focus on the gather*
environment, which centers its contents and doesn't generate an equation number. Compared with the gather
and gather*
environments, the significant added capability of the align
and align*
environments is their ability to vertically align equations along certain elements, such as on equal signs.
The first method is the "Plain TeX" method for generating displayed equations. ("Plain TeX" refers to a set of macros, written by Knuth, designed to make the so-called "TeX primitives" usable for ordinary typesetting purposes. A full explanation of what the $$
specifiers do is provided on p. 287 of the TeXBook.)
However, using the $$
method to initiate and terminate display-math mode in LaTeX documents is nowadays heavily deprecated. See the posting "Why is \[ ... \] preferable to $$ ... $$?" for a detailed discussion of why one should not employ $$ ... $$
directly when using LaTeX (or one of its successors, such as pdfLaTeX, XeLaTeX, etc).
The second method, \[
and \]
, is Leslie Lamport's re-implementation of the Plain-TeX $$
... $$
method. The LaTeX code that defines the \[
and \]
commands is contained in the file latex.ltx
:
\def\[{%
\relax\ifmmode
\@badmath
\else
\ifvmode
\nointerlineskip
\makebox[.6\linewidth]{}%
\fi
$$%%$$ BRACE MATCH HACK
\fi
}
\def\]{%
\relax\ifmmode
\ifinner
\@badmath
\else
$$%%$$ BRACE MATCH HACK
\fi
\else
\@badmath
\fi
\ignorespaces
}
Basically, \[
and \]
act as carefully designed wrappers around the opening and closing $$
directives. Error messages will be generated if a \[
statement is encountered while TeX is already in math mode or if a \]
statement is encountered while TeX is either not in math mode at all or if it is in "inner" math mode. You may ask, "What is 'inner' math mode?" A leading example of "inner" math mode is the material inside a \left<x> ... \right<y>
group, where <x>
and <y>
denote suitable delimiters, e.g., (
and )
. If LaTeX encounters \]
while in inner math mode, it produces the following error message:
! LaTeX Error: Bad math environment delimiter.
(If you were using $$
directly you'd get a slightly more cryptic error message.)
Furthermore, the command \[
checks if TeX is in so-called "vertical mode"; this happens to be the case, most commonly, at the start of a paragraph. If that's the case, i.e., if TeX is in vertical mode, the instruction \nointerlineskip
is executed to avoid inserting some additional vertical space immediately above the displayed equation.
This issue is illustrated in the following example, which contrasts two cases. In both cases, the code starts with a bunch of em-dashes, followed by a blank line (which inserts a paragraph break and switches TeX to vertical mode) and then a displayed equation. In the first case, the displayed equation is generated via \[...\]
; in the second case, $$...$$
is employed. Note that in the second case, some extra (and presumably unwanted) vertical whitespace gets inserted above the displayed equation; this does not happen in the first case, courtesy of the \nointerlineskip
directive that's part of the \[
directive.
\documentclass{article}
\setlength\textwidth{1in} % use a very narrow measure for this example
\begin{document}
\noindent
---------------
\[ u=vwxz\]
---------------
$$u=vwxz$$
---------------
\end{document}
The LaTeX code for the third method is simply:
\def\displaymath{\[}
\def\enddisplaymath{\]\@ignoretrue}
i.e., it is (essentially) equivalent to the second method, while arguably being easier to read and debug. By easier to debug, I mean that these commands are not as easy to overlook as are \[
and \]
.
Note that these first three methods do not generate equation numbers.
As implemented in LaTeX -- but without loading the amsmath
package -- the code for the fourth method (\begin{equation}
... \end{equation}
) is:
\@definecounter{equation}
\def\equation{$$\refstepcounter{equation}}
\def\endequation{\eqno \hbox{\@eqnnum}$$\@ignoretrue}
\def\@eqnnum{{\normalfont \normalcolor (\theequation)}}
The fourth method thus also provides a "wrapper" around the (internally generated)
$$
pair of commands, while adding a right-aligned equation number that's surrounded by parentheses. (The\eqno
macro is a TeX macro that's described on pp. 186-7 and elsewhere in the TeXBook; it inserts an equation number given by\@eqnum
.)However, do note that unlike in the case of
\[...\]
, no test is performed to check whether TeX is in vertical mode when\begin{equation}
is executed. Thus, if you start anequation
environment at the start of a paragraph, you may get some extra (and probably unwanted!) vertical space above that equation. This is one of the reasons for the freqently-encountered exhortation never to start a displayed equation at the start of a paragraph.
Finally, about the \begin{gather*}
... \end{gather*}
method that's made available by loading the amsmath
package. Before examining the details of this method, it's important to mention that the amsmath
package provides the commands \mathdisplay
and \endmathdisplay
, which serve as (even more elaborate) "wrappers" around -- you guessed it -- the plain-TeX $$
constructs. The amsmath
package provides a redefinition of the equation
environment and a new environment for unnumbered equations called equation*
:
\renewenvironment{equation}{%
\incr@eqnum
\mathdisplay@push
\st@rredfalse \global\@eqnswtrue
\mathdisplay{equation}%
}{%
\endmathdisplay{equation}%
\mathdisplay@pop
\ignorespacesafterend
}
\newenvironment{equation*}{%
\mathdisplay@push
\st@rredtrue \global\@eqnswfalse
\mathdisplay{equation*}%
}{%
\endmathdisplay{equation*}%
\mathdisplay@pop
\ignorespacesafterend
}
As you can see, the equation
and equation*
environments eventually call the \mathdisplay
and \endmathdisplay
commands, which, in turn, call the TeX $$
directive. Incidentally, amsmath
also redefines the LaTeX commands \[
and \]
as follows:
\DeclareRobustCommand{\[}{\begin{equation*}}
\DeclareRobustCommand{\]}{\end{equation*}}
(Observe that because displaymath
is defined in terms of the \[
and \]
commands, its appearance too may change when used in combination with the amsmath
package.)
On, then, to the code for the gather*
environment:
\newenvironment{gather*}{%
\start@gather\st@rredtrue
}{%
\endgather
}
where the \start@gather
command is defined as
\def\start@gather#1{%
\RIfM@
\nomath@env
\DN@{\@namedef{end\@currenvir}{}\@gobble}%
\else
$$%
#1%
\ifst@rred \else \global\@eqnswtrue \fi
\let\next@\gather@
\fi
\collect@body\next@
}
The command \endgather
, which is defined implicitly, executes the following instructions:
\math@cr \black@\totwidth@ \egroup
$$\ignorespacesafterend
The main thing to take away from examining these lines of code -- without going into too many of the details, many of which relate to the fact that the align, align*, gather, gather*, etc environments can typeset multiple, consecutive lines of displayed-math material -- is that the gather*
environment too (eventually) calls the $$
TeX macro, while taking care of quite a few housekeeping and error-avoidance steps along the way.
In sum, one thing that should be amply clear from all this is that using the $$
constructs directly is unnecessary and even reckless, as doing so risks causing all kinds of messy screw-ups. The alternative methods 2 through 5 take care to avoid these problems and should therefore always be preferred to using the $$
pair of commands.
-
-
-
@Mico you said
If that's the case, it avoids inserting any additional vertical space by issuing the command \nointerlineskip.
. But quite to the contrary, LaTeX then issues an empty line adding vertical space and forcing the use of the long form of the above and below display skips.– user4686May 19, 2014 at 11:39 -
1@Mico why do not the reasons disqualifying
$$
also disqualify\begin{equation}
? why isn't\[
coherent in vertical spacing with\begin{equation}
?– user4686May 19, 2014 at 11:52 -
2@Mico your example with the extra vertical space above a
$$
encountered in vertical mode is not convincing because\nointerlineskip$$
fixes the issue. But\[
does a bit more than\nointerlineskip
it creates a horzontal box of width.6\linewidth
and this can NOT be undone!– user4686May 19, 2014 at 19:47
$$
is not supported in LaTeX at all, it is primitive TeX syntax that just happens to sort of almost work most of the time. The standard LaTeX classes for example all have a fleqn
option to align rather than center \[ \]
. Even with no options, the behaviour of \[
and $$
are different at the start of a paragraph, where \[
does
\nointerlineskip
\makebox[.6\linewidth]{}%
before doing $$
-
thanks for editing, was just looking up the quote syntax, and by the time I came back you'd fixed it:-) Jan 9, 2012 at 10:21
-
2
The differences have been described, in earlier answers, I am just adding here for illustrative purposes regarding vertical spaces this image:
which is obtained from this code:
\documentclass{article}
\begin{document}
\hrule
\[ E=mc^2\]
\hrule
$$E=mc^2$$
\hrule
\[ E=mc^2\]
\hrule
$$E=mc^2$$
\hrule
\end{document}
Notice the vertical spaces added by the \[..\]
construct, which furthermore are asymmetrical between before and after. Apart from that the \[..\]
does some error checking whose usefulness, IMHO, is limited to the real beginners in LaTeX
.
Here is another image:
obtained from this code:
\documentclass{article}
\begin{document}
\noindent
abc\hrulefill\par
\[ E=mc^2\]
abc\hrulefill\par
$$E=mc^2$$
abc\hrulefill\par
\[ E=mc^2\]
abc\hrulefill\par
$$E=mc^2$$
abc\hrulefill\par
\end{document}
Notice the incoherent vertical spacing resulting from the use of \[..\]
.
I do not include the images here, but one can check that these incoherencies do disappear with \usepackage{amsmath}
(for reasons which are clear from looking at the code reproduced in Mico's answer). I know my opinion is ultra-minoritarian, but clearly, only for people using amsmath
can one convincingly argue it is better to not use $$
directly.
Certainly $$
is not in the spirit of the LaTeX
syntax, but the \[
in LaTeX2e
is not coherent with the other math environment in its handling of vertical spacing. Thus, I personnally concluded that when I am not loading amsmath
, I should rather avoid \[
.
-
1can I offer a bounty to the first upvoter?– user4686May 19, 2014 at 12:36
-
1
-
I think neither your examples invalidates the claim that
\[
-- unlike$$
-- takes care to avoid inserting an extra blank line if it's encountered while TeX is in vertical mode. In your first example, note that TeX is not in vertical mode at the end of\hrule
, hence there's no issue with$$
inadvisedly slipping in an extra blank line. Replace the\hrule
and\hrulefill
directives with a bunch of em-dashes, and it becomes quite apparent that the equation is nicely centered (between the rows of em-dashes) if it's typeset with\[ ... \]
but not if it's typeset via$$...$$
.– MicoMay 19, 2014 at 13:15 -
@Mico are you sure? try replacing
\hrule
with\hrule\ifvmode \typeout{VMODE}\else \typeout{NOT VMODE}\fi
and check the log. And\hrulefill
is followed by\par
. It is true that after\]
or$$
,TeX
is in horizontal mode but this is another issue. I will check what you mean with the em-dashes.– user4686May 19, 2014 at 13:55 -
@Mico I have replaced in my code sample
\hrule
by 15 successive\textemdash
es and then, as expected, because we start in horizontal mode, both\[..\]
and$$..$$
give the exact same result. (by the way I have upvoted your comment about upvoting my answer;-)
)– user4686May 19, 2014 at 14:00
Also align
does not behave the same way as \[...\]
regards to spacing before. \[...\]
and the equation
environment can adjust the spacing above the equation if there is enough
room, align
cannot (because of the manner in which it is constructed)
-
1An example of this spacing difference can be found at align vs equation. Jan 9, 2012 at 20:20
-
-
1The first is displayed math the other is a slight alternative to $... $– daleifNov 15, 2013 at 22:53
An additional answer that is one of the most important for me: the cleveref
package will automatically handle an equation contained in \begin{equation}
but not \[
.
-
8Equations using
\[...\]
are not numbered, and so the concept of\ref
has no meaning in this context. Jan 30, 2017 at 14:24 -
1Your posting appears to apply to all unnumbered environments, not just
equation
-type environments. For the\label
-\ref
(or\label
-\cref
) mechanism to "work", the object (say, anequation
environment) that's being\label
-ed has to be associated with a counter variable that's being incremented (either by the environment itself or via a command such as\caption
). Since\[ ...\]
, by design, does not increment any counters, it's not possible to use the\ref
or\cref
to create cross-references to the non-existent equation numbers.– MicoJan 30, 2017 at 15:41
amsmath
has a coherent redefinition of\[
. But for some aspects of theLaTeX
\[
, see my answer\(...\)
instead of$...$
too.