1

Using pgfplotstable library to define a table from a data file containing numeric and alphanumeric values, I get this error "! Package PGF Math Error: Could not parse input 'B' as a floating point number, "while compiling this short MWE example .. don't understand what's wrong with this example.

\documentclass{article}
%
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{pgfplots,pgfplotstable,makecell,booktabs}
\begin{document}
    \begin{table}
      \pgfplotstabletypeset[%
    columns/0/.style={column name=\makecell{Col1\\{}},
        string type},
    columns/1/.style={column name=\makecell{Col2\\{}},
        column type=r,fixed},
    columns/2/.style={column name=\makecell{$GD$\\{}},
        column type=r,sci,zerofill},
    columns/3/.style={column name=\makecell{$\Delta$\\{}},
        column type=r,sci,zerofill},
    every head row/.style={
        before row={\toprule}, % have a rule at top
        after row={\midrule} % rule under units
    },
    skip first n=1
    ]
    {test_data.txt} % filename/path to file
   \end{table}
\end{document}

test_data.txt file is:

Iter X Y Z
A 12.2 13.4 14.5
B 114.5 345.6 456.5
C 45 5 5
2

The skip first n means that the first row of your data file is skipped, but because the first column has a non-numerical entry, the A-row is read as a header row with column names. When you have a table that has column names, you cannot use column indices when you set the column styles, i.e. columns/<integer>/.style. The manual says:

If your tables don’t have column names, you can simply use integer indices instead of <column name> to refer to columns. If you have column names, you can’t set column styles using indices

Because you have column names, column/0/.style will influence the column with the name 0, and not the index 0. There is no such column, and as a result the first column does not get the string type key, and the B found there is parsed as a number. Which it is not.

Possible fixes:

  • Add header=false. This means the A-row will not be parsed as a header row, and you can use indices to refer to the columns.
  • Remove skip first n=1, and use Iter, X, Y and Z (i.e. the column names read from the first line) instead of the integer indices.
  • Remove skip first n=1 and use display columns/<integer index>/.style instead. (Display columns act on the output columns instead of input columns.)

enter image description here

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc} 
\usepackage{pgfplotstable,makecell,booktabs}
\begin{document}

      \pgfplotstabletypeset[%
    columns/0/.style={column name=\makecell{Col1\\{}},
        string type},
    columns/1/.style={column name=\makecell{Col2\\{}},
        column type=r,fixed},
    columns/2/.style={column name=\makecell{$GD$\\{}},
        column type=r,sci,zerofill},
    columns/3/.style={column name=\makecell{$\Delta$\\{}},
        column type=r,sci,zerofill},
    every head row/.style={
        before row={\toprule}, % have a rule at top
        after row={\midrule} % rule under units
    },
    every last row/.style={after row=\bottomrule},
    header=false, % <---- add this
    skip first n=1
    ]
    {test_data.txt} 

\bigskip

      \pgfplotstabletypeset[%
    columns/Iter/.style={column name=\makecell{Col1\\{}}, % <-- Iter
        string type},
    columns/X/.style={column name=\makecell{Col2\\{}}, % <-- X
        column type=r,fixed},
    columns/Y/.style={column name=\makecell{$GD$\\{}}, % <-- Y
        column type=r,sci,zerofill},
    columns/Z/.style={column name=\makecell{$\Delta$\\{}}, % <-- Z
        column type=r,sci,zerofill},
    every head row/.style={
        before row={\toprule}, % have a rule at top
        after row={\midrule} % rule under units
    },
    every last row/.style={after row=\bottomrule}
%    skip first n=1 % <-- remove
    ]
    {test_data.txt} 

\bigskip


      \pgfplotstabletypeset[%
    display columns/0/.style={column name=\makecell{Col1\\{}}, % <-- display columns
        string type},
    display columns/1/.style={column name=\makecell{Col2\\{}}, % <-- display columns
        column type=r,fixed},
    display columns/2/.style={column name=\makecell{$GD$\\{}}, % <-- display columns
        column type=r,sci,zerofill},
    display columns/3/.style={column name=\makecell{$\Delta$\\{}}, % <-- display columns
        column type=r,sci,zerofill},
    every head row/.style={
        before row={\toprule}, % have a rule at top
        after row={\midrule} % rule under units
    },
    every last row/.style={after row=\bottomrule}
    %skip first n=1 % <-- remove
    ]
    {test_data.txt} % filename/path to file

\end{document}
  • Is this totally accurate? In the section on columns, the pgfplotstable guide says "If your tables don’t have column names, you can simply use integer indices instead of <column name> to refer to columns. If you have column names, you can’t set column styles using indices." The display columns command is there so that you can refer to the column indices in the output table. – Stephen Bosch Jun 24 '18 at 18:35
  • 1
    @StephenBosch I don't think I noticed that part, I suppose you're right that it's not entirely accurate. In this case of course the table does have column names, so the last sentence of your quote applies. – Torbjørn T. Jun 24 '18 at 18:45
  • @StephenBosch Thanks though, I completely rewrote my answer, as I realized I hadn't really understood what was going on. Should be better now. – Torbjørn T. Jun 24 '18 at 19:30

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.