In my DB the number of columns is rather large. Hence, I am trying to eliminate any columns which contain only 0 entries.

The number of columns, nor the column keys, are know upfront. So, I step through the DB and generate a list of columns that are to be omitted. When I do that the omit list provided seems to be ignored. Below I have omitted the code that determines which columns are

There are no problems for the case of:

  • Manually specified omit list: So \DTLdisplaydb[A,C]{myDB} works fine.

  • Omit list specified by a macro that has fixed content: So


    also works fine.

Only for the case where I compute the list of columns to omit and add them to \ListOfColumnsToOmit via the \AddToCommaSeparateList macro do things break.

So, the correct result would be that the last table was identical to the two middle tables.

enter image description here




% Make csv for question
A, B, C, D, E
0, 1, 0, 2, 3
0, 0, 0, 4, 4
0, 2, 0, 0, 5

% https://tex.stackexchange.com/questions/83595/expansion-issue-when-adding-to-csv-list-from-within-a-foreach/
    % #1 = separator
    % #2 = listname
    % #3 = content to add to CSV list
    \IfStrEq{#2}{}{}{\g@addto@macro#2{#1}}% Don't add a leading comma in list
    % Extra brace group in case #3 contains a comma


With \emph{no} omit list provided:\par

With manually specified omit list:\par

With fixed macro  omit list:\par

With generated macro omit list:\par

The omit list was: \ListOfColumnsToOmit
  • Wow, 22 days without an expansion related question from me. Must be a new record. :-) Commented Dec 22, 2012 at 0:38

1 Answer 1


The expansion of \ListOfColumnsToOmit in the last case is

{A},\space {C}

which is obviously wrong. Even omitting the \space, which serves no purpose, the additional braces don't allow datatool to recognize the column names.

If I use

    % #1 = separator
    % #2 = listname
    % #3 = content to add to CSV list
    \IfStrEq{#2}{}{}{\g@addto@macro#2{#1}}% Don't add a leading comma in list

then the result is the same as in the "manual" examples.

  • Hmmm.. Well it is obviously wrong only in this case - has been working great when I want to typeset the list.. So I guess I'll define in starred variant which does not add the \space, and the extra brace group. Commented Dec 22, 2012 at 2:36

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .