11

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

maetra
  • 4,540
Sean Allred
  • 27,421
  • To be picky is it the thousands sep not the decimal alignment you are asking for? – percusse Sep 01 '13 at 14:37
  • @percusse I'm looking for centered alignment at the decimal point, but aligning the thousands sep as well would be a definite plus. See my edit. – Sean Allred Sep 01 '13 at 14:47
  • I think I still don't get it. Where should the centerline hit in your first example? – percusse Sep 01 '13 at 14:52
  • @percusse Each column should be centered relative to the longest (width-wise) item in the column (note that this could be data or the header), and each centered piece of data should also align with the others at its decimal point. – Sean Allred Sep 01 '13 at 15:02
  • The dec sep align introduces two columns, one right-aligned, the other one left-aligned. And in fact, the decimal point is center-aligned (you just don’t have any decimals in your numbers). The dec sep align key/algorithm is not very flexible. I would go with a siunitx solution here, too. You may need to tell PGFmath/plotstable to not typeset numbers but leave them alone so that siunitx can parse them, see an answer of mine that uses siunitx and pgfplotstable (look out for string type). – Qrrbrbirlbel Oct 12 '13 at 20:46

1 Answers1

7

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}
maetra
  • 4,540
  • Take a look at the multicolumn names key for the problem with \multicolumn. Also siunitx does not parse a cell if its content is surrounded by braces (but that gets tricky to set up because { } will be removed internally a few times). But you can use assign column name/.code=\pgfkeyssetvalue{/pgfplots/table/column name}{{{#1}}}, and then use assign column name instead of column name for every column without the need for any braces (in the main input) and \multicolumn. – Qrrbrbirlbel Oct 12 '13 at 20:53
  • @Qrrbrbirlbel thanks for the advice, the code looks cleaner now. I did I just wonder why the columns/.style key does not have any effect. Maybe you happen to have another hint? Also I didn't quite understand what you meant with the multicolumn names key. – maetra Oct 12 '13 at 22:39