1

Consider the following MCE:

\documentclass{article}
\newcommand{\mynewcommand}{foo}
\NewDocumentCommand{\MyNewDocumentCommand}{}{bar}
\ExplSyntaxOn
\iow_term:x {mynewcommand=\mynewcommand}
\iow_term:x {MyNewDocumentCommand=\MyNewDocumentCommand}
\ExplSyntaxOff
\begin{document}
\begin{itemize}
\item \verb|\mynewcommand|=\mynewcommand
\item \verb|\MyNewDocumentCommand|=\MyNewDocumentCommand
\end{itemize}
\end{document}

When it is compiled, what it is displayed:

  • in the resulting PDF is expected:

      • \mynewcommand=foo
      • \MyNewDocumentCommand=bar
    
  • in the terminal is expected for \mynewcommand but is unexpected for \MyNewDocumentCommand:

      mynewcommand=foo
      MyNewDocumentCommand=\MyNewDocumentCommand
    

Why, in the terminal, the ⟨code⟩ of \NewDocumentCommand{⟨cmd⟩}{⟨arg spec⟩}{⟨code⟩} isn't displayed, whereas the ⟨code⟩ of \newcommand{⟨cmd⟩}[⟨num⟩][⟨default⟩]{⟨code⟩} is displayed?

Denis Bitouzé
  • 9,652
  • 4
  • 27
  • 85

2 Answers2

4

If you use \newcommand\macro then the \macro is defined using TeX primitives \long\def\macro. The macro is expanded inside \edef and inside \write and \message primitives, i.e. when you write an information into the terminal or log file.

On the other hand, if you use \NewDocumentCommand\macro then the macro is defined by TeX primitives \protected\def\macro (and the macro body is more complicated in this case). When the \macro is defined as \protected then it is expanded in typesetting context but it isn't expanded in \edef, \write, \message.

wipet
  • 74,238
1

I'm not sure why you'd expect that output. For instance

\iow_term:x { LaTeX=\LaTeX }

will stop with a weird error

! Undefined control sequence.
\S@10 ->\gdef \tf@size
                       {10}\gdef \sf@size {7}\gdef \ssf@size {5}

If you want to use \NewDocumentCommand to define a container for some text (but you shouldn't), then do

\NewExpandableDocumentCommand{\MyNewDocumentCommand}{}{bar}

and now

\iow_term:x {MyNewDocumentCommand=\MyNewDocumentCommand}

will indeed produce

MyNewDocumentCommand=bar

I'm not sure what would this be useful for.

egreg
  • 1,121,712