Tag Info

Hot answers tagged

20

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 } { ...


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

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


18

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


18

I have not progressed to the level of processing a list myself but I have been using etoolbox by Philipp Lehman for some other TeX programming tasks I am doing. I believe 3.7 List processing should give you a good solution. For example (on a recent version of etoolbox) you can write: \DeclareListParser*{\myfor}{;} \myfor{\fbox}{item1; item2; item3}


18

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


16

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

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


15

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


15

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}{% ...


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


12

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}{ } { ...


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


12

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


11

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


11

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


11

I was hoping someone to post a better/nicer solution, but so far this seems to be the best one I've seen. \def\do@scan#1:{% \ifx#1\relax \let\next\relax \else \DoSomethingWith{#1}\let\next\do@scan \fi\next } \newcommand{\scanlist}[1]{\do@scan#1:\relax:} Of course instead of : one can use , or any other sensible separator.


10

Here a solution that uses the etoolbox package: \usepackage{etoolbox} \newcommand*{\newacronym}[1]{\typeout{New acronym: [#1]}} \newcommand*{\newacronyms}{% \let\do\newacronym \docsvlist } \newacronyms{acm, ams, cpu, nih}


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,% {% ...


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

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


10

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 } } \NewDocumentCommand{\getnthelement}{ ...


10

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


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


9

You can first replace the space with a special marker, say -\q_tobi_space_marker- so you can split at hyphens and have a way to check whether the item was a space. Here's an implementation. Plan of attack: We change spaces in the way outlined before The two lists are split into sequences The "syllables" sequence is mapped item by item, stepping a counter ...


9

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


9

In simple cases, you can use LaTeX's \@removeelement: \documentclass{article} \newcommand*{\citylist}{derby,leicester,nottingham} \makeatletter \@expandtwoargs\@removeelement{leicester}\citylist\citylist \makeatother \typeout{\citylist} \begin{document} \end{document} Result: derby,nottingham Simple means, the list is normalized without optional ...


9

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


8

The comments what color we have is only for demonstration here. \documentclass{article} \usepackage[T1]{fontenc} \makeatletter \def\setmycolour#1{\expandafter\setmycolour@i#1,,,,\@nil} \def\setmycolour@i#1,#2,#3,#4,#5\@nil{% \ifx$#2$ we have gray => #1 \else \ifx$#3$ we have a wrong color setting \else \ifx $#4$ we have a rgb setting ...


8

This is a classic expansion issue. What happens is that \testtag{\test@refnames} passes \test@refnames exactly as written. In writing to the log, this is fully-expanded by TeX, so you see what you expect. However \docsvlist{#1}% sees \test@refnames as #1, with no commas. So you need to expand \test@refnames correctly. Depending on other uses, either to ...



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