5

I would like to implement macros to generate multiple tables in a document. Some inspiration comes from here: usage-of-longtable-or-equivalent-and-pgfplotstabletypeset

The input csv file and generated tables should contain chars and floats.

A non-macro implementation works ok, for instance to generate a single table I can do the following without a problem:

\documentclass{article} 

\usepackage{pgfplotstable}
\usepackage{array}
\usepackage{booktabs}
\usepackage{longtable}
\usepackage{etoolbox}

\begin{filecontents*}{test.csv}
    A,B,C
    0,0,T
    1,1,T
    2,2,T
    3,3,T
    4,4,T
    5,5,T
\end{filecontents*}
\pgfplotstableset{
    begin table=\begin{longtable},
    col sep=comma,
    string type,
    columns={A,B,C},
    columns/A/.style={int detect,column name=A},
    columns/B/.style={int detect,column name=B},
    columns/C/.style={int detect,column name=C},
    every head row/.style={before row=\toprule,after row=\midrule\endhead},
    every last row/.style={after row=\bottomrule},
    end table=\caption{\mycaption}\end{longtable}
}
\begin{document}
\pgfplotstabletypesetfile{test.csv}
\end{document}

The alternative macro route works if the csv file contains only integer/float data, but not when chars are present:

\documentclass{article} 

\usepackage{pgfplotstable}
\usepackage{array}
\usepackage{booktabs}
\usepackage{longtable}
\usepackage{etoolbox}
\begin{filecontents*}{test.csv}
 A,B,C
 0,0,T
 1,1,T
 2,2,T
 3,3,T
 4,4,T
 5,5,T
\end{filecontents*}

\newrobustcmd{\strtable}[2]{
    \pgfplotstableset{
    begin table=\begin{longtable},
    end table=\caption{#2}\end{longtable}
    }
    \pgfplotstableset{columns={A,B,C}}
    \pgfplotstableset{columns/A/.style={int detect,column name=A}}
    \pgfplotstableset{columns/B/.style={int detect,column name=B}}
    \pgfplotstableset{columns/C/.style={int detect,column name=C}}

    \pgfplotstableread[col sep=comma, string type]{#1}\strtable

    \begin{center}
        \pgfplotstabletypeset[
            every head row/.style={before row=\toprule,after row=\midrule\endhead},
                every last row/.style={after row=\bottomrule},
            ]\strtable
    \end{center}
}
\begin{document}   
\strtable{test.csv}{test}
\end{document}

Unfortunately when I run the preceding macro things grind to a halt:

! Package PGF Math Error: Could not parse input 'T' as a floating point number,
 sorry. The unreadable part was near 'T'..

For some reason the csv file is parsed differently in the two cases and trouble arises in the second. I've experimented with the placement of the terms and looked through the pgfplotstable documentation without much luck.

How to implement the macros style with mixed chars and floats in a csv file?

  • Thanks Jake for the clarification: type definitions in .style statements matter! – Buck Thorn Jul 19 '13 at 11:32
4

string type is an output style, not an input one. If you apply it only to the last column, everything works fine:

\documentclass{article} 

\usepackage{pgfplotstable}
\usepackage{array}
\usepackage{booktabs}
\usepackage{longtable}
\usepackage{etoolbox}
\begin{filecontents*}{test.csv}
 A,B,C
 0,0,T
 1,1,T
 2,2,T
 3,3,T
 4,4,T
 5,5,T
\end{filecontents*}

\newrobustcmd{\strtable}[2]{
    \pgfplotstableset{
    begin table=\begin{longtable},
    end table=\caption{#2}\end{longtable}
    }
    \pgfplotstableset{columns={A,B,C}}
    \pgfplotstableset{columns/A/.style={int detect,column name=A}}
    \pgfplotstableset{columns/B/.style={int detect,column name=B}}
    \pgfplotstableset{columns/C/.style={string type,column name=C}}

    \pgfplotstableread[col sep=comma]{#1}\strtable

    \begin{center}
        \pgfplotstabletypeset[
            every head row/.style={before row=\toprule,after row=\midrule\endhead},
                every last row/.style={after row=\bottomrule},
            ]\strtable
    \end{center}
}
\begin{document}   
\strtable{test.csv}{test}
\end{document}
  • Thanks, that did it! I suspected it had to do with the type definition in .style but I missed that in the documentation. – Buck Thorn Jul 19 '13 at 11:29
  • By the way it is a bit strange that an error did not crop up in the non-macro version, that was a source of confusion. – Buck Thorn Jul 19 '13 at 11:30
  • @TryHard: That's because in that version you had string type applied to the whole table, so all entries were treated as characters. – Jake Jul 19 '13 at 11:31
  • Hmm, I thought I had included that in \pgfplotstableread[col sep=comma, string type], but evidently that was not a solution. – Buck Thorn Jul 19 '13 at 11:48
  • @TryHard: That's what I meant with "It's an output style, not an input style". The options you supply to \pgfplotstableread are only used to read the text file and store the contents into a macro, but at that point everything is still a string anyway. string type is only relevant when you want to output the table using \pgfplotstabletypeset. – Jake Jul 19 '13 at 11:52

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.