{pgfplotstable} is a package that can read, parse, modify or generate tables. Data is provided via either external files or inline code. It also serves as the data handler of the graphics package {pgfplots}.
The pgfplotstable tag is suitable for questions related to the pgfplots data handling or table typesetting via the package pgfplotstable
. Though it is shipped as a by-product of pgfplots
package, it offers a quite comprehensive table typesetting framework.
The main focus of this package, is to acquire data from various sources, to manipulate data, to allow for changing the table column/row properties and eliminate the manual (La)TeX-table typesetting.
Examples
Typesetting an inline table is done using the \pgfplotstabletypeset
command, example from the manual:
\documentclass{article}
\usepackage{pgfplotstable}
\begin{document}
\pgfplotstabletypeset[sci zerofill]{
a b
5000 1.234e5
6000 1.631e5
7000 2.1013e5
9000 1000000
}
\end{document}
The following examples are also adapted from the manual using an external datafile called pgfplotstable.dat
:
# Convergence results
# fictional source, generated 2008
level dof error1 error2 info grad(log(dof),log(error2)) quot(error1)
1 4 2.50000000e-01 7.57858283e-01 48 0 0
2 16 6.25000000e-02 5.00000000e-01 25 -3.00000000e-01 4
3 64 1.56250000e-02 2.87174589e-01 41 -3.99999999e-01 4
4 256 3.90625000e-03 1.43587294e-01 8 -5.00000003e-01 4
5 1024 9.76562500e-04 4.41941738e-02 22 -8.49999999e-01 4
6 4096 2.44140625e-04 1.69802322e-02 46 -6.90000001e-01 4
7 16384 6.10351562e-05 8.20091159e-03 40 -5.24999999e-01 4
8 65536 1.52587891e-05 3.90625000e-03 48 -5.35000000e-01 3.99999999e+00
9 262144 3.81469727e-06 1.95312500e-03 33 -5.00000000e-01 4.00000001e+00
10 1048576 9.53674316e-07 9.76562500e-04 2 -5.00000000e-01 4.00000001e+00
The most basic way to typeset this into a table:
\documentclass{article}
\usepackage{pgfplotstable}
\begin{document}
\pgfplotstabletypeset{pgfplotstable.dat}
\end{document}
A more advanced example loading the data into a macro for reuse or manipulation before typesetting. This also formats the data and headers of the table using the booktabs
package. Finally, this also plots the dof
against the level
data.
\documentclass{article}
\usepackage{booktabs}
\usepackage{pgfplots, pgfplotstable}
\pgfplotstableset{% global config, for example in the preamble
% these columns/<colname>/.style={<options>} things define a style
% which applies to <colname> only.
columns/dof/.style={int detect,column type=r,column name=\textsc{Dof}},
columns/error1/.style={
sci,sci zerofill,sci sep align,precision=1,sci superscript,
column name=$e_1$,
},
columns/error2/.style={
sci,sci zerofill,sci sep align,precision=2,sci 10e,
column name=$e_2$,
},
columns/{grad(log(dof),log(error2))}/.style={
string replace={0}{}, % erase ’0’
column name={$\nabla e_2$},
dec sep align,
},
columns/{quot(error1)}/.style={
string replace={0}{}, % erase ’0’
column name={$\frac{e_1^{(n)}}{e_1^{(n-1)}}$}
},
empty cells with={--}, % replace empty cells with ’--’
every head row/.style={before row=\toprule,after row=\midrule},
every last row/.style={after row=\bottomrule}
}
\begin{document}
% Read the file into a macro and use this to typeset the table
\pgfplotstableread{pgfplotstable.dat}{\loadedtable}
\pgfplotstabletypeset[ % local config, applies only for this table
1000 sep={\,},
columns/info/.style={
fixed,fixed zerofill,precision=1,showpos,
column type=r,
}
]
{\loadedtable}
\begin{tikzpicture}
\begin{axis}
\addplot table[x=level,y=dof] {\loadedtable};
\end{axis}
\end{tikzpicture}
\end{document}