36

The main loop for comma separated lists in LaTeX3 is \clist_map_inline:nn The first argument is an explicit list, the second argument tells LaTeX what to do with each item. For instance, we want to print an enumerate environment from the items: \documentclass{article} \usepackage{xparse} \ExplSyntaxOn \NewDocumentCommand{\makeenumerate}{ m } { \begin{...


22

My first LaTeX3 answer! Yay! :) The l3clist package has a lot of built-in commands to deal with comma-separated lists. Here's an attempt: \documentclass{article} \usepackage{expl3} \usepackage{xparse} \ExplSyntaxOn \NewDocumentCommand \countItems { m } { \clist_count:N #1 } \NewDocumentCommand \countInlineItems { m } { \clist_count:n {#1} } \...


21

If you need repeated access to arbitrary items then an "array" of command names \mylist1, \mylist2\ ... might be more suitable than a list. \documentclass{minimal} \usepackage{etoolbox} \newcounter{mylistcounter} \def\saveitem#1{% \stepcounter{mylistcounter}% \expandafter\def\csname mylist\themylistcounter\endcsname{#1}} \forcsvlist{\saveitem}{% ...


19

A loop and a counter: Code \documentclass{article} \makeatletter \newcommand*{\length}[1]{% \@tempcnta\z@ \@for\@tempa:=#1\do{\advance\@tempcnta\@ne}% The length of the list #1 is \the\@tempcnta.% } \makeatother \begin{document} \length{0,1,54,1,3} \def\mylist{0,1,54,1,3} \length\mylist \end{document} Output The length of the list 0,1,54,1,...


19

The following two macros need fixing: \newcommand*\@defineLine[2]{% \typeout{Defining wickerson@#1@pos = #2} \expandafter\xdef\csname wickerson@##1@pos\endcsname{#2} } That will define for \@defineLine{a}{10} the macro \wickerson@#1@pos instead of \wickerson@a@pos. Fix: \newcommand*\@defineLine[2]{% \typeout{Defining wickerson@#1@pos = #2} \...


19

Assuming that the list is contained in a macro, otherwise you need nothing to extract a first item that you know explicitly, you can do \usepackage{xparse} \ExplSyntaxOn \NewDocumentCommand{\extractfirst}{mm} { \tl_set:Nx #1 {\clist_item:Nn #2 { 1 } } } \ExplSyntaxOff Complete example: \documentclass{article} \usepackage{tikz} \usepackage{xparse} \...


18

This is similar to @egreg's solution and avoids \ifcase in favour of just cycling the list, but the coding is probably a bit simpler (unless you'be already loaded expl3 for other reasons) \documentclass{article} \usepackage{xcolor} \def\ColorList#1{\def\xcolorlist{#1}} \let\xsection\section \def\section{\expandafter\xxcycle\xcolorlist,\xcolorlist\xsection} \...


18

etoolbox's list processing capabilities are straight forward: \documentclass{article} \usepackage{etoolbox}% http://ctan.org/pkg/etoolbox \newcommand{\ppath}[2][$\;\triangleright\;$]{% \def\nextitem{\def\nextitem{#1}}% Separator \renewcommand*{\do}[1]{\nextitem\textsf{##1}}% How to process each item \docsvlist{#2}% Process list } \begin{document} A ...


17

Here's a version with xparse and LaTeX3 code, with the help of the random.tex file by D. Arsenau \documentclass{article} \usepackage{xparse} \input{random} \ExplSyntaxOn \NewDocumentCommand{\htguse}{ m } { \use:c { htg_arg_#1: } } \NewDocumentCommand{\selectNrandom}{ m m m } { \htg_select_n_random:nnn { #1 } { #2 } { #3 } } \cs_new_protected:Npn \...


17

PGFMath has an array parser which can be used for this. The syntax is {comma delimited list}[index]. You need the outer braces so to use it on \johnlist you'd need to add the braces in. As coordinates are passed through pgfmath then you can use this as-is in coordinate specifications, in other uses you need to use \pgfmathparse (or one of its variants). \...


15

There is a package lambda-lists by Alan Jeffrey for "Lists in TeX's mouth". Here's an example in plain TeX: \catcode`@=11 \input lambda.sty \catcode`@=12 \tracingmacros=1 % check out the log file afterwards! \def\mylist{\Listize[1, 2 ,3,,4=foo,5,]} \def\myfun#1{do something with #1\def\foo{}} \def\fooDefined?{\ifdefined\foo foo's defined\else foo's ...


15

Using egreg's template for straight LaTeX \documentclass{article} \makeatletter \newcommand{\makeenumerate}[2][,]{\begin{enumerate}% \def\dolist##1{\expandafter\@dolist##1#1\@eol}% \def\@dolist##1#1##2\@eol{% \begingroup\setbox0=\hbox{##1\unskip}\ifdim\wd0=0pt\endgroup\else\endgroup\item \fbox{\ignorespaces##1\unskip}\fi% \ifx\@eol##2\@eol\else\...


14

This will cycle over the listed colors: \documentclass[convert,border=2,varwidth]{standalone} \usepackage{xcolor,xpatch,xparse} \xpretocmd{\section}{\sectioncolor}{}{} \ExplSyntaxOn \NewDocumentCommand{\ColorList}{m} { \seq_gset_split:Nnn \g_gonzalo_colors_seq { , } { #1 } } \seq_new:N \g_gonzalo_colors_seq \NewDocumentCommand{\sectioncolor}{ } { \...


14

Since you are using etoolbox already, you can use \docsvlist and redefine \do appropriately. \documentclass{article} \usepackage{xcolor}% http://ctan.org/pkg/xcolor \usepackage{etoolbox}% http://ctan.org/pkg/etoolbox \newcounter{listitem} \newcommand\sectioncolor{% \setcounter{listitem}{-1}% At this point, section counter has not been incremented \...


14

The etoolbox package provides some list processing tools. For comma-separated lists there are \docsvlist{<list>} where each item is put in the input stream with \do{<item>}. This means a suitable definition of \do has to be provided. \forcsvlist{<handler>}{<list>} where each item is put in the input stream as argument to <handler&...


14

This is very easy with xparse: \documentclass{article} \usepackage{xparse} \NewDocumentCommand{\myfunc}{ >{\SplitList{,}} m }{% \ProcessList{#1}{\func}% } \NewDocumentCommand{\func}{m}{% \fbox{#1} % a space follows } \begin{document} \myfunc{a,b,c,x,y,z} \end{document} Note that \func can be defined in whatever way you like, also with \...


14

How about Lua? \def\sortlist#1{% \directlua{% local t = { #1 } table.sort(t) tex.sprint(table.concat(t,", ")) }% } \sortlist{"World", "Hello"} \bye Addendum: One of the really useful advantages of this approach is that it is fully expandable, meaning that after \edef\x{\sortlist{"World", "Hello"}} the macro \x will contain the sorted ...


13

The standard LaTeX \documentclass syntax doesn't use = at all, just a comma separated list of values like [12pt,twoside] so \documentclass[toc=listof, toc=bibliography, toc=flat]{article} would just be three (undefined) options toc=listof , toc=bibliography and toc=flat There is no mechanism for passing values to an option toc. Some classes load keyval ...


12

I believe that with expl3 it's more straightforward, since it doesn't rely on knowing what's needed to expand and when. \documentclass{article} \usepackage{xparse} \ExplSyntaxOn \seq_new:N \l_wickerson_list_seq \NewDocumentCommand{\printState}{>{\SplitList{,}}m} { \seq_clear:N \l_wickerson_list_seq \ProcessList{#1}{\splitatslash} Current~state\...


12

xstring solution \documentclass{article} \usepackage{xstring} \def\johnlist{2,3,5,7,11,13} \def\splicelist#1{ \StrCount{#1}{,}[\numofelem] \ifnum\numofelem>0\relax \StrBefore[1]{#1}{,}[\myhead]% \StrBehind[1]{#1}{,}[\mytail]% \StrBehind[\numofelem]{#1}{,}[\mylast]% \else \let\myhead#1% \let\mylast#1% \def\mytail{N/A} \fi } \...


12

Package kvsetkeys Package kvsetkeys provides parsers for key value lists and comma separated lists. Many formats are supported: LaTeX, plain TeX, it works even with iniTeX. Syntax: \comma@parse{⟨comma separated list⟩}{⟨code/processor with one argument⟩} Example The example is given for iniTeX to show the minimal requirements of the package. % Setup for ...


12

This seems to work, with no packages. EDITED to solve the upper/lower-case problem. EDIT: Resolved problem when a comparison ran out of letters prior to resolving the order, for example, wash, washer. See ADDENDUM for handling (after a fashion) diacritics. \documentclass{article} \def\listterminator{;} \makeatletter \newcommand\alphabubblesort[1]{\def\...


11

Here's a possible solution with xparse: \documentclass{article} \usepackage{xparse} \ExplSyntaxOn \NewDocumentCommand{\newgniourflist}{ m } { \seq_new:c { g_gniourf_#1_seq } } \newgniourflist{gniourflist} \NewDocumentCommand{\addtogniourflist}{ O{gniourflist} m } { \seq_gput_right:cn { g_gniourf_#1_seq } { #2 } } \NewExpandableDocumentCommand{\...


11

The following interface collects card backs in a list macro and then processes it once you have a collection of 9 or when you're \AtDocumentEnd: \documentclass{article} \usepackage[utf8]{inputenc} %Needed for input \usepackage{venturis} %CM does not look right at this size \usepackage[T1]{fontenc} %Needed for output \usepackage[margin=0.4cm]{geometry} %The ...


11

LaTeX has a built in command for iterating over comma separated lists: \documentclass{article} \makeatletter \newcommand\myfunc[1]{\@for\tmp:=#1\do{\func{\tmp}}} \let\func\fbox \begin{document} \myfunc{a,b,c,d} \end{document}


10

There are many ways of tackling this problem: which you choose depends on your particular requirements. Taking the example in the question, the reason for the apparent failure with \clist_map_inline:nn is that expl3 is very careful not to expand anything 'by accident'. Thus when the argument grabbed is a macro containing a comma-separated list, the code ...


10

There are probably a dozen standard methods for parsing comma-separated lists, but I don't know any of them, so here's my ad hoc solution (with a grain of salt :) \makeatletter \newcommand\@else@ifc{\noexpand\else} \newcommand\@or@ifc{\noexpand\or} \def\commalisttoifcase#1,% {% #1\@commalisttoifcase } \def\@commalisttoifcase#1,#2,% {% \ifx\empty#2\...


10

\documentclass{article} \newcommand{\commut}[2]{\left[{#1},{#2}\right]} \makeatletter \def\qcommut#1{\xcommut#1,\relax,} \def\xcommut#1,{\xxcommut{#1}} \def\xxcommut#1#2,{% \ifx\relax#2% #1% \expandafter\@gobbletwo \fi \xxcommut{\commut{#1}{#2}}} \begin{document} $\qcommut{1,2,3,4,5,6}$ \end{document}


10

You haven't defined \A to be 1 you've defined it to be \GetListMember{1,2,3}{1} (you get 1 when \A is expanded). TikZ has no idea what to do with \GetListMember when it encounters it in a coordinate. In this case you can use \pgfmathtruncatemacro. To see the difference consider the following (for clarity I've used \A and \B): \documentclass{article} \...


10

This is using the under-development tabstackengine package, first introduced here at Writing a table with equally spaced columns, based on the widest column (source code available at Measuring align). The package extends the stackengine package by adding tabbing capability. This answer, Can I tab inside of align environment?, gives some of the syntax of ...


Only top voted, non community-wiki answers of a minimum length are eligible