如何檢查某物的雙重展開是否為空?

如何檢查某物的雙重展開是否為空?

我正在使用 listofitems 包,它會產生一個列表,其元素可以通過

\LIST[n]

但在手冊中我讀到我需要雙重擴充。

特別是我如何檢查(的雙重展開)\LIST[n]是否為空?

答案1

您可以透過使用將事物擴展兩次\expandafter\expandafter\expandafter(第一個\expandafter步驟在第二個步驟上,第三個在一次後擴展參數,第一個和第三個被刪除,第二個在第二次{後擴展參數)。{如果您需要擴展巨集的參數,則還需要\expandafter在其前面放置三個(這對於 來說不是必需的,因為\detokenize它會擴展所有內容,直到找到左大括號;對於 來說這也不是必需的\if,因為它展開所有內容,直到找到兩個不可展開的標記)。

\documentclass{article}

\usepackage{listofitems}
\readlist\foo{12,abc,3,4,,}

\makeatletter
\providecommand\@secondofthree[3]{#2}
\newcommand\iflistitememptyTF[2]
  {%
    \if\relax\detokenize\expandafter\expandafter\expandafter{#1[#2]}\relax
      \expandafter\@secondofthree
    \fi
    \@secondoftwo
  }
\makeatother

\begin{document}
\iflistitememptyTF\foo{5}{empty}{not empty}
\end{document}

相關內容