PGFPlots Table – How to Center and Decimal-Align a PGFPlots Table

pgfplotstabletables

Here is the code I am working with:

\documentclass{article}
\usepackage{filecontents}
\begin{filecontents}{measurements.dat}
sample num-bugs num-part other-measure another
   5       80      190        200        210
  15      520      410        430        350
  25      650      640        630        900
  35     1100     1200       1150       1020
\end{filecontents}

\usepackage{pgfplots, pgfplotstable, booktabs, colortbl}
\pgfplotsset{compat=1.7}
\pgfplotstableset{
  every head row/.style=
    {before row={\toprule},
     after row={\midrule}},
  every last row/.style=
    {after row={\bottomrule}},
  every even row/.style=
    {before row={\rowcolor[gray]{0.9}}},
  columns/.style=
    {dec sep align,
    %column type=c
    }
  }

\begin{document}
\pgfplotstabletypeset[
  columns={sample, another, num-part},
  columns/sample/.style={
    column name={Sample},
  }
  ]{measurements.dat}
\end{document}

This results in the data being aligned at the decimal as expected.
However, the data is also aligned at l.
I need it to be aligned at c, so I add the option

columns/.style={column type=c}

(as commented-out above).

This generates the error:

ERROR: Extra alignment tab has been changed to \cr.

--- TeX said ---
<template> \endtemplate 

l.31   ]{measurements.dat}

--- HELP ---
There are too many separate items (column entries) in a single row of
an array or tabular environment. In other words, there were too many &
's before the end of the row. You probably forgot the \\ at the end of
the preceding row.

How can I effect a centered and aligned column?


Example

Header1 Header2 LongerHeader3 HeaderWithoutARealName4
  1.0     2.0        3.0                    4.0
  2.0    13.0        4             1234567890.23

Current left aligned outcome

enter image description here

Best Answer

Would that work for you? It uses siunitx to format the columns. There seems to some issues between the siunitx and pgfplotstable, therefore each column has to be formatted separatly and a column name has to be provided in braces. If not, siunitx complains about each e in the column name. Maybe some of the wizards here, know how to solve that.

enter image description here

MWE

\documentclass{article}
\usepackage{filecontents}
\begin{filecontents}{measurements.dat}
sample num-bugs num-part other-measure another
   5       80      190        200        210.02
  15      520      410.00     430        350
  25      650      640        630.2      900
  35     1100     1200        1150       1020
\end{filecontents}
\usepackage{pgfplots, pgfplotstable, booktabs, colortbl, siunitx, array}
\pgfplotsset{compat=1.8}

\begin{document}
\pgfplotstabletypeset[
    assign column name/.code=\pgfkeyssetvalue{/pgfplots/table/column name}{{{#1}}},
  columns={sample, another, num-part},
    every head row/.style=
    {before row={\toprule},
     after row={\midrule}},
  every last row/.style=
    {after row={\bottomrule}},
  every even row/.style=
    {before row={\rowcolor[gray]{0.9}}},
  columns/sample/.style={
   assign column name={Sample}, 
    column type={S[]}, string type,
  },
    columns/another/.style={%
        assign column name={Another},
        column type={S[]}, string type,
   }, 
        columns/num-part/.style={%
        assign column name={Num-Part},
        column type={S[]}, string type,
   },
    % Does produce the desired effect
    % columns/.style={column type={S[]}, string type,},
  ]{measurements.dat}
\end{document}
Related Question