9

I would like to go from a list of words through a dedicated command, for instance \listofwords{one word;one longer word;word and word} to the expanded list of the same words, ie:

\begin{list}{}
\item one word
\item one longer word
\item word and word
\end{list}

and the initial list could store more (or less) semicolon-separated words.

Edit The following may be more challenging: how to go from two different lists, \listofwords{word;wordd;worddd} and \listofterms{term;termm;termmm} to

\begin{list}{}{}
\item word term
\item wordd termm
\item worddd termmm
\end{list}

where worddd (or any other of the lists above) could store several space-separated words. Based on the previous answers, I had a look at the xparse and etoolbox packages but could no find any useful solution :(

4
  • Does it have to use semicolon as the delimiter, or would commas work as well?
    – Jake
    Sep 6, 2012 at 21:14
  • semicolon are preferred because commas could be part of the words. Is this a problem?
    – pluton
    Sep 6, 2012 at 21:20
  • @pluton and semicolons can't be part of the words? Sep 6, 2012 at 21:22
  • well, no they are not supposed to but \listofwords{one word}{one longer word}{word and word} would be acceptable as well.
    – pluton
    Sep 6, 2012 at 21:24

2 Answers 2

11

etoolbox provides list processing capabilities to suit this requirement:

enter image description here

\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}{;} creates a list parser \@listofwords that processes its separated by ;.

11

This is easy with 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}

The list environment is just to show that the output is identical. (Note that list takes two arguments.)

enter image description here

1
  • I am giving points to Werner so that you are not too far ahead :)
    – pluton
    Sep 7, 2012 at 7:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .