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.

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
share|improve this question
add comment

1 Answer

up vote 3 down vote accepted

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}
share|improve this answer
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.