Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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 :(

share|improve this question
    
Does it have to use semicolon as the delimiter, or would commas work as well? –  Jake Sep 6 '12 at 21:14
    
semicolon are preferred because commas could be part of the words. Is this a problem? –  pluton Sep 6 '12 at 21:20
    
@pluton and semicolons can't be part of the words? –  Gonzalo Medina Sep 6 '12 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 '12 at 21:24
add comment

2 Answers

up vote 11 down vote accepted

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 ;.

share|improve this answer
add comment

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

share|improve this answer
    
I am giving points to Werner so that you are not too far ahead :) –  pluton Sep 7 '12 at 7:59
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.