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 :)
\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}
% listheadings:
\def\listheadings{%
\expandafter\xlistheadings\listtobegenerated,\relax,%
\expandafter\xlistheadings\mylist,\relax,%
\hline}
% xlistheadings
\def\xlistheadings#1,{%
\ifx\relax#1%
\expandafter\\
\else
&\textbf{#1}%
\expandafter\xlistheadings
\fi}
\begin{document}
\begin{table}
\scalebox{0.7}{
\tablestart
\listheadings
\end{tabular}
} % end scalebox
\caption{Caption goes here.}
\begin{enumerate}
\item Item number one.
\item Item number two.
\end{enumerate}
\end{table}
\end{document}