我想通过专用命令从单词列表转到\listofwords{one word;one longer word;word and word}
相同单词的扩展列表,即:
\begin{list}{}
\item one word
\item one longer word
\item word and word
\end{list}
并且初始列表可以存储更多(或更少)以分号分隔的单词。
编辑以下问题可能更具挑战性:如何从两个不同的列表中\listofwords{word;wordd;worddd}
选择\listofterms{term;termm;termmm}
\begin{list}{}{}
\item word term
\item wordd termm
\item worddd termmm
\end{list}
where worddd
(或上述任何其他列表)可以存储多个空格分隔的单词。根据之前的答案,我查看了xparse
和etoolbox
包,但找不到任何有用的解决方案 :(
答案1
etoolbox
提供列表处理功能以满足此要求:
\documentclass{article}
\usepackage{etoolbox}% http://ctan.org/pkg/etoolbox
\makeatletter
\newcommand{\listofwords}[1]{%
\renewcommand{\do}[1]{\item ##1}% Update how the list items will be processed
\begin{list}{}{}%
\@listofwords{#1}% Process items
\end{list}%
}
\DeclareListParser{\@listofwords}{;}% \@listofwords{<word>;<word>;...}
\makeatother
\begin{document}
\listofwords{one word;one longer word;word and word}
\begin{list}{}{}
\item one word
\item one longer word
\item word and word
\end{list}
\end{document}
\DeclareListParser{\@listofwords}{;}
创建一个列表解析器\@listofwords
,处理以 分隔的列表;
。
答案2
这很容易xparse
:
\documentclass{article}
\usepackage{xparse}
\NewDocumentCommand{\listofwords}{>{\SplitList{;}}m}
{\begin{list}{}{}
\ProcessList{#1}{\additem}%
\end{list}}
\NewDocumentCommand{\additem}{m}{\item #1}
\begin{document}
\listofwords{one word;one longer word;word and word}
\begin{list}{}{}
\item one word
\item one longer word
\item word and word
\end{list}
\end{document}
环境list
只是为了显示输出是相同的。(请注意,list
需要两个参数。)