# Are $$and$$ preferable to dollar signs for math mode?

Along the lines of Why is $...$ preferable to $$?, what reasons are there (if any) to favor $$...$$ over  ... ? - for whatever it's worth, i remember hearing knuth say that if he were creating tex all over again, he wouldn't use a "toggle" approach for math mode, but would use a clean begin/end notation. i'm pretty sure that this question was asked in a q&a session, all of which are republished in his book Digital Typography (the ones held before that book was published, of course). – barbara beeton Oct 8 '11 at 19:13 I once used   to temporarily exit math mode inside a mathematical environment. Don't remember when, and a regexp to find this in my files would be very difficult to imagine. – user2987828 Dec 3 '13 at 22:47 ## 9 Answers $$...$$ is LaTeX syntax.  ...  is TeX syntax. plainTeX only allows . In LaTeX you can use both, but $$...$$ will give less obscure error messages when there is a mistake inside it. Both are shortcuts to start inline math environments. - $$...$$ shouldn't render differently from  ... , although you are right about the checking. The fact that it's possible to 'hook into' $$...$$ (but not  ... ) means that I'd favour the LaTeX syntax. – Joseph Wright Jul 28 '10 at 19:52 I'd note with my 'LaTeX3' hat on that there is a strong chance we'll favour \ ( ... \ ) to the point of not supporting  ...  for LaTeX3. So in the long term it might be best to get used to \ ( ... \ ). – Joseph Wright Aug 17 '10 at 20:35 @Joseph obviously our 2 'LaTeX3' hats have something different under the bonnet then ;-). I'm a strong advocate for providing ... as syntax for in-line math. There aren't really any disadvantages in that and the syntax is simply far more readable, e.g., $$a$$ to a. Displaymath is a different story and there are good reasons for not supporting the plain TeX syntax there. – Frank Mittelbach Jan 16 '12 at 13:01 @FrankMittelbach I have been thinking about this issues, along with others, and suspect a more acceptable approach all round will be to actively deal with $$, either using an active-$ approach or using \everydisplay to trap 'unauthorised' use of $$ (i.e. keep  but deal with $$). – Joseph Wright Jan 16 '12 at 14:50 @FrankMittelbach I 100% agree with you. $a$ is better-to-read, not speaking about $f(x)=(x+1)/2$and$g(x)=(x-1)^{1+x}$are$(n+1)$-free compared to $$f(x)=(x+1)/2$$ and $$g(x)=(x-1)^{1+x}$$$ are $$(n+1)$$-free. Too many (s and {s is simply too many. Dollar is well-recognizable in the code and to be honest, I have never got into the problem of not-knowing whether I'm in or out of $...$. – yo' Feb 3 '12 at 15:46

It is instructive to examine the exact definitions of $$ and $$ that are provided by the LaTeX kernel (contained in the file latex.ltx, version early 2016):

\DeclareRobustCommand$${% \relax\ifmmode\@badmath\else\fi}% \DeclareRobustCommand$${%
\relax\ifmmode\ifinner$\else\@badmath\fi\else \@badmath\fi}%  The main thing to note is that $$ and $$ act as wrappers around the "primitive" $ token:

• $$ checks first if it occurs in ("inner" or "outer") math mode. If that's the case, it issues an error message, viz., "LaTeX Error: Bad math environment delimiter", as it can't be write to try to initiate inline-math mode if TeX is already in math mode. Conversely, if \( is encountered outside of math mode, a single  token is inserted and TeX's inline-math mode is entered. • Likewise, $$ checks first if it occurs in "inner" math mode. If that's the case, a single $ token is generated, terminating inline-math mode. Conversely, if TeX is not in inner math mode when it encounters \), a "LaTeX Error: Bad math environment delimiter" error message is generated. Moreover, both $$ and $$ are declared to be "robust". This means, inter alia, that they can be used inside "moving" arguments, e.g., inside the arguments of sectioning commands and \captions. (Aside: Robustness of these macros was not standard in versions of the LaTeX kernel prior to 2015, necessitating the loading of the fixltx2e package -- which, I think is fair to say, few people ever did...) Do these observations imply that using $$...$$ is necessarily preferable to typing $ ... $? Not necessarily! For instance, if your inline math expressions contain a lot of opening and closing round parentheses, ( and ), the presence of $$ and $$ can be quite distracting if one needs to debug an inline equation and locate where an opening or closing parenthesis may be missing. Typing $$ and $$ may also entail (marginally) more effort than typing $ does. Of course, the input effort will also depend importantly on the editor that's being used.

-

Note that if your LaTeX installation is older than 2015, you shouldn't use $$...$$ unless you also load the fixltx2e package. By default, $$ and $$ were fragile commands and couldn't be used inside section headings or captions.

-
Nice to know! I'm beginning to get the idea that there are lots of these little packages that ought to be in my default 'usepackage' list. – Loop Space Jul 29 '10 at 8:45
Thank you. I have always wondered why I could not use $$...$$ in section headings. – Justin J Stark Mar 25 '11 at 21:37
@Jasper — Generally speaking yes. But "LaTeX3" the concept is still very much under development and isn't yet in a form where it can be used to generate documents from scratch. Our work has been focussed on improving things from within LaTeX2e, which still requires additional packages, etc., to be loaded. – Will Robertson Apr 29 '11 at 5:05
Now that fixltx2e has been merged into LaTeX2e,  is no longer fragile. – krnk Dec 10 '15 at 0:42

