[Tex/LaTex] Is it possible to exchange rows and columns of a table without retyping

tables

I wrote a table that turned out to be too wide. So I have to turn it sideways for it to fit a typical journal paper. Is there a way I can avoid typing it all over again?!

Best Answer

Like John Kormylo said, pgfplotstable can be a convenient way to do this.

\documentclass{article}
\usepackage{booktabs}
\usepackage{pgfplotstable}

\begin{document}

\pgfplotstableread{
{Header 1}   A   B   C   D   E
{Header 2}   1   2   3   4   5
{Header 3}  10   11   12   13   14
}\tablesource

\pgfplotstabletranspose[string type,
                        colnames from={Header 1},
                        input colnames to={Header 1}
                        ]\tabletransp{\tablesource}

\begin{center}
Table Source:

\pgfplotstabletypeset[string type,
        every head row/.style={before row=\toprule,after row=\midrule},
        every last row/.style={after row=\bottomrule},
        after row=\midrule
]{\tablesource}
\vspace{8mm}

Table transpose:

\pgfplotstabletypeset[string type,
        every head row/.style={before row=\toprule,after row=\midrule},
        every last row/.style={after row=\bottomrule},
        after row=\midrule
]{\tabletransp}
\end{center}

\end{document}

I'm not a expert, but in my test's I understand that's need to stay alert about spaces.

In \pgfplotstableread you will enter you source table. Into the final of this command, you will name this source; in this case is \tablesource.

In \pgfplotstabletranspose you will finish with tha name of transposed table; \tabletransp.

The command \pgfplotstabletypeset will call what you put into brackets.

Source and Transpose tables with pgfplotstabletranspose

Related Question