Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

How am I able to rotate the first entries of a table with pgfplotstable? I have the following table and want to rotate the descN labels in the first row.

enter image description here

\documentclass{article}
    \usepackage[table]{xcolor}
    \usepackage{pgfplotstable}

    \pgfplotstableset{
        every head row/.style={
            before row=\hline,
            after row=\hline\hline
        },
        every last row/.style={
            after row=\hline
        },
        every first column/.style={
            column type/.add={|}{}
        },
        every last column/.style={
            column type=||l|
        },
        color cells/.style={
            col sep=&,
            row sep=\\,
            string type,
            postproc cell content/.code={%
                    \pgfkeysalso{@cell content=\rule{0cm}{2.4ex}%
                    \pgfmathsetmacro\y{max((##1 * 100), 0}%
                    \edef\temp{\noexpand\cellcolor{black!\y}}\temp%
                    \pgfmathtruncatemacro\x\y%
                    \ifnum\x>50 \color{white}\fi%
                    ##1}%
                    },
            columns/x/.style={
                column name={},
                postproc cell content/.code={}
            }
        }
    }

\begin{document}
    \begin{table}
    \centering
    \pgfplotstabletypeset[color cells]{
        desc1 & desc2 & desc3 & desc4 & x \\
        0.9 & 0.1 & 0 & 0 & desc1 \\
        0 & 0.8 & 0.1 & 0.1 & desc2 \\
        0 & 0 & 0.95 & 0.05 & desc3 \\
        0 & 0.1 & 0.05 & 0.85 & desc3 \\
    }
    \end{table}
\end{document}
share|improve this question
add comment

2 Answers

up vote 6 down vote accepted

There must be another key to reach this but I can't remember it so I did it a little laborously.

\documentclass{article}
    \usepackage[table]{xcolor}
    \usepackage{pgfplotstable}

    \pgfplotstableset{
        every head row/.style={
            before row=\hline,
            after row=\hline\hline,
            typeset cell/.code={
            \ifnum\pgfplotstablecol=\pgfplotstablecols
            \pgfkeyssetvalue{/pgfplots/table/@cell content}{\\}%
            \else
            \pgfkeyssetvalue{/pgfplots/table/@cell content}{\rotatebox{90}{##1}&}%
            \fi
            }
        },
        every last row/.style={
            after row=\hline
        },
        every first column/.style={
            column type/.add={|}{}
        },
        every last column/.style={
            column type=||l|
        },
        color cells/.style={
            col sep=&,
            row sep=\\,
            string type,
            postproc cell content/.code={%
                    \pgfkeysalso{@cell content=\rule{0cm}{2.4ex}%
                    \pgfmathsetmacro\y{max((##1 * 100), 0}%
                    \edef\temp{\noexpand\cellcolor{black!\y}}\temp%
                    \pgfmathtruncatemacro\x\y%
                    \ifnum\x>50 \color{white}\fi%
                    ##1}%
                    },
            columns/x/.style={
                column name={},
                postproc cell content/.code={}
            }
        }
    }

\begin{document}
    \begin{table}
    \centering
    \pgfplotstabletypeset[color cells]{
        desc1 & desc2 & desc3 & desc4 & x \\
        0.9 & 0.1 & 0 & 0 & desc1 \\
        0 & 0.8 & 0.1 & 0.1 & desc2 \\
        0 & 0 & 0.95 & 0.05 & desc3 \\
        0 & 0.1 & 0.05 & 0.85 & desc3 \\
    }
    \end{table}
\end{document}

enter image description here

share|improve this answer
    
Did you remember the key in the mean time? It seems relevant for tex.stackexchange.com/questions/99121/… too, where a different but equally kludgy solution was given. –  Christian Feb 6 at 14:23
add comment

I tried the solution given by @percusse, but it didn't show the desired result, e.g. no rotated header.

It seems that the typeset cell key is being ignored by the every head row selector - at least with pgfplotstable v1.5.1 that I am using.

A slight modification using the comment on another topic over at sf.net by Christian Feuersaenger did produce the desired output!

The key change is to apply the typeset cell on the whole table and use conditionals to isolate the part of the tabular we want to modify.

\documentclass{article}
\usepackage[table]{xcolor}
\usepackage{pgfplotstable}

\pgfplotstableset{
    %MODIFICATION START
    typeset cell/.append code={%
        \ifnum\pgfplotstablerow<0%head rows are -1
            \ifnum\pgfplotstablecol=\pgfplotstablecols%special treatment for last column
                \pgfkeyssetvalue{/pgfplots/table/@cell content}{\\}%
            \else
                \pgfkeyssetvalue{/pgfplots/table/@cell content}{\rotatebox{90}{#1}&}%
            \fi
        \fi
    },
    every head row/.style={
        before row=\hline,
        after row=\hline\hline,
    },
    %MODIFICATION END
    every last row/.style={
        after row=\hline
    },
    every first column/.style={
        column type/.add={|}{}
    },
    every last column/.style={
        column type=||l|
    },
    color cells/.style={
        col sep=&,
        row sep=\\,
        string type,
        postproc cell content/.code={%
                \pgfkeysalso{@cell content=\rule{0cm}{2.4ex}%
                \pgfmathsetmacro\y{max((##1 * 100), 0}%
                \edef\temp{\noexpand\cellcolor{black!\y}}\temp%
                \pgfmathtruncatemacro\x\y%
                \ifnum\x>50 \color{white}\fi%
                ##1}%
                },
        columns/x/.style={
            column name={},
            postproc cell content/.code={}
        }
    }
}
\begin{document}
\begin{table}
\centering
\pgfplotstabletypeset[color cells]{
    desc1 & desc2 & desc3 & desc4 & x \\
    0.9 & 0.1 & 0 & 0 & desc1 \\
    0 & 0.8 & 0.1 & 0.1 & desc2 \\
    0 & 0 & 0.95 & 0.05 & desc3 \\
    0 & 0.1 & 0.05 & 0.85 & desc3 \\
}
\end{table}
\end{document}

In my case I wanted to change the appearance of the head row, so may I suggest modifying the question to a more general form? that way more people with similar problems, like this closed question, will be drawn to this discussion.

Thanks again to @percusse and Christian Feuersaenger for pointing me to the right direction. I missed the importance of using \pgfkeyssetvalue in the manual ;)!

share|improve this answer
    
Oh I have version 1.6.1 indeed. –  percusse Oct 13 '12 at 23:07
    
Nice to see that that's an upcoming feature: I like your solution better! –  jhn Oct 13 '12 at 23:30
    
By the way, it's now the current version so it's released on CTAN. Hence you might want to update your version. And the question you've linked is closed because there was no MWE or any feedback from the OP. –  percusse Oct 14 '12 at 9:03
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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