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 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 \[.

share|improve this question
12  
    
amsmath has a coherent redefinition of \[. But for some aspects of the LaTeX \[, see my answer –  jfbu May 19 at 12:33

4 Answers 4

up vote 59 down vote accepted

As I understand your question, you'd like to know what the differences are between constructing display-math expressions using the following five methods:

  1. $$ ... $$
  2. \[ ... \]
  3. \begin{displaymath} ... \end{displaymath}
  4. \begin{equation} ... \end{equation}
  5. \begin{gather*} ... \end{gather*}. Actually, you asked about the align environment, but 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 terminated display math mode in LaTeX documents is nowadays heavily deprecated. See, for instance, the posting "Why is \[ ... \] preferable to $$?" for a detailed discussion of why one should not employ $$ ... $$ directly when using LaTeX (or one 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 $$ 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 not in "outer" (aka "normal") math mode. Aside: What is "inner" math mode? An example of "inner" math mode is the material inside a \left( ... \right) pair. If LaTeX encounters \] before \right) it produces the 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, it avoids inserting some additional vertical space by issuing the command \nointerlineskip. In the following example, a bunch of emdashes is 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 it's generated by \[...\], in the second case by $$...$$. Note the extra vertical space that's inserted above the second displayed equation.

enter image description here

\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)}}

This 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, described on pp. 186-7 and elsewhere in the TeXBook, that 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 an equation 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 $$ macros. 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.

share|improve this answer
3  
This is a great answer. Thank you! –  niklasfi Jan 9 '12 at 18:39
    
Your link seems to be broken. –  celtschk Jan 9 '12 at 20:07
    
@celtschk: Thanks for pointing out the problem; I've fixed the link. –  Mico Jan 9 '12 at 20:12
    
@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. –  jfbu May 19 at 11:39
    
@Mico why do not the reasons disqualifying $$ also disqualify \begin{equation}? why isn't \[ coherent in vertical spacing with \begin{equation}? –  jfbu May 19 at 11:52

$$ 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 $$

share|improve this answer
    
thanks for editing, was just looking up the quote syntax, and by the time I came back you'd fixed it:-) –  David Carlisle Jan 9 '12 at 10:21
1  
Welcome back to TeX.SX! –  egreg Jan 9 '12 at 15:10

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)

share|improve this answer
    
An example of this spacing difference can be found at align vs equation. –  Peter Grill Jan 9 '12 at 20:20
    
And what is the difference between \[...\] and \(...\). –  jafan Nov 15 '13 at 18:13
1  
The first is displayed math the other is a slight alternative to $... $ –  daleif Nov 15 '13 at 22:53

The differences have been described, in earlier answers, I am just adding here for illustrative purposes regarding vertical spaces this image:

test Lamport

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:

again Lamport vs $$

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 \[.

share|improve this answer
    
can I offer a bounty to the first upvoter? –  jfbu May 19 at 12:36
1  
OK, I've upvoted your answer. :-) –  Mico May 19 at 13:00
    
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 $$...$$. –  Mico May 19 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. –  jfbu May 19 at 13:55
    
@Mico I have replaced in my code sample \hrule by 15 successive \textemdashes 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 ;-)) –  jfbu May 19 at 14:00

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.