add tag
निरंजन
I was experimenting with these two commands and I realized that there are cases where they aren't identical.

```
\documentclass{article}
\NewDocumentCommand{ \foocmd }{  }{%
  \NewDocumentCommand{ \barcmd }{ >{\SplitList{,}} m }{%
    \ProcessList{##1}{\textbf}%
  }%
}
\NewDocumentEnvironment{ fooenv }{  }{%
  \NewDocumentEnvironment{ barenv }{ >{\SplitList{,}} m }{%
    \ProcessList{##1}{\textbf}%
  }%
}

\begin{document}
\foocmd{Hello world\barcmd{a,b,c,d}}

\begin{fooenv}%
  Hello world%
  \begin{barenv}{%
      a,b,c,d%
    }%
  \end{barenv}%
\end{fooenv}
\end{document}
```

I expect the output of both the commands to be same which it isn't.

![Screenshot_2022-06-08_21-27-25.png](/image?hash=09b9585cfc42450cbb2122d3c908b35c8823aefd385c1c7b4501deb36d15c377)

This is rather strange. Is this the expected behavior or a bug?
Top Answer
Skillmon
The issue is that `\NewDocumentEnvironment` has an argument more than `\NewDocumentCommand`, that argument defines what should be done in the `\end` part of said environment. So the syntax is:

```
\NewDocumentEnvironment {<name>} {<args>}
  {<begin>}
  {<end>}
```

(this is analogue to `\newenvironment`, that also allows you to define the action in `\begin` and `\end`).

Now when you call your environment `fooenv` it'll do the `\NewDocumentEnvironment{barenv}` definition, and that one will grab an argument more than you provide (in your example that argument is `H`). Now the environments do what you tell them to, and on `\end{barenv}` the `H` is reinserted (as that's the definition for the `\end`-part now).

Please note that I'd say using `\NewDocumentEnvironment` to define a nested environment seems wrong. What I'd do is define it globally, but also define a toggle that'll throw an error if you don't use it nested  in `fooenv`.

Consider the following example:

```
\documentclass{article}

\makeatletter
\NewDocumentEnvironment{ fooenv }{  }{%
  \let\barenv@\@empty
}{}
\newcommand*\barenv@
  {\GenericError{}{niranjan}{Misplaced environment barenv}{}}
\NewDocumentEnvironment{ barenv }{ >{\SplitList{,}} m }{%
  \barenv@
  \ProcessList{#1}{\textbf}%
}{}
\makeatother

\begin{document}
\begin{fooenv}%
  Hello world%
  \begin{barenv}{%
      a,b,c,d%
    }%
  \end{barenv}%
\end{fooenv}
\end{document}
```

Enter question or answer id or url (and optionally further answer ids/urls from the same question) from

Separate each id/url with a space. No need to list your own answers; they will be imported automatically.