How to create a comma-separated list in LaTeX according to the number of elements in another list?


I found this: Comma delimited lists , 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 :)

\documentclass[12pt]{article}
\usepackage[american]{babel}
\usepackage{graphicx}

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

% this is the the list I attempt to generated based on the number of elements in mylist and the content of mytext:
\newcommand\listtobegenerated{species:,species:,species:,species:,species:}

% preamble:
\def\preamble{\expandafter\xpreamble\mylist,\relax,}
\def\xpreamble#1,{%
\ifx\relax#1%
\else
c%
\expandafter\xpreamble
\fi}

% tablestart:
\def\tablestart{%
\edef\temp{\noexpand\begin{tabular}{c\preamble}\noexpand\hline}%
\temp}

\hline}

\ifx\relax#1%
\expandafter\\
\else
&\textbf{#1}%
\fi}

\begin{document}
\begin{table}
\scalebox{0.7}{
\tablestart
\end{tabular}
} % end scalebox
\caption{Caption goes here.}
\begin{enumerate}
\item Item number one.
\item Item number two.
\end{enumerate}
\end{table}
\end{document}

-
expl3 is a LaTeX-answer. –  Marco Daniel May 31 '12 at 23:20
etoolbox comes to mind... –  clemens 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:

\documentclass{article}
\usepackage{xparse,graphicx}

\ExplSyntaxOn

\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@{} }
\clist_map_inline:Nn #1
{
\tl_put_right:Nn \l_nrz_preamble_tl { c }
}
\nrz_build_table:
}
\cs_new:Npn \nrz_build_table:
{
\resizebox{\textwidth}{!}{
\begin{tabular}{\l_nrz_preamble_tl}
\end{tabular}
}
}
\tl_new:N \l_nrz_preamble_tl
\ExplSyntaxOff

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

\begin{document}
\noindent\generatetable{\mylist}{\mytext}
\end{document}


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

\makeatletter
\newcommand\generatefromwith[3]{%
\gdef#1{\@gobble}%
\@for\next:=#2\do
\expandafter\expandafter\expandafter\gdef\expandafter\expandafter\expandafter#1\expandafter\expandafter\expandafter{#1}
}
\makeatother


Then

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


will define \myheader to expand to
species:,species:,species:,species:,species:

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