# How to make pgfplotstable read tables with macros

I want to migrate my tables to pgfplotstables. I used to have a macro for the rows of my tables, partly, because Kile used to.

So currently, my tables more or less look like:

oldtable:
\begin{tabular}{ccc}
1&2&3\\
\myrow{}
\rowentry{1}{2}{3}{4}{5}{6}
\end{tabular}


And I want to migrate that to something like:

newtable:
\pgfplotstabletypeset[debug]{
one & two & three\\
\myrow{}
\rowentry{1}{2}{3}{4}{5}{6}
}


But that fails with the following error:

./main.tex:28: Package pgfplots Error: input table '<inline_table>' has an unba
lanced number of columns in row '1' (expected '3' cols; got '1'). Maybe the inp
ut table is corrupted? If you need unbalanced data, consider using 'nan' in emp
ty cells (perhaps combined with 'unbounded coords=jump').

------- PGFPLOTSTABLE DEBUG MODE: --------
./main.tex:28: Undefined control sequence.
\\  ->\let \reserved@e
\relax \let \reserved@f \relax \@ifstar {\let \reserv...
l.28 }


I suppose that is because the macros don't get expanded. I don't necessarily want to edit all the tables. So my question is: How to make pgfplotstables read my table definition?

Full example:

\documentclass{article}
\usepackage{booktabs}
\usepackage{pgfplotstable}

% global settings
\pgfplotstableset{
col sep = &,
row sep=\\,
%string type, %% The error changes when enabling this type
}

\newcommand{\myrow}{foo & bar & baz\\}
\newcommand*{\rowentry}[6]{#1 & #2 & #3\\} %& #4}\\}
\begin{document}

oldtable:
\begin{tabular}{ccc}
1&2&3\\
\myrow{}
\rowentry{1}{2}{3}{4}{5}{6}
\end{tabular}

newtable:
\pgfplotstabletypeset[debug]{
one & two & three\\
\myrow{}
\rowentry{1}{2}{3}{4}{5}{6}
}

\end{document}

-

\documentclass{article}
\usepackage{booktabs}
\usepackage{pgfplotstable}

% global settings
\pgfplotstableset{
col sep = &,
row sep=\\,
string type, %% The error changes when enabling this type
}

\newcommand{\myrow}{foo & bar & baz\\}
\newcommand*{\rowentry}[6]{#1 & #2 & #3\\} %& #4}\\}

\def\foo#1[#2]#3{{
\let\\\relax
\gdef\tmpa{#1[#2]}%
\xdef\tmpb{\noexpand\tmpa{#3}}}%
\tmpb}

\begin{document}

oldtable:
\begin{tabular}{ccc}
1&2&3\\
\myrow{}
\rowentry{1}{2}{3}{4}{5}{6}
\end{tabular}

\foo
\pgfplotstabletypeset[debug]{
one & two & three\\
\myrow{}
\rowentry{1}{2}{3}{4}{5}{6}
}

\end{document}

-
You, sir, are awesome. Thanks. It works with the small caveat of one having to provide the optional argument. But that's fine. It looks much like dark magic to me and I am afraid I would have been able to come up with that myself. Can you explain a little what it does, if it's not too much to ask for? –  Frederick Nord Mar 21 '13 at 15:57
It could be re-arranged to not requote the argument or you could have to functions \def\foob#1#2{{ \let\\\relax \gdef\tmpa{#1}% \xdef\tmpb{\noexpand\tmpa{#2}}}% \tmpb} to use in the no [] case. It simply uses \xdef to expand any macros in the main argument of \pgfplotstabletypeset before \pgfplotstabletypeset is called, so the pgf code never sees myrow or \rowentry, the string type that you had added but commented out is then needed as teh pgf code then does see foo & bar & baz which are not numbers:-) –  David Carlisle Mar 21 '13 at 16:21