Syntax to use \newcommand in a post

What is the syntax to use \newcommand in a post?
I tried

\$\newcommand{\foo}{1+1}\$

But this leave a blank line.
Also, for severals \newcommand

\$\$
\newcomand{\foo}{1+1}
\newcomand{\bar}{2+2}
\$\$

also leaves blank lines in the post

– robjohn Mod
Feb 2 '13 at 16:50
• The link you gave didn't work, but this one do.
– user14108
Feb 2 '13 at 17:47
• @NicolasEssis-Breton: the link I gave must need 2K to work. Did you fabricate yours by substituting the ID of my answer into the link for Asaf's revisions?
– robjohn Mod
Feb 2 '13 at 18:09
• Yes, that's what I did.
– user14108
Feb 2 '13 at 18:11
• Unfortunately, this bug is fixed now Apr 6 '17 at 6:41
• ...whoops, actually it isn't fixed unless you install the Stack Overflow Unofficial Patch Apr 6 '17 at 7:09
• Currently \newcommand should behave correctly again, and be particular to a specific post, if I'm not mistaken. Mar 27 '19 at 13:15
• I will add to the previous comment that the change which limits scope of the macro to a single post (single comment) was announced in January 2019 as on this meta. This change is also mentioned in this list: Recent feature changes to Stack Exchange (2019). Feb 13 '20 at 11:29