4

Here is an MWE:

\documentclass{article}
\newcommand{\new}{}
\begin{document}
\new{Why is this printed?}
\end{document}

Which outputs a document that says "Why is this printed?".

My main question is whether someone can explain to me how new command is defining \new here.

A side question out of curiosity is that isn't {Why is the printed?} technically and argument, and \new should by default expect 0 arguments?

Another example is

\documentclass{article}
\newcommand{\new}{$\sigma$}
\begin{document}
\new{test}
\end{document}

which prints the symbol sigma, as defined by the command, but then prints tests after the sigma... However, if I do

\documentclass{article}
\newcommand{\new}[1]{$\sigma$}
\begin{document}
\new{test}
\end{document}

Then I just get the symbol sigma; test is not printed (which I think means that telling it that $/sigma$ is the only argument makes it ignores {test}, an additional argument...

Thanks.

  • 5
    \newcommand{\new}{} has zero arguments which means \new{Why is this printed?} executes \new and then processes the bracketed group, which is not an argument to \new (it has no arguments, remember?). – Steven B. Segletes Apr 17 '16 at 2:54
  • 1
    That makes a lot of sense, and answers my question. Thank you. – majmun Apr 17 '16 at 2:55
  • 2
    In the case of \newcommand{\new}[1]{$\sigma$}, in contrast, the code \new{test} calls \new and passes it the argument {test}, since \new now expects 1 argument. However, \new, while accepting the argument, makes no use of it. If it was defined as \newcommand{\new}[1]{$\sigma$ #1}, it would print out a sigma followed by printing out the argument. – Steven B. Segletes Apr 17 '16 at 2:57
  • @StevenB.Segletes This sounds like an answer to me. – Johannes_B May 1 '16 at 16:10
  • By expansion, in the second place \new{test} would expand to $\sigma${test}; the only “special” thing is the grouped word, but that's something you can have in any text like {this} one or {\em this} one. – Manuel May 1 '16 at 19:21
5

The OP's first example is

\documentclass{article}
\newcommand{\new}{}
\begin{document}
\new{Why is this printed?}
\end{document}

and he/she wonders why "Why is this printed?" get printed out.

The key to understand here is that something following a macro name, even if it looks like an argument, such as \new{Why is this printed?}, is only an actual argument if the macro is expecting one, as called for in its definition. However, the definition

\newcommand{\new}{}

defines \new with zero arguments. Thus, \new{Why is this printed? actually executes \new and only after its completion, comes across the group {Why is this printed?}. It therefore processes the group which in this case just prints out the enclosed text.

The OP's 2nd example is similar, with one distinction:

\documentclass{article}
\newcommand{\new}{$\sigma$}
\begin{document}
\new{test}
\end{document}

Here, the macro \new still does not take on an argument; however, it does print out the math character \sigma. But because \new has no specified arguments, {test} results in the word "test" getting printed out following completion of \new (which printed out the sigma).

Finally, in example 3,

\documentclass{article}
\newcommand{\new}[1]{$\sigma$}
\begin{document}
\new{test}
\end{document}

\new is defined with 1 argument, as denoted by the [1] following the macro name in its \newcommand definition. Thus, in this instance, \new{test} does pass the group {test} as the argument to \new. In the definition of \new, however, nothing is done with the argument and so the word test, for all practical purposes, disappears from the input stream.

  • Why do you refer to OP as OP in 3rd person? :) – percusse May 1 '16 at 18:38
  • 2
    @percusse I use OP in the 3rd person because it is grammatically correct. However, if you mean why do I direct my answer to the forum about the OP, rather than direct my answer to the OP, I guess that's somehow related to my habit of reporting science in the passive voice (though the new craze of my editors is to report in 1st person, yuck). The fact that the question is 2 weeks old also contributes to the personal distance that lends itself to 3rd person. Just a quirk, I guess. – Steven B. Segletes May 1 '16 at 19:07
  • I actually think that 3rd person is just being pompous especially with single author articles. But that's my taste. – percusse May 1 '16 at 22:35
  • @percusse That the author of this answer is pompous has been a consensus reached by others, as well. – Steven B. Segletes May 2 '16 at 1:16
  • ahahah OK fair enough – percusse May 2 '16 at 9:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.