# The correct way to mark formulas in LaTeX

Published on

The first thing I learned when I was introduced to LaTeX in 2003 was that you put inline formulas in $...$ and display formulas in $$...$$. Over the years I became aware of the alternative forms, $$...$$ and $...$, but I found them ugly (because they are) and assumed they were relics from the early days of TeX, sort of like trigraphs in C.

As it turns out, it’s all exactly the other way around. $...$ and $$...$$ are the old TeX syntax; $$...$$ and $...$ are the new LaTeX syntax.

Couldn’t Leslie Lamport think of a better syntax? I bet he could; he just couldn’t implement it. These days if we don’t like a language, we create a new one, with its own parser. But Lamport’s LaTeX is not a new language. It is a set of macros that work on top of Knuth’s TeX. What LaTeX has achieved is a testament to TeX’s flexibility and Lamport’s ingenuity. The $...$ “syntax” is just a pair of TeX macros, $ and $, and like all other control sequences they have to start with a backslash.

So why should we prefer Lamport’s ugly $$...$$ and $...$ to Knuth’s beautiful $...$ and $$...$$?

You can find the exact details in the answers to the following TeX.SE questions:

Another good reference is An essential guide to LaTeX2ε usage: Obsolete commands and packages.

It boils down to the fact that $$...$$ and $...$ can be (and are) redefined by packages, whereas $...$ and $$...$$ can’t.

The main example you’ll find in the above TeX.SE discussion is that $$...$$, when used between paragraphs, inserts unwanted white space; so $...$ is (re)defined to suppress it.

The specific problem I had with $$...$$ is that it breaks the lineno package, which is used to add line numbers to the output document. The lines preceding a display formula don’t get numbered:

This is a problem with all the equation environments, as described in the lineno manual, section 7.1. lineno redefines the $ and $ macros and displaymath, equation, eqnarray, and eqnarray* environments to work around this issue. But lineno can’t redefine the $$...$$ syntax; hence if you use it, it’ll break the numbering. (Oddly enough, lineno also doesn’t redefine the equation* environment, which is another problem I’ve run into.)

Here’s a cool thing: pandoc will automatically emit the new LaTeX formula syntax regardless of what syntax you use in the input file. This means you can continue to use $ and $$ in markdown and get the correct result: % echo '$$a^2+b^2=c^2$$' | pandoc -f markdown -t latex $a^2+b^2=c^2$ If you feel adventurous, you could even filter your LaTeX source through pandoc: % echo '$$a^2+b^2=c^2$\$' | pandoc -f latex -t latex
$a^2+b^2=c^2$