3

I try to automate generating same tables. With a \newcommand a comma separated list of filenames is defined. I want to iterate over these files, import them and concat them to a single table. Sounds easy, but I fail.

Reading the manual of pgfplotstable, it offers 3 approaches.

  • \foreach : is grouped, such that the output defined within the loop is not accessible from the outside.

  • \pgfplotsforeachungrouped : seems especially designed for that task. It is ungrouped and the example in the manual nearly works for me. But is only works if I explicit define the list. Not if I declare the list elsewhere with \newcommand.

    The problem: It does not iterate over the entries. Just one iteration with \filename being the whole string of all filenames.

  • \pgfplotsinvokeforeach : same as \pgfplotsforeachungrouped, just slightly changed syntax.

I can think of two approaches, but can't solve them.

  • Use \pgfplotsforeachungrouped with working external list
  • use \foreach and somehow write the output to be globally accessible

What can/should i do?


Here is my current test environment.

\newcommand*\fileList{List1.csv,List2.csv}

\documentclass[parskip=half]{scrartcl}

\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}

\usepackage{pgfplotstable}
    \pgfplotsset{compat=1.14}

\usepackage{filecontents}
    \begin{filecontents}{List1.csv}
A B
1 2
    \end{filecontents}
    \begin{filecontents}{List2.csv}
A B
3 4
    \end{filecontents}  

\begin{document}

% % % This works fine
\begin{center}\bfseries
    Test with pgfplotsforeachungrouped and explicit list
\end{center}

\pgfplotsforeachungrouped \filename in {List1.csv,List2.csv} {%
    <\filename> %debug output
    \pgfplotstableread{\filename}{\temptable} %read file
    \pgfplotstablevertcat{\output}{\temptable} %append file
    \pgfplotstabletypeset{\temptable} %debug output
    \hfill %debug output
}%
Output : \pgfplotstabletypeset{\output} %debug output
\pgfplotstableclear{\output} %clear \output

% % % This does not work
\begin{center}\bfseries
    Test with pgfplotsforeachungrouped and external list
\end{center}

\pgfplotsforeachungrouped \filename in {\fileList} {%
    <\filename> 
}
% Outputs <List1.csv,List2.csv>

% % % This does not work
\begin{center}\bfseries
    Test with pgfplotsforeachungrouped and external list
\end{center}
Package pgfplots Error: Found unexpected characters: expected 'in '.

%\pgfplotsforeachungrouped \filename in \fileList {%
%   <\filename> %debug output
%}

% this works
\begin{center}\bfseries
    Test with pgfplotsinvokeforeach and explicit list
\end{center}
\pgfplotsinvokeforeach{List1.csv,List2.csv}{ %
    <#1>
}

%
%
%
\begin{center}\bfseries
    Tests with foreach and external list
\end{center}
\foreach \filename in \fileList{ %
    <\filename> %debug output
    \pgfplotstableread{\filename}{\temptable} %read file
    \pgfplotstablevertcat{\output}{\temptable} %append file
    \pgfplotstabletypeset{\temptable} %debug output
    \hfill %debug output
}%
% \output is not defined


\foreach \filename in {\fileList}{ %
    <\filename>
    % Outputs <List1.csv,List2.csv>
}

\end{document}
2

The problem is, that \fileList is not expanded. The different syntax of \pgfplotsinvokeforeach is useful here. Add two \expandafters and it works with your \fileList.

\newcommand*\fileList{List1.csv,List2.csv}

\documentclass[parskip=half]{scrartcl}

\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}

\usepackage{pgfplotstable}
    \pgfplotsset{compat=1.14}

\usepackage{filecontents}
    \begin{filecontents}{List1.csv}
A B
1 2
    \end{filecontents}
    \begin{filecontents}{List2.csv}
A B
3 4
    \end{filecontents}  

\begin{document}

% this works
\begin{center}\bfseries
    Test with pgfplotsinvokeforeach and some \verb|\expandafter|\,s
\end{center}
\expandafter\pgfplotsinvokeforeach\expandafter{\fileList}{ %
    <#1>
    \pgfplotstableread{#1}{\temptable} %read file
    \pgfplotstablevertcat{\output}{\temptable} %append file
    \pgfplotstabletypeset{\temptable} %debug output
    \hfill %debug output
}
Output : \pgfplotstabletypeset{\output} %debug output
\end{document}

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.