15

Is it possible to modify LaTeX so that good old $$...$$ behaves like \[...\] (and by this I mean that it should be synonymous to it).

If yes: Dear LaTeX3 developer, please integrate this into LaTeX3! (Not that I really believe you will actually do this.)

MWE (even if I don't really see its use in this case):

\documentclass{article}

\begin{document}

I've forgotten we're not in the plain \TeX\ world anymore:
$$
x = 3\alpha^2 + \beta
  = \int f\, d\mu.
$$

\end{document}
Joseph Wright
  • 259,911
  • 34
  • 706
  • 1,036
Gaussler
  • 12,801
  • 14
    If you forget that you're not in plain TeX, you should correct yourself, not the system. No, this is effectively not possible. You could implement it, but it would be too fragile to be usable in general. As well, feature requests don't belong on this site :-) (that's to the "LaTeX3" part of your text); I'm also sure that I know at least two LaTeX3 developers who would never implement this :-) – yo' Apr 23 '15 at 10:23
  • I was trying to explore the possibility of whether it could (theoretically) be integrated into LaTeX3. It is a more aesthetic and simple notation than \begin{align*}...\end{align*}. And if a new format is designed around this, it should be less fragile (I assume). – Gaussler Apr 23 '15 at 10:28
  • 4
    Sorry, this isn't plain TeX world. You must specify \eqalign or \eqalignno in plain TeX before using &. The native $$...$$ doesn't support this. If you need to do \eqalign at every display then you can set \everydisplay. And my recommendation: forget LaTeX, use simply plain TeX. – wipet Apr 23 '15 at 10:29
  • 1
    I don't share your sense of aesthetics. Typing 30 chars more is not what slows you down when writing a mathematical text. – yo' Apr 23 '15 at 10:29
  • 2
    It probably is possible, but it would be a bad idea, align should not be used for single line equations, and it would be hard to detect in time that there was no second line, also you should not have a blank line before align (or $$) and it is hard to define the macros to correct that. – David Carlisle Apr 23 '15 at 10:31
  • 1
    Of course, the plain TeX code is "more aesthetic" because there are not many curly braces and begin/end something. Why do you use LaTeX and not plain TeX in such case? This is much more simple. – wipet Apr 23 '15 at 10:32
  • @Gaussler Yes, it's certainly possible. However, what about gather* or multline* or alignat* or flalign*? What's the purpose of obscuring one's code for a “gain” which is not such? – egreg Apr 23 '15 at 10:44
  • 8
    @Gaussler I'm also certainly not trying making it work: I think it's completely useless and even unworthy a try. – egreg Apr 23 '15 at 10:50
  • 1
    Personally I don't see the point of this. I would understand if the question was $$…$$ behaving like \[…\], but why should it be like align*? – Franck Pastor Apr 23 '15 at 12:21
  • I perfectly appreciate that as well. In fact, I'll change the question accordingly. – Gaussler Apr 23 '15 at 12:22
  • 1
    For the changed question: look at the l2tabu package to see how it redefines $ as an active character to catch the wrong usage of $$...$$ and modify according to your wish. The usefulness of this still escapes me. – egreg Apr 23 '15 at 12:26
  • 1
    I was trying to find out if it was possible; as mentioned, I personally think this notation should be revived in LaTeX. Even if I'm the only one... – Gaussler Apr 23 '15 at 12:27
  • 1
    I bet they didn't tell you when you joined the site that 1000 'attaboys (http://www.urbandictionary.com/define.php?term=attaboy) entitles you to a gold medal... however, just 1 "Aw $#!t" wipes the slate clean (http://www.urbandictionary.com/define.php?term=clean+slate). You have my condolences, as I can see that the crew is merciless towards this question. – Steven B. Segletes Apr 23 '15 at 12:29
  • 2
    @wipet: Is it necessary to defy LaTeX in almost any case? ;-) It doubt you would programe a graphical interface in assembling language instead of using C(++) or Java ;-) There are definitely some features of LaTeX which should be improved, this will be most probably attacked in LaTeX3 (or LaTeX4.... ;-)) ... The site is called TeX.SX, but the tour page says: Welcome to TeX - LaTeX Stack Exchange –  Apr 23 '15 at 12:30
  • 5
    What I find even more strange is that we take Knuth's old TeX, then replace all of the brilliant and simple notations he created with new, much longer and more fragmented commands. $$...$$ became \[...\], $...$ might one day become \(...\), and \bf, \tt etc. were divided into math and text versions. So instead of a small, logical library of simple commands we got a gigantic, fragmented one. I like LaTeX and the ideas of it and would like to keep using it; I would just have liked if they modified the existing commands. – Gaussler Apr 23 '15 at 12:35
  • 5
    @Gaussler If you don't like the way LaTeX works don't use LaTeX (other people with the same view don't and are quite happy). You can always use plain plus a set of custom macros. (There are reasons for the differences between plain and LaTeX2e syntax.) – Joseph Wright Apr 23 '15 at 12:38
  • (Continuation of my last comment.) LaTeX is based on the idea that TeX is a good program; then why at the same time do we do whatever is in our power to call it wrong and replace its notation? It is love and hate at the same time. We end up with a small TeX program at the bottom and a gigantic library of LaTeX on top of it based on totally different ideas. If TeX is so wrong, then why did we not create a new program? – Gaussler Apr 23 '15 at 12:42
  • @JosephWright, I like LaTeX a lot; I like to let go of design decisions. There are just some choices in its design that I don't understand. Like, why do we not just modify the existing commands instead of creating new ones? (It sounds as if it is possible.) – Gaussler Apr 23 '15 at 12:43
  • @JosephWright Where can we learn more about the reasons of LaTeX commands having different names than their Plain TeX counterparts? I mean, why would it be “bad” to redefine these commands while retaining their names? Sorry if the question is naïve… – Franck Pastor Apr 23 '15 at 12:46
  • 2
    @fpast (and Gaussler) That would be a different question :-) For example, for math mode the LaTeX approach is that \( ... \) and \[ ... \] have to match in pairs. LaTeX2e does allow $ and that means $$ still works as at the time of design tokens were scarce (nowadays with e-TeX and lots of space you can move $ as a math shift token out of the way). The short font commands get mentioned in http://tex.stackexchange.com/questions/15361/will-two-letter-font-style-commands-bf-it-ever-be-resurrected-in-latex, but probably a separate question would allow a better answer. – Joseph Wright Apr 23 '15 at 12:54
  • @JosephWright But nobody actually answered that question. He asked if \bf, \tt etc. would ever get the same meaning as \mathbf/\textbf, \mathtt/\texttt etc., allowing them to be used again. And all he got was preaches why \bf, \tt are obsolete. Well, his idea as to make them up to date. – Gaussler Apr 23 '15 at 12:58
  • 3
    @ChristianHupfer I am only surprised why OP calls for plain TeX simplicity and he don't use simply plain TeX. – wipet Apr 23 '15 at 13:01
  • 1
    @wipet Because OP likes LaTeX (because he is not a good document designer and likes to let others take care of that), but does not understand why the commands in LaTeX did not keep the name of their TeX counterparts, for the sake of simplicity. – Gaussler Apr 23 '15 at 13:03
  • @Gaussler --- \bf, \tt etc. were used in LaTeX 2.09 and their meaning was preserved in 2e to maintain compatibility with old documents. Also, you might be interested in Barbara Beeton's comment here regarding Donald Knuth's thoughts on the use of $ symbols for entering and exiting math mode. – Ian Thompson Apr 23 '15 at 13:21
  • 2
    \bf and friends are not available with memoir by default. KOMA-classes give warnings. – Johannes_B Apr 23 '15 at 13:23
  • Another approach is taken by onlyamsmath.sty which causes $$...$$ to throw an error. I rather agree with others that it would be better not to legitimize plain TeX practice in LaTeX. So onlyamsmath.sty might be one approach to breaking someone of the habit. It also enforce some other policies. – sgmoye Apr 24 '15 at 17:19