There are some good 'meta' reasons for using $$...$$ in some circumstances. Environments such as Fancy Verbatim and alltt will allow $$ and $$ to act as math mode, but $will be interpreted as a literal. Another convenience is that some editors have an easier time with pair matching $$...$$ than $.

-
Fancy verbatim can be configured easily to accept $ as a trigger for math mode. – Konrad Rudolph Sep 7 '10 at 12:04 For users of the package soul: The soul manual states that its macros* accept math mode within their arguments, but only if $...$ is used; i.e. $$...$$ interestingly doesn't work there. *The manual's example is \so{foo$x^3$bar}, which uses the spacing-out macro \so, and I've tested it with the strikeout macro \st. - There is actually a very good reason to prefer $$...$$ over $ ... $, but it's relevant only in one specific context I can think of: If you have a macro that applies an argument like this $#1$, supplying an empty argument when using the macro (which might make, depending on the command, perfect sense) would lead to an obscure error message, whereas $$#1$$ will not. An update (thanks to a comment by @BrunoLeFloch): As the above problem can be circumvented by writing $ #1$ instead, a better way of thinking about this is the following: It is better to teach people $$...$$ than $ ... $, though technically the disadvantages of using the latter are minor and can be circumvented with enough knowledge. Teaching people the former means that they need to pay attention to at least one fewer trap caused by the fact that TeX practically treats both $ and $$ as tokens. - You could use  #1 to keep the plain TeX notation without the odd error message. – Bruno Le Floch Jul 14 '12 at 19:54 @BrunoLeFloch Thanks; see my edit. – Lover of Structure Jul 23 '12 at 5:19 What do you mean by the final emphasized sentence? – egreg Jul 23 '12 at 6:46 That if I teach people about $$...$$, those people (under the naive assumption that they will use only what they've been taught) will not use  ...  and thus don't make the mistake I've described in my answer's first paragraph (namely, the mistake of supplying an empty argument to a macro that uses #1). As @BrunoLeFloch has pointed out, this can be circumvented easily, but a user will do this only if he is aware of the fact that $$ in TeX is practically treated like a single token and not as a sequence of two $-tokens. ... – Lover of Structure Jul 23 '12 at 7:02 ... But a user who has never heard of $ ... $ (in practice a bad assumption, admittedly) won't need to know this, because he'll (theoretically) never use $ ... $. So I only need to teach him "use $$...$$" instead of "you may $ ... $ but avoid accidentally creating the character sequence $$". So, overall one fewer piece of knowledge to teach. – Lover of Structure Jul 23 '12 at 7:04 Interestingly, nobody mentioned that the mathtools package has a very nice feature of adding italic correction in some places at the boundary between text and math mode (see its documentation for details, but aesthetically this really matters!), and this feature works only with $$...$$. - Could you perhaps add a little example with and without mathtools to this answer? – doncherry Jun 9 '12 at 14:09 OK, I'll try to do this later today. – mbork Jun 9 '12 at 14:39 @mbork: Hmm, I'm not really happy with mathtools in that respect - see the comments to this answer. – Hendrik Vogt Jun 9 '12 at 18:44 Configuring $$ and $$ for inline math tags in MathJax is beneficial as you don't need to escape  in your HTML contents. Note: MathJax is a javascript library to render LaTeX commands on the browser. - Anyone who's ever tried writing a simple perl (or whatever) script to quickly parse a LaTeX document and do something in maths mode but not in text mode will agree that $$..$$ is the only way to go! Trying to get the pattern matching right against a^2 + b^2$$c^2 + d^2$ is a nightmare.

(Before anyone asks, yes I have, and it's called mathgrep. The pain and agony of writing that convinced me more than anything else of the value of making everything a configurable macro so it can be easily changed later.)

Another reason: looking at the code, $$ and $$ actually checks that TeX is in the right mode before starting. So hello $$world \(can$$ we\) have some tea? will produce an error, whereas hello $world$can$we$ have some tea? will not.

-
Andrew, I must be the exception that proves the rule. I heavily pre-process almost all my LaTeX input through sed which, as you probably know, uses $$ and $$ for \1, \2, \3... group back-substitution. Just the mere thought of writing and maintaining sed regexp's the "LaTeX-preferred" way sends waves of shudders down my spine!! :)) – Geoffrey Jones Aug 23 '10 at 12:34
From a purely progrmatic point of view I agree that $...$`is slightly less preferable because, as you pointed out, there is no way to automatically identify start and end tokens. However, from a practical point of inputting manuscripts and reading them I still maintain that the plain TeX syntax is superior and in my opinion outweights that disadvantage in this case. – Frank Mittelbach Jan 16 '12 at 18:07
@GeoffreyJones said it! How can LaTeX decisions be made based on which external program users may or may not use for purposes that may or may not be directly related to LaTeX? Don't you think then the reasoning becomes more and more counterintuitive as well as counterproductive? – Amar May 30 '15 at 14:48