In this answer egreg mentions that the r
argument type from the xparse
package handles nested delimited arguments, as shown in this small example:
\documentclass{article}
\usepackage{xparse}
\NewDocumentCommand\foo{r()}{(#1)}
\begin{document}
\[ \foo(x\cdot\foo(y+\foo(z))\cdot w) \]
\end{document}
The package documentation doesn't explain how this is implemented, though. I tried looking at the code but it's really long and barely readable for someone who's not familar with LaTeX3.
As discussed in the question linked above, TeX's default delimited parameters can't be used for this. The only way I can think of is parsing through the whole following text via \futurelet
, thereby somehow taking care that macros are expanded properly, until the final closing delimiter is found. Can anyone explain how xparse
actually implements this?
align
ortabularx
etc which need to grab the environment body while matching begin/end, there you use a delimited argument for\end
but check whether what you grabbed contains a nested\begin
and if it does, iterate.