ifmtarg testando uma macro vazia?

ifmtarg testando uma macro vazia?

O código a seguir funciona conforme o esperado:

\NewDocumentCommand{\checker}{m}{\@ifmtarg{#1}{empty}{not empty}}
\checker{}  % Prints "empty"
\checker{x} % Prints "not empty"

No entanto, como fazer funcionar quando uma macro potencialmente vazia é fornecida como argumento:

\newcommand{\emptymacro}{}
\NewDocumentCommand{\checker}{m}{\@ifmtarg{#1}{empty}{not empty}}
\checker{\emptymacro}  % Prints "not empty"

Como imprimi-lo emptymanipulando uma macro vazia como um argumento vazio?

Responder1

Você deseja usar expl3uma expansão recursiva.

\ExplSyntaxOn

\NewExpandableDocumentCommand{\checker}{mmm}
 {
  \tl_if_empty:eTF { #1 } { #2 } { #3 }
 }

\ExplSyntaxOff

Exemplo completo:

\documentclass{article}

\ExplSyntaxOn

\NewExpandableDocumentCommand{\checker}{mmm}
 {
  \tl_if_empty:eTF { #1 } { #2 } { #3 }
 }

\ExplSyntaxOff

\newcommand{\aaa}{\bbb}
\newcommand{\bbb}{\ccc}
\newcommand{\ccc}{}

\newcommand{\ddd}{\eee}
\newcommand{\eee}{e}

\begin{document}

\checker{}{empty}{not empty}

\checker{e}{empty}{not empty}

\checker{\aaa}{empty}{not empty}

\checker{\bbb}{empty}{not empty}

\checker{\ccc}{empty}{not empty}

\checker{\ddd}{empty}{not empty}

\checker{\eee}{empty}{not empty}

\end{document}

insira a descrição da imagem aqui

Se quiser considerar “vazio” também sequência de espaços, substitua por

\NewExpandableDocumentCommand{\checker}{mmm}
 {
  \tl_if_blank:eTF { #1 } { #2 } { #3 }
 }

Exemplo completo:

\documentclass{article}

\ExplSyntaxOn

\NewExpandableDocumentCommand{\checker}{mmm}
 {
  \tl_if_blank:eTF { #1 } { #2 } { #3 }
 }

\ExplSyntaxOff

\newcommand{\aaa}{\bbb}
\newcommand{\bbb}{\ccc}
\newcommand{\ccc}{}

\newcommand{\ddd}{\eee}
\newcommand{\eee}{e}

\newcommand{\fff}{\space\space}

\begin{document}

\checker{}{empty}{not empty}

\checker{e}{empty}{not empty}

\checker{\aaa}{empty}{not empty}

\checker{\bbb}{empty}{not empty}

\checker{\ccc}{empty}{not empty}

\checker{\ddd}{empty}{not empty}

\checker{\eee}{empty}{not empty}

\checker{\fff}{empty}{not empty}

\end{document}

insira a descrição da imagem aqui

informação relacionada