\documentclass[a4paper]{article}

\usepackage[intlimits]{amsmath}
\usepackage{amssymb}
\usepackage{amsfonts}
\usepackage{ifpdf}
\usepackage{listings}
\usepackage{rotating}

\usepackage{array}
\usepackage{booktabs}
\usepackage{colortbl}

\lstset{basicstyle=\ttfamily\footnotesize,tabsize=16}

\ifpdf
\else
\def\pgfsysdriver{pgfsys-dvipdfm.def}
\fi
\usepackage{pgfplots}

\parindent=0pt

\def\testsection#1{\message{STARTING TEST SECTION '#1'}\section{#1}}
\def\testsubsection#1{\message{STARTING TEST SUBSECTION '#1'}\subsection{#1}}
\def\testsubsubsection#1{\message{STARTING TEST SUBSUBSECTION '#1'}\subsubsection{#1}}

\author{Christian Feuers\"anger}
\title{Test cases for pgfplotstable}

\begin{document}
\maketitle

\testsection{Reading a tab-separated table with many entries}
\begin{sidewaystable}
	\lstinputlisting{numtabletestdata}
\end{sidewaystable}
\testsubsection{Reading it...}
\tracingmacros=2\tracingcommands=2
\pgfplotstableread{numtabletestdata}\table
\tracingmacros=0\tracingcommands=0

\testsubsection{Quering colnames...}
%\tracingmacros=2\tracingcommands=2
\pgfplotstablegetcolumnlist\table\to\cols
\pgfplotslistforeach\cols\as\col{Column ``\col''\par}%
%\tracingmacros=0\tracingcommands=0

\testsubsection{Quering column content by column name}
%\tracingmacros=2\tracingcommands=2
\pgfplotstablegetcolumnbyname L2\of\table\to\colcontent
\pgfplotslistforeach\colcontent\as\colc{Row data: ``\colc''\par}%
%\tracingmacros=0\tracingcommands=0

\testsubsection{Quering column content by column name for all cols}
\pgfplotstablegetcolumnlist\table\to\cols
\pgfplotslistforeach\cols\as\col{\textbf{Column ``\col''}\par
	\pgfplotstablegetcolumnbyname\col\of\table\to\colcontent
	\pgfplotslistforeach\colcontent\as\colc{Row data: ``\colc''\par}%
}%

\testsection{Reading gnuplot output for use in 'plot file'}
\lstinputlisting{numtabletestdata2.gnuplot}
%\tracingmacros=2\tracingcommands=2
\pgfplotstableread{numtabletestdata2.gnuplot}\table
%\tracingmacros=0\tracingcommands=0

\testsubsection{Quering column content by column name for all cols}
\pgfplotstablegetcolumnlist\table\to\cols
\pgfplotslistforeach\cols\as\col{\textbf{Column ``\col''}\par
	\pgfplotstablegetcolumnbyname\col\of\table\to\colcontent
	\pgfplotslistforeach\colcontent\as\colc{Row data: ``\colc''\par}%
}%

\testsection{Table typesetting}
%
% Set this here globally:
\pgfplotstableset{/pgf/number format/sci zerofill}% any number in SCI format shall use zero filling.

\pgfplotstableread{numtabletestdata}\table

\testsubsection{no options at all}
\pgfplotstabletypeset\table

\testsubsection{Basis, L2, no options}
%\tracingmacros=2\tracingcommands=2
\pgfplotstabletypeset[columns={Basis,L2}]\table
%\tracingmacros=0\tracingcommands=0

\testsubsection{some column adjustments}
\pgfplotstabletypeset[
	columns={maxlevel,Basis,L2,cgiter},
	columns/maxlevel/.style={column name=$l_\infty$,column type=r},
	columns/cgiter/.style={column name={CG}},
	columns/L2/.style={column name={$L_2$},/pgf/number format/sci subscript,/pgf/number format/sci zerofill}]
	\table

\testsubsection{some row adjustments}
\pgfplotstabletypeset[
	/pgf/number format/sci subscript,/pgf/number format/sci zerofill,
	every head row/.style={before row=\toprule,after row=\midrule},
	every last row/.style={after row=\bottomrule},
	]
	\table


\testsubsection{coloring rows}

\pgfplotstabletypeset[
	every even row/.style={before row={\rowcolor[gray]{0.9}}},
	every head row/.style={before row=\toprule,after row=\midrule},
	every last row/.style={after row=\bottomrule},
	]
	\table

\testsubsubsection{writing to numtabletest.generated1.tex}%
\pgfplotstabletypeset[
	outfile=numtabletest.generated1.tex,
	every even row/.style={before row={\rowcolor[gray]{0.9}}},
	every head row/.style={before row=\toprule,after row=\midrule},
	every last row/.style={after row=\bottomrule},
	]
	\table

\testsubsubsection{loading numtabletest.generated1.tex}
\input{numtabletest.generated1.tex}

\testsubsection{Choosing columns by index}
%\tracingmacros=2\tracingcommands=2
\pgfplotstabletypeset[
	columns={[index]5,dof,[index]0}
	]
	\table
%\tracingmacros=0\tracingcommands=0

\testsubsection{column styles}
\pgfplotstabletypeset[
	every even column/.style={column type={>{\columncolor[gray]{.8}[\tabcolsep]}l}},
	]
	\table

\begingroup
\testsubsection{column styles + string type}
\pgfplotstableread{numtabletestdata2}\table
%\tracingmacros=2\tracingcommands=2
\pgfplotstabletypeset[
	every first column/.style={string type,column type={@{}r|},column name=},
	every head row/.style={after row=\hline},
	every even column/.style={column type={>{\columncolor[gray]{.8}[\tabcolsep]}l}},
	]
	\table
\endgroup

\begingroup
\testsubsection{column styles and special characters =/,}
\pgfplotstableread{numtabletestdata3}\table
\pgfplotstabletypeset[
	columns/{grad(log(dof),log(L2))}/.style={
		column name=$L_2$ slopes,
		/pgf/number format/fixed,
		/pgf/number format/precision=1,
		/pgf/number format/fixed zerofill},
	columns/L2/.style={
		column name=$L_2$,
		/pgf/number format/sci
	}
	]
	\table

\testsubsection{Input formats}
\tracingmacros=2\tracingcommands=2
{
\pgfplotstableset{columns/head/.style={string type}}
\testsubsubsection{comma}
\pgfplotstabletypesetfile[col sep=comma]{numtabletestdata2.csv}

\testsubsubsection{colon}
\pgfplotstabletypesetfile[col sep=colon]{numtabletestdata2.colon}

\testsubsubsection{semicolon}
\pgfplotstabletypesetfile[col sep=semicolon]{numtabletestdata2.semicolon}

\testsubsubsection{brace}
\pgfplotstabletypesetfile[col sep=braces]{numtabletestdata2.brace}
\tracingmacros=0\tracingcommands=0
}
\endgroup


{
\testsection{Col type}

\newcolumntype{L}[1]
	{>{\begin{pgfplotstablecoltype}[#1]}r<{\end{pgfplotstablecoltype}}}

\begin{tabular}{L{int detect}L{sci,sci subscript,sci zerofill}}
9      & 2.50000000e-01\\
25     & 6.25000000e-02\\
81     & 1.56250000e-02\\
289    & 3.90625000e-03\\
1089   & 9.76562500e-04\\
4225   & 2.44140625e-04\\
16641  & 6.10351562e-05\\
66049  & 1.52587891e-05\\
263169 & 3.81469727e-06\\
1050625& 9.53674316e-07\\
\end{tabular}

}

\testsection{Creating new columns}
{
\tracingmacros=2\tracingcommands=2
\pgfplotstableset{columns={dof,L2,maxlevel,cgiter,new},columns/new/.style={string type}}
\pgfplotstableread{numtabletestdata2}\table
\pgfplotstablecreatecol[
	create col/assign next/.code={%
		\getthisrow{maxlevel}\entry
		\getnextrow{maxlevel}\nextentry
		\edef\entry{this=\entry; next=\nextentry. (\#\pgfplotstablerow/\pgfplotstablerows)}%
		\pgfkeyslet{/pgfplots/table/create col/next content}\entry
	},
]
{new}
\table

\pgfplotstabletypeset\table

}

\end{document}
