8
$\begingroup$

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

$\endgroup$
1
5
$\begingroup$

I usually use \newcommand at the end of the first paragraph, which is usually introductory which does not contain much LaTeX. You can use several \newcommand sequentially within a single $ environment and it works fine.$\newcommand{\example}{\mathsf{For\space example\space as\space in\space this\space post}}$

$$\example$$

$\endgroup$
2
  • $\begingroup$ I'm glad your answer was accepted. If mine were accepted, it would show first and not be affected by your \newcommand. $\endgroup$
    – robjohn Mod
    Feb 2 '13 at 16:47
  • $\begingroup$ @robjohn: In the case your were accepted it would have made another point clear, but let me rain on your answer from above and say... $\renewcommand{\example}{\mathsf{This\space is\space SPARTA}!}$ $\endgroup$
    – Asaf Karagila Mod
    Feb 2 '13 at 19:25
3
$\begingroup$

Beware the side-effects of using \newcommand in a post or in chat. As MathJax is currently implemented, it affects all the subsequent posts on the page; e.g. $$ \example $$ This might make sort dependent rendering of answers.

$\endgroup$
12
  • 2
    $\begingroup$ Not to mention an overrun by marauders!$$\sin(x)=\newcommand{\sin}{\text{TROLL'd!}}\sin(x)$$ $\endgroup$
    – Asaf Karagila Mod
    Feb 2 '13 at 15:57
  • $\begingroup$ The edit button is not clickable for me. Is there another way to look at the internal of your post? I have the same problem with Asaf post, but there I could click on edited 1 hout ago. $\endgroup$
    – user14108
    Feb 2 '13 at 16:10
  • 1
    $\begingroup$ @NicolasEssis-Breton: right-click on the rendered MathJax and a contextual menu should pop up. Choose "Show Math As > TeX Commands" and you should see \example. $\endgroup$
    – robjohn Mod
    Feb 2 '13 at 16:40
  • 1
    $\begingroup$ @NicolasEssis-Breton: you can try this link, but that might not work until you have 2K rep. Being on a different page, the \example command is not defined, so it is left unrendered in red. $\endgroup$
    – robjohn Mod
    Feb 2 '13 at 16:44
  • 1
    $\begingroup$ @AsafKaragila: $e^{ix}=\cos(x)+i\sin(x)$ $\endgroup$
    – robjohn Mod
    Feb 2 '13 at 16:50
  • $\begingroup$ The link you gave didn't work, but this one do. $\endgroup$
    – user14108
    Feb 2 '13 at 17:47
  • $\begingroup$ @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? $\endgroup$
    – robjohn Mod
    Feb 2 '13 at 18:09
  • $\begingroup$ Yes, that's what I did. $\endgroup$
    – user14108
    Feb 2 '13 at 18:11
  • $\begingroup$ Unfortunately, this bug is fixed now $\endgroup$ Apr 6 '17 at 6:41
  • $\begingroup$ ...whoops, actually it isn't fixed unless you install the Stack Overflow Unofficial Patch $\endgroup$ Apr 6 '17 at 7:09
  • 1
    $\begingroup$ Currently \newcommand should behave correctly again, and be particular to a specific post, if I'm not mistaken. $\endgroup$ Mar 27 '19 at 13:15
  • $\begingroup$ 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). $\endgroup$ Feb 13 '20 at 11:29

You must log in to answer this question.