0

Awkward it is! I can not find the basic definition of \newcommand, though it is used a million times.

I suspect it needs improvement.

  • 2
    Could you elaborate on the 'needs improvement' part?I wonder what you are really after here! – Joseph Wright May 4 at 22:54
  • 2
    there are many alternative macro definition commands, notably those of the xparse package. Changing \newcommand would most likely break most existing latex code so it would be hard to consider any improvements. – David Carlisle May 4 at 22:56
  • @Joseph Wright @ David Carlisle First of all I would like to see the genuine definition of \newcommand – Matthias Borck-Elsner May 4 at 22:56
  • 1
    I put the location in the answer below, but you can always use \show\newcommand to see the definition. – David Carlisle May 4 at 22:56
2

Line 838 of latex.ltx is the start of the definition.

\def\@star@or@long#1{%
  \@ifstar
   {\let\l@ngrel@x\relax#1}%
   {\let\l@ngrel@x\long#1}}

\let\l@ngrel@x\relax
\def\newcommand{\@star@or@long\new@command}
\def\new@command#1{%
  \@testopt{\@newcommand#1}0}
\def\@newcommand#1[#2]{%
  \kernel@ifnextchar [{\@xargdef#1[#2]}%
                {\@argdef#1[#2]}}
\long\def\@argdef#1[#2]#3{%
   \@ifdefinable #1{\@yargdef#1\@ne{#2}{#3}}}
\long\def\@xargdef#1[#2][#3]#4{%
  \@ifdefinable#1{%
     \expandafter\def\expandafter#1\expandafter{%
          \expandafter
          \@protected@testopt
          \expandafter
          #1%
          \csname\string#1\endcsname
          {#3}}%
       \expandafter\@yargdef
          \csname\string#1\endcsname
           \tw@
           {#2}%
           {#4}}}
\long\def\@testopt#1#2{%
  \kernel@ifnextchar[{#1}{#1[{#2}]}}
\def\@protected@testopt#1{%
  \ifx\protect\@typeset@protect
    \expandafter\@testopt
  \else
    \@x@protect#1%
  \fi}
\long \def \@yargdef #1#2#3{%
  \ifx#2\tw@
    \def\reserved@b##11{[####1]}%
  \else
    \let\reserved@b\@gobble
  \fi
  \expandafter
    \@yargd@f \expandafter{\number #3}#1%
}
\long \def \@yargd@f#1#2{%
  \def \reserved@a ##1#1##2##{%
    \expandafter\def\expandafter#2\reserved@b ##1#1%
    }%
  \l@ngrel@x \reserved@a 0##1##2##3##4##5##6##7##8##9###1%
}
\long\def\@reargdef#1[#2]{%
  \@yargdef#1\@ne{#2}}

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.