I read everywhere (e.g. https://en.wikibooks.org/wiki/TeX/let) that \let copies the content of a command into a new command.
My question is, why do we expect \let\comdwithtwoargs\@gobble makes \comdwithtwoargs have one argument?
\makeatletter
\newcommand{\cmd}[3]{%
#1 #2 #3
\let\cmd\@gobble
}
\makeatother
\cmd{abcdef} \cmd{abc}
In the example above, the code wouldn't run if I remove \let\cmd\@gobble. I don't understand the effect of \let and \@gobble and how they work.
\letting\cmdto\@gobble, the macro\cmdloses all of its original meaning (it is no longer a macro that takes two arguments or does anything), except to gobble the next argument. – Steven B. Segletes May 14 '20 at 17:34\cmd{a}{b}{c}. – Ulrike Fischer May 14 '20 at 17:35\cmd{arg1}, but it does run normally in the example. – Frank May 14 '20 at 17:47\cmdactually has three arguments the code in the example does not really do what one would naively expect. In\cmd{abcdef} \cmd{abc}the first\cmdreads three arguments: Its first argument isabdcdef, its second argument is\cmdand its third argument isabc. Then it expands to#1 #2 #3, so we getabcdef \cmd abc, so the second\cmdwhich at that point has not been redefined grabs its three arguments, which are (because there are no{and}s)a,bandcin that order and itself expands toa b c. So you end up withabdcdef a b c. ... – moewe May 14 '20 at 18:03{abcdef}\cmd{abc}and outputs them unchanged (but without the braces. then the second\cmdgets the argumentsabc ` and puts them back in the output. I don't think it should matter if the \let is there or not, but perhaps you didn't show everything. – Ulrike Fischer May 14 '20 at 18:05\cmdis\letto\@gobble. But this doesn't matter any more, since\cmdis not used after that. This is something wildly different from what one would expect if one were to call the macro with the correct number of arguments. If\cmdhad been defined as\newcommand{\cmd}[1]{#1 let\cmd\@gobble}so that it just takes one argument\cmd{abcdef} \cmd{abc}would have resulted in justabcdefin the output: The first call\cmd{abdcdef}would have expanded toabcdef \let\cmd\@gobbleand would have redefined\cmdto just eat its argument. Then\cmd{abc}produces no output – moewe May 14 '20 at 18:06\somethingas an argument. I see. Thank you @UlrikeFischer and @moewe – Frank May 14 '20 at 18:07