5

I can use the csvsimple package to automatically display data from a CSV file in a tabular environment. Can the same be achieved in transposed (rows to columns and vice versa) form , as shown by the second hand-coded example?

\begin{filecontents*}{transpose.csv}
course,J,Js,F,Fs,G,Gs,T,Ts
1,4,4,4,4,3,3,5,5
2,3,7,2,6,2,5,4,9
3,4,11,3,9,5,10,2,11
4,2,13,7,16,3,13,7,18
5,7,20,3,19,7,20,7,25
6,5,25,3,23,7,27,2,27
7,3,28,5,28,2,29,3,30
8,6,34,4,32,7,36,3,33
9,4,38,2,34,3,39,7,40
10,4,42,2,36,6,45,2,42
\end{filecontents*}

\documentclass{article}
\usepackage{booktabs}
\usepackage{csvsimple}

\begin{document}
\centering

\begin{tabular}{rcccc}
  \toprule
  \# & Subj J & Subj F & Subj G & Subj T \\
  \midrule
    \csvreader[head to column names,late after line=\\]{transpose.csv}{}%
    {\course & \J & \F & \G & \T}
  \bottomrule
\end{tabular}

\vspace{2em}

\begin{tabular}{lcccccccccc}
  \toprule
  \# & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 & 10 \\
  \midrule
  Subj J & 4 & . & . & . & & &  &  &  & 4 \\
  Subj F & 4 & . & . &  & & &  &  & . & 2 \\
  Subj G & 3 & . &  &  & & &  & . & . & 6 \\
  Subj T & 5 &  &  &  & & & . & . & . & 2 \\
  \bottomrule
\end{tabular}

\end{document}

Graphically: top shows the current behaviour, bottom the desired output:

Top table from CSV file, desired bottom table with transposed rows/columns

  • I did not know that csvsimple exists. Seems to be useful. As there is no option to scvreader to transpose you seem have to do it yourself. See groups.google.com/forum/#!topic/comp.text.tex/K8UTcsocIJ0 for an awk script. If you only have a small amount of csv-data you can use MS Excel (at least the current version): Copy your data, right click (at some table sheet with enough room) and choose "transpose" from the context menu. – Rüdiger Voigt Jul 21 '14 at 11:41
3

My first thought was to use pgfplotstable. You can choose which columns (of the original table) to plot by the columns key in the \pgfplotstabletranspose macro. The colnames from key says that I want the columns of the transposed table to be named based on the values in the course column of the original table. The postproc cell content line adds Subj before each of the course subjects.

begin{filecontents*}{transpose.csv}
course,J,Js,F,Fs,G,Gs,T,Ts
1,4,4,4,4,3,3,5,5
2,3,7,2,6,2,5,4,9
3,4,11,3,9,5,10,2,11
4,2,13,7,16,3,13,7,18
5,7,20,3,19,7,20,7,25
6,5,25,3,23,7,27,2,27
7,3,28,5,28,2,29,3,30
8,6,34,4,32,7,36,3,33
9,4,38,2,34,3,39,7,40
10,4,42,2,36,6,45,2,42
\end{filecontents*}

\documentclass{article}
\usepackage{booktabs}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.10}

\begin{document}

\pgfplotstableread[
    col sep=comma,
]{transpose.csv}\normal

\pgfplotstabletranspose[
    colnames from=course,
    columns={course,J,F,G,T},
]\transpose\normal

\begin{center}
\pgfplotstabletypeset[
    string type,
    every head row/.style={before row=\toprule, after row=\midrule},
    every last row/.style={after row=\bottomrule},
    every col no 0/.style={
        column type={l},
        column name={\#},
        postproc cell content/.append style={/pgfplots/table/@cell content/.add={Subj }{}},
    },
]\transpose
\end{center}

\end{document}

pgfplotstabletranspose

3

My idea is to build up the transposed rows step by step with every data line. Afterwards, the table is generated:

\begin{filecontents*}{transpose.csv}
course,J,Js,F,Fs,G,Gs,T,Ts
1,4,4,4,4,3,3,5,5
2,3,7,2,6,2,5,4,9
3,4,11,3,9,5,10,2,11
4,2,13,7,16,3,13,7,18
5,7,20,3,19,7,20,7,25
6,5,25,3,23,7,27,2,27
7,3,28,5,28,2,29,3,30
8,6,34,4,32,7,36,3,33
9,4,38,2,34,3,39,7,40
10,4,42,2,36,6,45,2,42
\end{filecontents*}

\documentclass{article}
\usepackage{booktabs,etoolbox}
\usepackage{csvsimple}

\begin{document}
\centering

\begin{tabular}{rcccc}
  \toprule
  \# & Subj J & Subj F & Subj G & Subj T \\
  \midrule
    \csvreader[head to column names,late after line=\\]{transpose.csv}{}%
    {\course & \J & \F & \G & \T}
  \bottomrule
\end{tabular}

\vspace{2em}

% These macros save the data
\def\columnAll{}
\def\courseAll{}
\def\JAll{}
\def\FAll{}
\def\GAll{}
\def\TAll{}

% Reading data and store into the auxiliary macros
\csvreader[head to column names]{transpose.csv}{}%
  {\edef\columnAll{\columnAll c}
   \edef\courseAll{\courseAll&\course}%
   \edef\JAll{\JAll&\J}%
   \edef\FAll{\FAll&\F}%
   \edef\GAll{\GAll&\G}%
   \edef\TAll{\TAll&\T}}%

% Finally: output as table
\begin{tabular}{l\columnAll}
  \toprule
  \# \courseAll\\
  \midrule
  Subj J \JAll\\
  Subj F \FAll\\
  Subj G \GAll\\
  Subj T \TAll\\
  \bottomrule
\end{tabular}

\end{document}

enter image description here

  • I really like the simplicity of your answer, but I think that the pgfplotstable is the 'sharper knife' for the job. – ojdo Jul 22 '14 at 23:57

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.