Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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

    \def\ListOfColumnsToOmit{A,C}
    \DTLdisplaydb[\ListOfColumnsToOmit]{myDB}
    

    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

References:

Code:

\documentclass{article}
\usepackage{xstring}
\usepackage{datatool}

% Make csv for question
\usepackage{filecontents}
\begin{filecontents*}{MyData.csv}
A, B, C, D, E
0, 1, 0, 2, 3
0, 0, 0, 4, 4
0, 2, 0, 0, 5
\end{filecontents*}

\makeatletter
% http://tex.stackexchange.com/questions/83595/expansion-issue-when-adding-to-csv-list-from-within-a-foreach/
\newcommand*{\AddToCommaSeparateList}[3][,\space]{%
    % #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
    \begingroup\edef\x{\endgroup%
      \noexpand\g@addto@macro\noexpand#2{{#3}}}\x%
}%
\makeatother


\begin{document}
\DTLloaddb{myDB}{MyData.csv}

With \emph{no} omit list provided:\par
\noindent\DTLdisplaydb{myDB}%

With manually specified omit list:\par
\noindent\DTLdisplaydb[A,C]{myDB}%

\def\ListOfColumnsToOmit{A,C}
With fixed macro  omit list:\par
\noindent\DTLdisplaydb[\ListOfColumnsToOmit]{myDB}%

\def\ListOfColumnsToOmit{}
\AddToCommaSeparateList{\ListOfColumnsToOmit}{A}%
\AddToCommaSeparateList{\ListOfColumnsToOmit}{C}%
With generated macro omit list:\par
\noindent\DTLdisplaydb[\ListOfColumnsToOmit]{myDB}%

The omit list was: \ListOfColumnsToOmit
\end{document} 
share|improve this question
    
Wow, 22 days without an expansion related question from me. Must be a new record. :-) –  Peter Grill Dec 22 '12 at 0:38
add comment

1 Answer

up vote 6 down vote accepted

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

\newcommand*{\AddToCommaSeparateList}[3][,]{%
    % #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
    %
    \begingroup\edef\x{\endgroup
      \noexpand\g@addto@macro\noexpand#2{#3}}\x
}

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

share|improve this answer
    
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. –  Peter Grill Dec 22 '12 at 2:36
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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