5

I want to generate a formatted table from a CSV file:

  • with every other row in a different color
  • using 4 digits precision for numbers

Right now I can't seem to have both.

Here's an example code to illustrate my issue:

test.csv

a,b,c
d,0.4897844135,0.489789797
e,0.4897844135,0.489789797
f,0.4897844135,0.489789797

test.tex

\documentclass[a4paper,10pt]{article}

\usepackage{pgfplotstable}
\usepackage{colortbl}

\begin{document}


  \pgfplotstabletypeset[
    dec sep={$,$},
    col sep=comma,
    columns={a,b,c},
    columns/a/.style={string type, column type={|c}},
    columns/b/.style={precision=4, column type={|c}},
    columns/c/.style={precision=4, column type={|c|}},
    every head row/.style={before row=\hline,after row=\hline},
    every row/.style={after row=\hline},
    every last row/.style={after row=\hline},
    postproc cell content/.code={
        \ifodd\pgfplotstablerow\relax
        \else
            % ah - an even row number.
            % ah - introduce a cell color:
            \pgfkeysalso{@cell content={\cellcolor[gray]{0.9}#1}}%
        \fi
    }
    ,]{test.csv}
  }


\end{document}

Output: Illustration

Note that I also lose my decimal separator setting.

What should I add to my code to make it behave the way I want?

Should I use another package/syntax to achieve the result I expect? I'm open to using any other package that would achieve the same result.

4

By setting \pgfkeysalso{@cell content={\cellcolor[gray]{0.9}#1}}% you're overwriting the cell content that PGFPlotstable generated (the #1 only refers to the raw value of the cell, not the rounded output). You only need to add the \cellcolor[gray]{0.9} command to the cell content but leave the rest unchanged. You can do that using the key

\pgfkeysalso{@cell content/.prefix={\cellcolor[gray]{0.9}}}%

\documentclass[a4paper,10pt]{article}

\usepackage{pgfplotstable}
\usepackage{colortbl}

\begin{document}


  \pgfplotstabletypeset[
    dec sep={$,$},
    col sep=comma,
    columns={a,b,c},
    columns/a/.style={string type, column type={|c}},
    columns/b/.style={precision=4, column type={|c}},
    columns/c/.style={precision=4, column type={|c|}},
    every head row/.style={before row=\hline,after row=\hline},
    every row/.style={after row=\hline},
    every last row/.style={after row=\hline},
    postproc cell content/.code={
        \ifodd\pgfplotstablerow\relax
        \else
            % ah - an even row number.
            % ah - introduce a cell color:
            \pgfkeysalso{@cell content/.prefix={\cellcolor[gray]{0.9}}}%
        \fi
    }
    ,]{
a,b,c
d,0.4897844135,0.489789797
e,0.4897844135,0.489789797
f,0.4897844135,0.489789797    
    }


\end{document}
2
  • This works but I just realized this broke the horizontal centering of the table allowing it to be outside document margins I achiever using \centerline{} around the table. Any explanation/workaround for this?
    – nicoco
    Jun 5 '16 at 8:22
  • @nicoco: I can't reproduce that issue. You could try using the \centering command instead, but if that doesn't work you might have to open a new question.
    – Jake
    Jun 6 '16 at 10:07
0

Jake's answer was correct, but I ended up doing things slightly differently and figured I'll share:

\documentclass[a4paper,10pt]{article}

\usepackage{pgfplotstable}
\usepackage{colortbl}
\usepackage{xcolor}


\begin{document}
text
\begin{table}[!h]
\center

  \pgfplotstabletypeset[
    dec sep={$,$},
    col sep=comma,
    columns={a,b,c},
    columns/a/.style={string type, column type={c}},
    columns/b/.style={precision=5, column type={c}},
    columns/c/.style={precision=5, column type={c}},
    every even row/.style={before row={\rowcolor{blue!10}}},
    every odd row/.style={before row={\rowcolor{blue!3}}},
    ,]{
    a,b,c
    d,0.4897844135,0.489789797
    e,0.4897844135,0.489789797
    f,0.4897844135,0.489789797    
    }

\caption{Blah blah blah}
\end{table}
text
\end{document}

As you can see the syntax is more understandable like this.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.