2

I almost confranted with the same question as this link:

I get to know that:

  • \newcommand couldn't be expanded with \edef
  • \NewExpandableDocumentCommand could be used with \edef

But in my following case, it seemed that it also related with the optional arguments?

\documentclass[border=6pt,tikz]{standalone} \usetikzlibrary{matrix} \NewExpandableDocumentCommand\testB{m}{\node {#1};} \newcommand{\testC}[1]{\node {#1};} \NewExpandableDocumentCommand\testBB{O{orange} m}{\node[draw=#1] {#2};} \newcommand{\testCC}[2][magenta]{\node[draw=#1] {#2};} \begin{document} \begin{tikzpicture} \matrix (m) [matrix of nodes] { TEST & REASON \\ \testB{I'm test B} & works with Expandable without optional argument\\ \testBB{I'm test BB} & works with Expandable with optional argument\\ \testC{I'm test C} & work with newcommand without optional argument\\ % \testCC{testCC} why? \testC is okay but \testCC failed? \texttt{testCC failed} & failed\\ }; \end{tikzpicture} \tikz{\testB{B}} \tikz{\testC{C}} \tikz{\testBB{BB}} \tikz{\testCC{CC}} \end{document} 

which yields:

Undefined control sequence. \\testCC [#1]#2->\node 

the result

My Question:

Is there the exists of optional argument that differed between \newcommand and \NewExpandableDocumentCommand? Why? \testC is okay but \testCC failed in the above case?


Edit Confustion in comment:

Thanks to your paraphase, but if in my case if want to give the color to be optional assigned inside the \matrix, I think can't use \NewExpandandableDocumentCommand,because with the unexpandable \node inside, this will make the NewExpandableDocumentCommand NOT fully expandable.

Meanwhile the \newcommand*{<cmd>}[2][cyan]{<code>} doesn't track expandablity(failed with testCC), how can I make my cmd \node to handle the color argument properly?

2
  • 1
    \testCC fails because of the PGF/TikZ-matrix. If you use \path;\testCC{…} it works. It doesn't detect that your cell actually starts with \node and assumes the cell to be just text. So it does \node{\testCC{testCC} why?}; which is a \node command inside a node's text. And since \node is not defined outside of TikZ (and thus inside of nodes), you get an error message. What you can do is \newcomand*\testCCaux{\path;\testCC}\newcommand\testCC[2][magenta]{…}. Commented Feb 22 at 17:12
  • It's another extraordinary solution and the reason behind, and which prevent from defining the unexpandable node inside Expandable cmd. Commented Feb 22 at 17:22

1 Answer 1

4

The implementation of \newcommand doesn't track expandablity: all optional arguments are grabbed in a non-expandable way. This is the same as for \NewDocumentCommand. A different code path is used by \NewExpandandableDocumentCommand for optional arguments: as documented in usrguide, this has some restrictions which is why it's not the first choice.

Note that you are using a non-expandable command (\node) inside your definitions, so you can never have a fully-expandable situation here.

6
  • 2
    @Explorer I don't see why you can't use \NewExpandandableDocumentCommand - it does what you want Commented Feb 22 at 8:47
  • maybe I didn't fully understand that you mentioned "you are using a non-expandable command (\node) inside your definitions, so you can never have a fully-expandable situation here", as I use \node inside \NewExpandandableDocumentCommand, so I said "I can't use \NewExpandandableDocumentCommand`"... Commented Feb 22 at 11:58
  • 2
    @Explorer Most of the time people want full expansion of everything - your case is unusual as pgf expands up to things it 'knows' - in your case \node Commented Feb 22 at 15:47
  • That is to said, although NewExpandableDocumentCommand may cause problem with optional arguments, but in my case with pgf's unusual parse, it's okay to make unexpandable \node inside NewExpandableDocumentCommand,right? BTW, is there any related link introduce more about the unusual pgf parser?Thanks for yr kindness! Commented Feb 22 at 16:30
  • 1
    @Explorer You will be fine - there are no docs for the pgf parser: I've read over the code for debugging purposes Commented Feb 22 at 16:54

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.