4 Answers4

21

This will no doubt be a controversial answer, but here goes nothing!1

What you are asking is flawed in its premise: if you wanted to modify $$...$$ to behave like \[...\] it is not LaTeX, but TeX, that you would have to modify. LaTeX does not change the behavior of $$ at all from the TeX definition.

All that \[...\] is is a wrapper around $$...$$ to do some mode checks, provide some enhanced error reporting, and change some spacing if the display occurs at the beginning of a paragraph. You can see the complete definition in the sources and/or at the accepted answer to What are the differences between $$, \[, align, equation and displaymath?.

So LaTeX took the Plain TeX basics and extended it to cover the use cases of a presumed majority of users. It's no different than any other macro provided by LaTeX: it's an augmentation of the building blocks provided by TeX.

In my opinion, if you want the behavior of \[...\], simply use \[...\]. Redefining $$, while probably technically possible, is nontrivial because the very command defining the desired behavior uses it internally. Additionally, there's no telling how many (sometimes badly-designed, but nonetheless) documents rely on the old functionality.


1 Hold my beer!

Paul Gessler
  • 29,607
  • 1
    This seems more a comment than an answer.... – JPi Apr 24 '15 at 12:25
  • 2
    @JPi I (and other users of the site) respectfully disagree. It answers the question asked (Can I get $$…$$ to behave like \[…\]?) by saying: probably, but it would be difficult (explaining why) and would potentially break lots of other stuff and would not provide much benefit. There are many cases on the site where the answer to a question is, "well you probably could, but it's not a great idea". – Paul Gessler Apr 24 '15 at 12:34
17

Here's a LuaLaTeX-based solution to your challenge.

The Lua function replace_ddollar doesn't actually redefine the $$ primitive. Instead, it scans the input text at a very early stage of processing (before TeX's "eyes" start doing anything) and replaces all instances of $$ with either \[ or \]. The Lua code can handle code such as

 $$a^2+b^2=c^2$$ 

