3

I tried to find an answer to this but currently I have no idea how to get it to work.

I want to create a new command which fills some lines of a tabular based on a given list with variable length like

\makeTabularRows{item1, item2, item3}

The output of this should be

\multicolumn{3}{c}{item1} & \\
\multicolumn{3}{c}{item2} & \\
\multicolumn{3}{c}{item3} &

So that it fits in a tabular like

\begin{tabular}{lclr}
    \makeTabularRows{item1, item2, item3}
\end{tabular}

I found a lot of partial solutions for example this short one

\newcommand{\makeTabularRows}[1]{%
    \def\nextitem{\def\nextitem{.}}%  
    \@for \el:=#1\do{\nextitem\el}%
}

to insert dots between the elements and other solutions looking more complex for example to create an enumeration. I found out that one problem is to include the & sign and my tests showed me that even inserting the \\ seems to be not possible. I am lost to find a solution for the tabular if there is one.

Any ideas how this can be done?

  • Can you give some more context? Why the \multicolumn? – egreg Feb 24 '16 at 17:56
  • as soon as TeX will "see" a & all definitions will be lost (for the next cell). You either need a purely expandable loop or some other possibly non expandable but cunning macro. There are expandable loops in the LaTeX kernel. (and you can do what you want with non expandable \xintFor macro from package xinttools). – user4686 Feb 24 '16 at 17:58
  • @egreg The multicolumn is used for the whole layout. The question here is just a part of the whole table. – baghira Feb 26 '16 at 13:51
  • @jfbu I tried a loop with xinttools, but I get an error, that the parameters of my command are wrong. I think it is because of that I had to use #2 or something. Maybe I get something wrong how and in which context to use this. Maybe you can write a full answer with a working code example in the context I need it (as a usable command with one parameter). Thank you! – baghira Feb 26 '16 at 14:07
  • @baghira Perhaps you need to double the # but anyhow I don't think it could work (now that I have read your question for real ;-) ). See answer. – user4686 Feb 26 '16 at 22:48
2

Here is an approach with an expandable loop. For some reason most xinttools facilities are for inputs of the {item1}{item2}{item3} type ; if one prefers item1, item2, item3 one needs to apply \xintCSVtoList as here in the \makeRows command.

I mentioned \xintFor in a comment but I had not read closely enough the question (sorry) ... \multicolumn is a beast which really wants to be first in its cell (after expansion). Macro \xintFor does some (non global) assignments, thus it is not the appropriate tool here.

\documentclass{article}

\usepackage{xinttools}

\newcommand\makeOneRow [1]{\multicolumn{3}{c}{#1} & \\ }
\newcommand\makeRows [1]{%
    \xintApplyUnbraced {\makeOneRow}{\xintCSVtoList{#1}}%
}%

\begin{document}

\begin{tabular}{lclr}
    \makeRows{item1, item2, item3}
\end{tabular}

\end{document}

enter image description here

If you had wanted the \multicolumn in the second cell it would have worked :

 \xintFor #1 in {item1, item2, item3}\do
  {& \multicolumn{3}{c}{#1}\\}

and if you put this in a \newcommand\makeTabularRows where #1 will stand for the csv of items, you need to use \xintFor ##1 in {#1} \do {...}.

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.