[Tex/LaTex] Multi line header for pgfplotstable

formattingpgfplotstabletables

I have a dataset in csv file format that I'm loading into pgfplotstable. I can't change the format of the original csv, and being financial data it has a column of item names and two or three explanatory data points at the top of the table. e.g.

\documentclass{article}
\usepackage{array,datatool,pgfplotstable}
  \begin{filecontents}{sample.csv}
    item,2008,2009,2010,2011
     ,Actual,Actual,Forecast,Forecast
     ,GBP,GBP,GBP,GBP
     ,('000),('000),('000),('000) rounded
    Area 1 OP, 1000000,1500000,1750000,250000
    Area 2 OP, 400000,500000, 450000,-50000
    Area 51 OP, 300000,375000,390000,15000
    No P, 1250000, 1000000, 950000, 50000
    Residuals, 800000, 80000, 90000, 10000
  \end{filecontents}

\pgfplotstableread[col sep=comma]{sample.csv}\mystuff

I'm stumped at the next stage which is to typeset \mystuff so I have comma separators and can add a row that sums the 'Area' columns. At the moment to display the table I have to do something like

\begin{document}

\pgfplotstableread[col sep=comma,header=false]{sample.csv}\mystuff

\pgfplotstabletypeset[
  columns/0/.style={string type},
  columns/1/.style={string type},
  columns/2/.style={string type},
  columns/3/.style={string type},
  columns/4/.style={string type}]\mystuff

\end{document}

How do I tag the top three rows as string and the rest of the last four columns as integer type so I can format and transform them?

Best Answer

As pgfplotstable does not yet support multiline headers, you need to format the cells on these rows specially. I couldn't get the row styles to do this for me, so ended up formatting each individual cell:

\documentclass{article}
\usepackage{array,datatool,pgfplotstable}
\pgfplotsset{compat=1.6}
  \begin{filecontents}{sample.csv}
    item,2008,2009,2010,2011
     ,Actual,Actual,Forecast,Forecast
     ,GBP,GBP,GBP,GBP
     ,('000),('000),('000),('000) rounded
    Area 1 OP, 1000000,1500000,1750000,250000
    Area 2 OP, 400000,500000, 450000,-50000
    Area 51 OP, 300000,375000,390000,15000
    No P, 1250000, 1000000, 950000, 50000
    Residuals, 800000, 80000, 90000, 10000
  \end{filecontents}

\pgfplotstableread[col sep=comma]{sample.csv}\mystuff

\begin{document}

\pgfplotstabletypeset[
fixed,
display columns/0/.style={string type},
every row 0 column 1/.style={string type},
every row 0 column 2/.style={string type},
every row 0 column 3/.style={string type},
every row 0 column 4/.style={string type},
every row 1 column 1/.style={string type},
every row 1 column 2/.style={string type},
every row 1 column 3/.style={string type},
every row 1 column 4/.style={string type},
every row 2 column 1/.style={string type},
every row 2 column 2/.style={string type},
every row 2 column 3/.style={string type},
every row 2 column 4/.style={string type}]\mystuff

\end{document}

Sample output

Note that I have specified fixed for the default cell formatting. Your original attempt could have just been styled with a singel string type rather than specifying this for each individual column.

Related Question