Tag Info

Hot answers tagged

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

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


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

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


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


14

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


13

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


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

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.


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


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

\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

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


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


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

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


8

As Qrrbrbirlbel says in a comment, it's an expansion issue; \getstr requires three expansion steps, not just one. \documentclass{article} \usepackage{pgffor} \newcommand{\setstr}[2]{\expandafter\def\csname#1\endcsname{#2}} \newcommand{\getstr}[1]{\csname#1\endcsname} \newcommand{\PROCESS}[1]{% The list items are: \foreach \i in {#1} {% (\i) }% } ...


8

Here is an etoolbox approach which provides \listhead and \listtail: \documentclass{article} \usepackage{etoolbox}% http://ctan.org/pkg/etoolbox \newcommand{\listhead}[1]{% \renewcommand*{\do}[1]{##1\renewcommand*{\do}[1]{}}% Manage each item and print head \expandafter\docsvlist\expandafter{#1}% Process list } \newcommand{\listtail}[1]{% ...


8

You can work with siunitx. Therefore I defined a command: \usepackage{siunitx} \ExplSyntaxOn \DeclareExpandableDocumentCommand{ \parsenumber } { m } { \num[ output-decimal-marker = { , }]{\fp_eval:n { abs ( #1 ) }} } \ExplSyntaxOff Your loop looks like: \foreach \x in {-1,-0.75,...,1} \node[fill=rouge!10,inner ...


8

In the spirit of John Wickerson's own answer (no extra packages), but with tail extraction support. The macro \spliceList{<csv list>} defines macros \head and \tail: \documentclass{article} % \spliceList{1,2,3} --> \def\head{1} \def\tail{3,4} % \spliceList{7} --> \def\head{1} \def\tail{} \def\spliceList#1{\expandafter\spliceListAux #1,\END} ...


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


8

In LaTeX3, the preferred way to get user-level functions is xparse and its \NewDocumentCommand function. If the list was not a comma-separated list, then you would have to do something like \ExplSyntaxOn \NewDocumentCommand{\PrintAnswerList}{>{\SplitList;}m} { \tl_map_inline:nn {#1} { \PrintAnswer {##1.ans} } } \ExplSyntaxOff \PrintAnswerList { file01 ...


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



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