7

Basically, I have a table (from a file) containing only numeric values. I would like to import in latex but it has to be formatted in a specific way, as illustrated by the picture.

The ... should be replaced by the table content. For example, I have filled the 'Line 1' field with some values coming from the table.

enter image description here

I know that pgfplotstable is a very powerful too but I can't figure how to import it correctly.

The code :

\documentclass{standalone}
\usepackage{filecontents,pgfplots}

\begin{filecontents}{example.dat}
    1.23 2.34  %first line
    2.45 6.54  %first line
    4.56 4.43  %first line
    %Also numeric values for the Line 2 and Line 3
\end{filecontents}


\begin{document}

\begin{tabular}{|c|p{3cm}|p{3cm}|}
\hline
    & Column 1& Column 2\\\hline
Line 1 & 1.23\newline 2.45\newline4.56 &2.34\newline 6.54\newline4.43 \\\hline
Line 2 & ...\newline ...\newline... &...\newline ...\newline...\\\hline
Line 3 & ...\newline ...\newline... &...\newline ...\newline...\\\hline
\end{tabular}

\end{document}
  • You show your data as single digits "a" and "b". It would be useful to know if they are indeed that, or single words, or phrases. If the data for an element contains blanks, what delimiter will your file have to separate column data? Or is that up to us, and you can create your input file to conform? – Steven B. Segletes Sep 25 '13 at 20:25
  • Thanks for pointing out this, I'll make it more precise ! – Laurent Dudok de Wit Sep 25 '13 at 20:27
  • See tex.stackexchange.com/questions/117747/… for ideas. – Steven B. Segletes Sep 25 '13 at 20:28
  • I have shown a suggestion for a mapping from the number to the fields in the table. It is also possible to combine three different tables. – Laurent Dudok de Wit Sep 25 '13 at 20:36
  • An illsutration for the contents in Line 1 : number of students in class A \newline total number of student \newline percentage of students – Laurent Dudok de Wit Sep 25 '13 at 20:38
6

If I'm understanding correctly, this is one way to go. I assumed that the list of class is given somewhere either an extra table column or a list defined as below and indexed by pgfmath function. And I'm hoping that every section has equal (here three) number of rows.

\documentclass{standalone}
\usepackage{pgfplotstable}
\pgfplotstableread[header=false]{
1.23 2.34
2.45 6.54
4.56 4.43
1.23 2.34
2.45 6.54
4.56 4.43
1.23 2.34
2.45 6.54
4.56 4.43
}\mytable
\newcounter{mycounter}
\setcounter{mycounter}{0}
\def\myclasslist{{"Class A","Class C","Class DDD"}}

\begin{document}
\pgfplotstabletypeset[
create on use/class/.style ={create col/set={}},
columns={class,[index]0,[index]1},
display columns/1/.style={column name=Column 1},
display columns/2/.style={column name=Column 2},
columns/class/.style={string type,
    column name={},
    assign cell content/.code={%
        \pgfmathtruncatemacro\temp{Mod(\pgfplotstablerow,3)}%
        \ifnum\temp=0\relax\pgfmathparse{\myclasslist[\value{mycounter}]}%
        \edef\temp{\pgfmathresult}\stepcounter{mycounter}
        \pgfkeys{/pgfplots/table/@cell content/.expanded=\temp}%
        \fi%
    }}]\mytable
\end{document}

enter image description here

  • This works great. However, is there an easy way to make a \hline every 3 lines ? – Laurent Dudok de Wit Sep 25 '13 at 21:14
  • @Laurent Add every nth row={3[-1]}{after row=\hline} to the options – percusse Sep 25 '13 at 21:23

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.