3

I am starting to use the package pgfplotstable as it seems to be a powerful tool and is able to do what I'd like to do.

I am importing custom CSVs, and as these will change over time, I need to plot the corresponding table with conditional formatting. I managed to get it 95% the way I want it but am struggling getting the last 5%, as usual.

Here is a MWE :

\documentclass{report}
\usepackage{pgfplotstable}
\usepackage{booktabs}

\begin{document}

\pgfplotstableread[col sep=comma]{test.csv}\table

\pgfplotstableset{
    bold/.append style={
        postproc cell content/.append code={
                \pgfkeysalso{@cell content=\textbf{##1}}%
        },
    },
    bold last row/.style={
        postproc cell content/.append code={
            \count0=\pgfplotstablerow
            \advance\count0 by1
            \ifnum\count0=\pgfplotstablerows
                \pgfkeysalso{@cell content=\textbf{##1}}%
            \fi
        },
    },
}

\begin{center}
\pgfplotstabletypeset[
string type,
% bolding the first row of data, and then the 4th, and finally the last one
every row 0 column 0/.style={bold},
every row 0 column 1/.style={bold},
every row 0 column 2/.style={bold},
every row 3 column 0/.style={bold},
every row 3 column 1/.style={bold},
every row 3 column 2/.style={bold},
bold last row,
%columns/0/.style={dec sep align, precision=3} % this doesn't work the way it is
%columns/1/.style={dec sep align, precision=3} % this doesn't work the way it is
%columns/2/.style={dec sep align, precision=3} % this doesn't work the way it is
every head row/.style={before row={\toprule},after row={\midrule}},
every last row/.style={before row={\midrule},after row={\toprule}},
]\table
\end{center}
\end{document} 

Also, the test.csv that is required looks like this :

Header 1,Header 2,Header 3
Cat 1 total,12767849.250,14470789.000
cat 1.1,5545718.200,NA
cat 1.2,7222131.050,NA
cat 2 total,3496774.665,5937501.000
cat 2.1,939910.000,NA
cat 2.2,856060.565,NA
cat 2.3,839982.000,NA
cat 2.4,433976.100,NA
cat 2.5,426846.000,NA
grand total,16264623.915,20408290.000

The remaining things I'd like to do :

  • headers in bold style

  • As you sill see the row 'cat 2 total' is in bold, on purpose, but I would also need a midrule before this row.

  • the first column (header 1) needs to be type l (aligned left)

  • the other columns need to be type 'dec sep align' (aligned on the decimal separator) and I need to be able to choose the level of precision i want. I thought that the commented line in the code would do that, but it doesn't and messes with the other stuff.

  • one last, I've choosen to put 'NA' where i dont have a value but maybe there is a better way ? leaving the cell empty ?

I'm guessing this is very easy for one that knows how to do it, so thanks for your help in advance.

Edit 1 : UPDATE

I've modified slightly the code above and am now using the following

\begin{center}
\pgfplotstabletypeset[
display columns/0/.style={
    column name={\textbf{Column 1}},column type = {l},string type
  },
display columns/1/.style={
    column name={\textbf{Column 2}},fixed,column type = {r}
  },
display columns/2/.style={
    column name={\textbf{Column 3}},fixed,column type = {r}
  },
% bolding the first row of data, and then the 4th, and finally the last one
every row 0 column 0/.style={bold},
every row 0 column 1/.style={bold},
every row 0 column 2/.style={bold},
every row 3 column 0/.style={bold},
every row 3 column 1/.style={bold},
every row 3 column 2/.style={bold},
bold last row,
every head row/.style={before row={\toprule},after row={\midrule}},
%every row no/3/.style={before row=\midrule} %that will break the head row ruling...
every last row/.style={before row={\midrule},after row={\toprule}},
]\tableone
\captionof{table}{Tonnes dumped per location}
\end{center}

By defining the columns names, type and content, it allows me also to get my headers in bold but this is probably not the best way... This way i am supposed to be able to use the dec sep align and precision as suggested in the comments. However it doesn't work properly when bolded. It seems that it will choose either to be in the right format ('fixed' parameter) and not bolded, or bolded but not formatted as the other numbers ... I am sure I'm missing something here. Note that I am just trying to get 'fixed' to work whenever it is bolded or not, and the alignment is taken care of using flush right for now. Ideally I'll use precision and dec sep align when I'll get this to work the way i want.

Also I can get the midrule before my row #3, but if i do that (using the commented line in my code above) it breaks the 'every head row' code as it is not the 3rd row...

So the remaining 2 problems that I have are :

  1. I want a midrule before the row 3 but wants to keep the formatting for head and last row.

  2. I'd like the numbers dec sep aligned as is, including when bolded. The bold function seems to override the dec sep align setting.

Thanks for your help.

  • Welcome to TeX.SX! You can have a look at our starter guide to familiarize yourself further with our format. – Martin Schröder Mar 13 '15 at 1:55
  • 1
    The reason dec sep align and the precision aren't working is because you've specified that the whole table should be typeset in string type, so pgfplotstable ignores the fact that these are numbers. You'll have to be more selective about where you apply the string type. – darthbith Mar 13 '15 at 12:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.