[Tex/LaTex] pgfplotstableread fails to read the input file

includepgfplotstabletables

I am trying to typeset two different tables in a moderately large document. The data for the tables are written as comma-separated values in two different .csv files. I am including the different sections of the document into a main file using \include{section-n}. In each file to be included, I read the the table data with \pgfplotstableread{table-n}{\data-n} and then typeset it as in the MWE.

I am able to typeset the first table without problems, but once it tries to read the second table it fails, entering into an infinite loop. Once I break the process it spits out the following error:

ERROR: Use of \pgfplotstableread@checkspecial@line@@ doesn't match its
definition.

— TeX said — \pgfplotstable@LINE ->$S
igma$,15,,,,1057 l.315 \pgfplotstableread{predevCN2.csv}{\CNPre2}
^^M

— HELP — It's probably one of the picture-drawing commands, and you have used the wrong syntax for specifying an argument. If it's
\@array that doesn't match its definition, then there is something
wrong in an @-expression in the argument of an array or tabular
environment—perhaps a fragile command that is not \protect'ed.

Now, what is going on?

Here's my best attempt at a MWE which captures my intentions, yet it fails in a completely different way than my main file. I'm also not sure about why it fails, but it seems to be related.

\documentclass{article}

\usepackage{lipsum}
\usepackage{filecontents}
\usepackage{booktabs}
\usepackage{array}
 \usepackage{pgfplots}
 \pgfplotsset{compat=1.7}
\usepackage{pgfplotstable}
\pgfplotstableset{
  col sep=comma,
  every head row/.style={%
    before row=\toprule,%
    after row=\midrule},%
  every last row/.style={%
    after row=\bottomrule}%
}

\begin{filecontents}{firstTable.csv}
A,B,C,D,E
1,2,3,4,5
$\Sigma$i,ii,iii,iv,v
\end{filecontents}

\begin{filecontents}{secondTable.csv}
a,b,c,d,e
1,2,3,4,5
I,II,III,IV,V
\end{filecontents}

\begin{filecontents}{section1.tex}
  Here's a nice table:

  \pgfplotstableread{firstTable.csv}{\loadeddata1}

  \begin{table}
    \centering
    \pgfplotstabletypeset[%
       columns={A,B,C,D,E}%
       ]{\loadeddata1}
    \caption{Simple caption for a simpler table.}
    \label{tab:table1}
  \end{table}
\end{filecontents}


\begin{document}
\section{Here goes section 1}
\lipsum[1]
\include{section1}


\section{Section 2}
And here we put another table.
 \pgfplotstableread{secondTable.csv}{\loadeddata2}

 \begin{table}
   \centering
   \pgfplotstabletypeset[%
     columns={a,b,c,d,e}%
     ]{\loadeddata2}
     \caption{Fancy caption for a simple table.}
     \label{tab:table2}
 \end{table}



\end{document}


%%%% Local Variables: 
%%%% mode: latex
%%%% TeX-engine: xetex
%%%% End:

Best Answer

There are three problems with your code:

  1. You're using macro names with numbers to store the tables (\loadeddata1). That doesn't work, you have to use letters (like \loadeddataone).

  2. Your tables contain letters, but \pgfplotstabletypeset by default assumes that tables contain numbers. To typeset tables that contain text, use the string type key.

  3. You have a macro in your first table. That needs to be protected from the parser by wrapping it in {...}:

Here's the corrected code:

\documentclass{article}

\usepackage{lipsum}
\usepackage{filecontents}
\usepackage{booktabs}
\usepackage{array}
 \usepackage{pgfplots}
 \pgfplotsset{compat=1.7}
\usepackage{pgfplotstable}
\pgfplotstableset{
  col sep=comma,
  every head row/.style={%
    before row=\toprule,%
    after row=\midrule},%
  every last row/.style={%
    after row=\bottomrule}%
}

\begin{filecontents}{firstTable.csv}
A,B,C,D,E
1,2,3,4,5
{$\Sigma$i},ii,iii,iv,v
\end{filecontents}

\begin{filecontents}{secondTable.csv}
a,b,c,d,e
1,2,3,4,5
I,II,III,IV,V
\end{filecontents}

\begin{filecontents}{section1.tex}
  Here's a nice table:

  \pgfplotstableread{firstTable.csv}{\loadeddataone}

  \begin{table}
    \centering
    \pgfplotstabletypeset[%
       columns={A,B,C,D,E},
       string type
       ]{\loadeddataone}
    \caption{Simple caption for a simpler table.}
    \label{tab:table1}
  \end{table}
\end{filecontents}


\begin{document}
\section{Here goes section 1}
\lipsum[1]
\include{section1}


\section{Section 2}
And here we put another table.
 \pgfplotstableread{secondTable.csv}{\loadeddatatwo}

 \begin{table}
   \centering
   \pgfplotstabletypeset[%
     columns={a,b,c,d,e},
     string type
     ]{\loadeddatatwo}
     \caption{Fancy caption for a simple table.}
     \label{tab:table2}
 \end{table}



\end{document}
Related Question