i.e., matched pairs of $$ directives on a single line, as well as the usual entry format for equations, i.e.,

$$
E = mc^2
$$ 

Remark: Precisely because $$ is not being redefined in this approach, things can (and will) go wrong if the document contains instances of $$ that are not used to initiate or terminate display-math mode. Some examples of such instances:

  • Unmatched instances of $$ in a comment (including comment-like environments)
  • Instances of $$ in a verbatim-like environment
  • Instances of $$ in a URL string encased in a \url{...} directive

(I'm sure there are still more possibilities for things to go wrong.) Just in case you do have such instances in your document, the code below provides the macro \ReplaceDoubleDollarOff, which turns off the operation of the Lua function replace_ddollar. There's also a companion macro, called \ReplaceDoubleDollarOn, that switches the Lua function back on.

The risk posed by instances of single instances of $$ on an input line that are not meant to initiate or terminate displaymath mode can be greatly reduced if it can be assumed that the only instances of $$ that are supposed to initiate or terminate displaymath mode occur at the very start of a line: If this assumption is valid, simply reduce the search string in the second string.gsub function, "%$%$", with "^%$%$". The ^ character indicates that a match can occur only if $$ occurs at the very start of a line. [In case you're curious why the Lua code contains %$%$ rather than just $$: In Lua, the $ character is "special" and has to be escaped (by prefixing a % symbol) in order to denote an actual $ symbol.]

enter image description here

% !TEX TS-program = lualatex
\documentclass{article}
\usepackage{luacode}    % for 'luacode' environment
\begin{luacode}
in_display_math = false
function replace_ddollar ( line )
   line = string.gsub ( line , "%$%$(.-)%$%$" , "\\[ %1 \\]" )
   line = string.gsub ( line , "%$%$" , function (x)
         if not in_display_math then
            in_display_math = true
            return "\\["
         else
            in_display_math = false
            return "\\]"
         end
     end )
   return line
end
\end{luacode}

\newcommand\ReplaceDoubleDollarOn{%
    \directlua{ luatexbase.add_to_callback( 
    "process_input_buffer", replace_ddollar, "replace_ddollar" )}}
\newcommand\ReplaceDoubleDollarOff{%
    \directlua{ luatexbase.remove_from_callback( 
    "process_input_buffer", "replace_ddollar" )}}
\ReplaceDoubleDollarOn % Replacement function turned on

\usepackage{url}  % just for this example

\begin{document}
$$
E = mc^2
$$ 

$$a^2+b^2=c^2$$ $$d^2+e^2=f^2$$ % Aside: I do not endorse this coding style!

$$
x = 3\alpha^2 + \beta = \int f\, d\mu.
$$

% Turn off replacement of double-dollars
\ReplaceDoubleDollarOff  

\url{A_URL_string_with_a_$$_and_$$$$_and_another_$$}

% Turn replacement of double-dollars back on
\ReplaceDoubleDollarOn 

$$ e^{i\pi}-1=0 $$

$$
1+1=2
$$ 
\end{document}
Mico
  • 506,678
  • That is probably an exercise I cannot solve, since I don't know Lua, let alone how to use it in LuaTeX. – Gaussler Apr 23 '15 at 15:17
  • 1
    Does this deal correctly with non-display instances of $$, e.g. I have \$$33.20$? (I checked: it does not. It eats the rest of the paragraph, and is out-of-sync with display start/stops for the rest of the document.) – Nick Matteo Apr 23 '15 at 15:20
  • 1
    @Gaussler - I've updated the code so that it can handle pairs of $$ directives on one line of input. – Mico Apr 23 '15 at 15:33
  • @Kundor - I honestly hadn't thought of cases such as \$$33.20$. (With the update to the code, cases such as $$\$33.20$$ are handled correctly...) I suppose one could always write \${}$33.20$, i.e., use an empty group -- or something functionally equivalent -- to break up the $$ pattern. – Mico Apr 23 '15 at 15:36
  • If you know how to use Lua patterns (I do not), it is probably possible to only match instances of $$ not preceded by , no? However, problems remain (verbatim environments; any unmatched $$ within a comment will throw off the rest of the document…) A robust solution along these lines would probably take excessive effort. – Nick Matteo Apr 23 '15 at 15:46
  • @kundor - The code definitely makes quite a few implicit assumptions that may or may not apply in a real document. If generalizing the code to apply to extra use cases is of importance to you, please feel free to post a new query (with a link to the current one), asking for more robust code. Give the other Lua experts in this forum a chance to weigh in too! :-) – Mico Apr 23 '15 at 15:52
  • That said, it would help to reset found_double_dollar to false on each \par, since new paragraphs cannot occur within a math environment: this would just eat one paragraph, instead of cascading failures through the rest of the document ;-) – Nick Matteo Apr 23 '15 at 15:52
  • Ah, that doesn't work as I'd have hoped, since the text replacement is done before TeX processes it. – Nick Matteo Apr 23 '15 at 16:02
  • @Kundor: I've modified the code to allow the user to switch the Lua function replace_double_dollar off -- say, because of a verbatim environment or URL strings that contain instances of $$ -- and back on again. Hopefully, the two new LaTeX macros will address the majority of use cases of $$ not being used to initiate and terminate display-math mode. – Mico May 03 '15 at 14:13
