平均能量损失

平均能量损失

有关说明,请参见以下 MWE。

平均能量损失

\documentclass{article}
\usepackage{etoolbox}

\newcommand\Push[1]{\gappto\Array{#1}}


\newcommand\NewItem[2]{%
    \expandafter\newrobustcmd\csname#1\endcsname{#2}%
    \Push{#1}%
}

\newcommand\PrintOut{%
 % for each <x> in \Array, call \<x>
}

\NewItem{Books}{There are 3 books in stock.}
\NewItem{Pencils}{There are 2 pencils in stock.}

\begin{document}
\section{Manually printed}
\begin{enumerate}
    \item \Books
    \item \Pencils
\end{enumerate}

\section{Automatically printed by iterating \textbackslash Array}
\end{document}

问题

如何迭代列表\Array并为每个元素调用相应的宏?

答案1

expl3这是财产清单的完美候选。

\documentclass{article}
\usepackage{xparse}

\ExplSyntaxOn

\prop_new:N \l_yasashii_items_prop

\NewDocumentCommand \AddItem { m m }
 {
  \prop_put:Nnn \l_yasashii_items_prop { #1 } { #2 }
 }

\NewDocumentCommand \GetItem { m }
 {
  \prop_get:Nn \l_yasashii_items_prop { #1 }
 }

\NewDocumentCommand \PrintList { }
 {
  \begin{enumerate}
   \prop_map_inline:Nn \l_yasashii_items_prop
    { \item ##2 }% <- #1 is key, #2 is value
  \end{enumerate}
 }

\ExplSyntaxOff

\begin{document}
\AddItem{Books}{There are 3 books in stock.}
\AddItem{Pencils}{There are 2 pencils in stock.}
\GetItem{Books}
\PrintList
\end{document}

在此处输入图片描述

相关内容