4

I use same table format for several data files, so I have written a macro named \tableMacro.

The problem is: I need to include different columns in different tables. I want to parametrize columns={} property of \pgfplotstabletypeset.

\documentclass{article}
\usepackage{pgfplotstable}
% #1 name of data file
% #2 name of columns
\newcommand{\tableMacro}[2]{
    \pgfplotstableread{#1}\table
    \pgfplotstabletypeset[columns={ColA, ColB}]\table % This works, but
    %\pgfplotstabletypeset[columns={#2}]\table  % I need this line to work
}
\begin{document}
\tableMacro{table.txt}{ColA, ColB} % This works.
\def\colnames{ColA, ColB}
\tableMacro{table.txt}{\colnames} % However, I need this type of parameter passing
                                  % since there are several tables that 
                                  % include same set of columns
\end{document}

Contents of table.txt:

ColA ColB
2   23.2
5   67.8
3   11.4

1 Answer 1

3

As Marc said, the content of the macro needs to be expanded before it is fed to the key. A simple way of doing this is to use the /.expand once mechanism of pgfkeys: If you add that string to the columns key, the argument will be expanded once before being interpreted:

\documentclass{article}
\usepackage{pgfplotstable}

\begin{filecontents}{table.txt}
ColA ColB
2   23.2
5   67.8
3   11.4
\end{filecontents}

\newcommand{\tableMacro}[2]{
    \pgfplotstableread{#1}\table
    \pgfplotstabletypeset[columns/.expand once={#2}]\table 
}
\begin{document}

\tableMacro{table.txt}{ColA, ColB} % This works.

\def\colnames{ColB,ColA,ColB}
\tableMacro{table.txt}{\colnames} % However, I need this type of parameter passing
                                  % since there are several tables that 
                                  % include same set of columns
\end{document}

You must log in to answer this question.

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