# Repeated pgfplotstable calls causing errors

I've been working with pgplotstable for some time now and initially I was reading in data from .csv files. I'm writing a couple of packages that print tables and so I've written various \defs to encapsulate some table printing routines. So far so good. Now I want to produce tables from scratch and all of a sudden I'm getting errors. If you run the MWE it prints a table. If you uncomment the last call the following error text is produced

./Repeated Plots.tex:23: Package pgfplots Error: \pgfplotslistfront\ from
\pgfplotstable@colnames\ although list is EMPTY.


I can do tables with the same definition over and over again when I'm reading them from file, but when creating them from scratch I get conflicts.

MWE

\documentclass[border=6mm]{standalone}

\usepackage{pgfplotstable}
\usepackage{booktabs}

\pgfplotsset{compat=1.9}
%: table printing routine
\def\printtable#1{%
\pgfplotstableset{%
create on use/set/.style={create col/expr={1+\pgfplotstablerow}},
every head row/.style={before row=\toprule, after row=\midrule},
every last row/.style={after row=\bottomrule}
}
\pgfplotstablenew[columns={set}]{#1}\testtable
\pgfplotstablecreatecol[create col/assign/.code={%
\getthisrow{set}\tval\pgfmathtruncatemacro\entry{2*\tval-1}%
\pgfkeyslet{/pgfplots/table/create col/next content}\entry
}]{test}\testtable
\pgfplotstabletypeset[columns={test}]\testtable
}
\begin{document}
\printtable{10}\par
%\printtable{5}
\end{document}

-

It seems to be a grouping problem: apparently, data structures of the table cannot be overwritten without problems.

EDIT: This is a bug in pgfplotstable; it has been fixed and will be gone as of pgfplotstable 1.11 (current version is 1.10).

A solution could be to introduce groups into your macro, i.e. to insert \begingroup ... \endgroup at the beginning resp. end of your macro: all local variables will be freed after \endgroup.

If you need these variables outside of your macro, you can also consider writing groups around the usages of your macro, i.e. something like

{%

\printtable{10}

}%

...

{%
\printtable{5}
}%


Note that \begingroup/\endgroup is typically preferred inside of macros; in the main document I tend to use curly braces (which have the same effect for all practical purposes).

-
Why is this a problem only with \pgfplotstablenew and not \pgfplotstableread? Your basic solution works. But your other suggestion seems a bit convoluted. I'm writing a package, so I can't ask my users to write brackets around calls. I could of course have macros within macros. Also, I would like to extend this question to reusing the code for a style with a row predicate. I want to be able to pass the name of the macro that will hold the table but amongst the predicate code is ##1 and #1. How do I introduce my macro parameter? Perhaps your package wasn't intended for this type of use? –  Geoff Pointer Dec 8 '13 at 23:31
I've solved the part of my question about encapsulation and added it to the OP. The weird thing is that looking at the log file I can't actually see at what point the doubled up hashes are digested. –  Geoff Pointer Dec 10 '13 at 5:05
Well, you are right that the root cause is that pgfplotstable does not detect that the new table exists already. Possible solutions are to throw an exception if the table exists already or to clear it. –  Christian Feuersänger Dec 10 '13 at 20:06
I would suggestions that you file a new questions for the double-up - otherwise it is hard for others to read the questions here. –  Christian Feuersänger Dec 10 '13 at 20:09
I believe \pgfplotstablenew should overwrite old tables of the same name correctly. I will take a note on my todo list. –  Christian Feuersänger Dec 15 '13 at 17:38