6

This is perhaps the easiest implementation. I still wish to stress not to use code like this. It either goes wrong from the get go, or worse: at one point you wish to change something in the document, you get an error which tells you you're missing an \item (or something else unrelated to anything math mode), and you have to recode your entire document because everything breaks down.

\documentclass{article}
%\usepackage[fleqn]{amsmath} % also works for instance

\let\dollar=$ \catcode`$=\active
\makeatletter
\protected\def${\@ifnextchar${\@doubledollareqn}{\@singledollareqn}}
\def\@singledollareqn#1${\(#1\)}
\def\@doubledollareqn$#1$${\[#1\]}
\makeatother

\begin{document}

Inline math $x^2+y^2=z^2$.

$$
f(v)=4\pi\left(\frac m{2\pi kT}\right)^{\frac32}v^2e^{-\frac{mv^2}{2kT}}.
$$
\end{document}
1010011010
  • 6,357
5

This solution is not perfect: it does no error checking, and probably has other issues. It makes $ and active character, which breaks some packages that don't expect that. But if you put the following near the end of your preamble, it may mostly work. I tried it on two of my documents and it seemed ok.

\catcode\`$\active
\makeatletter
\protected\def\ExpectBeginMath{\@ifnextchar${\let$\ExpectEndDisplayMath\[\@gobble}{\let$\ExpectEndInlineMath\(}}
\def\ExpectEndDisplayMath{\]\let$\ExpectBeginMath\@gobble}
\def\ExpectEndInlineMath{\)\let$\ExpectBeginMath}
\makeatother
\let$\ExpectBeginMath

Or something like that.

Mark
  • 1,393