I have been looking for a way to create dynamically a comma-separated list consisting of n entries, all entries equal, defined by \newcommand\mytext{species:} (example) and n being the number of entries in \newcommand\mylist. \newcommand\listtobegenerated shows how it the defined list should look like. Is there some way to do it?

I found this: Delete an element from a comma delimited list , thinking that I might be able to modify it by changing the deletion to replacement, but the accepted answer's \count@ and \toks@ gave me errors, which I was not able to solve and thought that there must be some cleaner approach instead of using \count@ and \toks@. Other answers to that question were for ConText and for expl3, and I'm looking for a LaTeX answer. So, how could I generate the content of listtobegenerated based on the content of mytext and the number of elements in mylist?

Thank you :)


\newcommand\mylist{Haliaeetus albicilla,Lagidium viscacia,Balaenoptera musculus,Panthera pardus orientalis,Human}

% this is the the list I attempt to generated based on the number of elements in mylist and the content of mytext:

% preamble:

% tablestart:

% listheadings:

% xlistheadings

} % end scalebox
\caption{Caption goes here.}
    \item Item number one. 
    \item Item number two.
  • expl3 is a LaTeX-answer. – Marco Daniel May 31 '12 at 23:20
  • etoolbox comes to mind... – cgnieder May 31 '12 at 23:27

It seems that you want to build a tabular environment, so it's not so efficient to go through many useless steps:



\NewDocumentCommand{\generatetable}{mm}{ \nrz_generate_table:nn { #1 } { #2 } }

\cs_new:Npn \nrz_generate_table:nn #1 #2 
  \tl_clear:N \l_nrz_preamble_tl
  \tl_set:Nn \l_nrz_preamble_tl { c@{} }
  \tl_clear:N \l_nrz_header_one_tl
  \tl_clear:N \l_nrz_header_two_tl
  \clist_map_inline:Nn #1
    \tl_put_right:Nn \l_nrz_preamble_tl { c }
    \tl_put_right:Nn \l_nrz_header_one_tl { & }
    \tl_put_right:NV \l_nrz_header_one_tl #2
    \tl_put_right:Nn \l_nrz_header_two_tl { & }
    \tl_put_right:Nn \l_nrz_header_two_tl { ##1 }
  \tl_put_right:Nn \l_nrz_header_one_tl { \\ }
  \tl_put_right:Nn \l_nrz_header_two_tl { \\ }
\cs_new:Npn \nrz_build_table:
  \tl_use:N \l_nrz_header_one_tl
  \tl_use:N \l_nrz_header_two_tl
\tl_new:N \l_nrz_preamble_tl
\tl_new:N \l_nrz_header_one_tl
\tl_new:N \l_nrz_header_two_tl

\newcommand\mylist{Haliaeetus albicilla,Lagidium viscacia,Balaenoptera musculus,Panthera pardus orientalis,Human}


It wouldn't be difficult to do the same with etoolbox facilities. But I believe that this is easier to understand.

In case you really want to build the comma separated list, you can do with



\newcommand\mylist{Haliaeetus albicilla,Lagidium viscacia,Balaenoptera musculus,Panthera pardus orientalis,Human}


will define \myheader to expand to

| improve this answer | |
  • you don't need quite so many expandafter as \@for does one level of expansion itself so you can give it the list as a macro (but I agree if you don't need to build the list at all that's better:-) – David Carlisle May 31 '12 at 23:59
  • @DavidCarlisle Thanks; the \@xp for \@for are too much indeed. But the following are to get rid of the first comma. – egreg Jun 1 '12 at 0:04
  • oops there are no commas at all in my answer:-) I think I'll just delete mine rather than fixing – David Carlisle Jun 1 '12 at 0:06
  • @egreg Thank you, it works. I use the comma-separated list generation solution because for me it's easier to incorporate it in my code, which is a little modified version of David Carlisle's answer to tex.stackexchange.com/questions/52472/… . – nrz Jun 1 '12 at 6:54
  • @egreg I am trying to build something based on your comma separated list solution, but I don't quite understand how it's supposed to be applied. Shouldn't \myheader expand to species: Haliaeetus albicilla, species: Lagidium viscacia, ... ? – Jörg Apr 14 '14 at 12:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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