[Tex/LaTex] tabular, siunitx and input – `Extra }, or forgotten $.`

errorsinputsiunitxtables

I'm trying to include data into a tabular environment

\usepackage{booktabs}
\usepackage{siunitx}

\begin{table}
  \begin{center}
    \begin{tabular}{SSSS}
      \toprule
      {$na^3$} & {$V_0/T_c^0$} & {$T_c/T_c^0$} & {$\nu$} \\
      \midrule
      \input{../results/110718/all.tex}
      \bottomrule
    \end{tabular}
    \caption{bla}
  \end{center}
\end{table}

On compiling I get the error message Extra }, or forgotten $.

When I insert all.tex manually, it works:

% ...      
      \midrule
      0 & 0 & 0.99959(72) & 0.882(44) \\
      % ...
      5e-3 & 2 & 1.02710(66) & 0.757(73) \\
      5e-3 & 7 & 0.53844(100) & 0.75(10) \\
      \bottomrule
% ...

Just copy paste, all.tex already contains the ampersands and backslashes.

When I don't use siunitx, it works, too:

% ...
    \begin{tabular}{cccc}
      \toprule
      {$na^3$} & {$V_0/T_c^0$} & {$T_c/T_c^0$} & {$\nu$} \\
      \midrule
      \input{../results/110718/all.tex}
      \bottomrule
    \end{tabular}
% ...

booktabs doesn't seem to cause the issue. It still doesn't work when I remove the booktabs rules.

The file all.tex is generated automatically. Thus, including it manually is not really an option. And because of the nice formatting capabilities of siunitx I would really like to use it.

I'm using TeX-Live 2011-05-26.

What causes this error and how can I solve this problem?

Best Answer

As Mikael has commented, the problem here is to do with getting the numbers expanded. In standard mode, siunitx needs to collect up the number before it typesetting it. The trick is to use an additional column. This will work without any extra packages:

\documentclass{article}
\usepackage{booktabs}
\usepackage{siunitx}
\begin{filecontents*}{all.tex}
& 1.1 & 2.2 & 3.3 & 4.4 \\
\end{filecontents*}
\begin{document}
\begin{table}
  \begin{center}
    \begin{tabular}{l@{}SSSS}
      \toprule
      & {$na^3$} & {$V_0/T_c^0$} & {$T_c/T_c^0$} & {$\nu$} \\
      \midrule
      \input{all.tex}
      \bottomrule
    \end{tabular}
    \caption{bla}
  \end{center}
\end{table}
\end{document}

There is an example in the siunitx manual using datatool which does a similar thing but uses data in CSV format. Adapting it here:

\documentclass{article}
\usepackage{booktabs}
\usepackage{datatool,siunitx}
\begin{filecontents*}{all.csv}
valuea,valueb,valuec,valued
1.1,2.2,3.3,4.4
\end{filecontents*}
\DTLloaddb{data}{all.csv}
\begin{document}
\begin{table}
  \begin{center}
    \begin{tabular}{SSSS@{}l}
      \toprule
      {$na^3$} & {$V_0/T_c^0$} & {$T_c/T_c^0$} & {$\nu$}  &
        \DTLforeach{data}
          {\myvaluea=valuea,\myvalueb=valueb,\myvaluec=valuec,\myvalued=valued}{%
          \DTLiffirstrow {\\ \midrule}{\\}%
          \myvaluea & \myvalueb & \myvaluec & \myvalued &
        }
      \\
      \bottomrule
    \end{tabular}
    \caption{bla}
  \end{center}
\end{table}
\end{document}

If you do not want to use an additional column, then turn off the siunitx number parser:

\documentclass{article}
\usepackage{booktabs}
\usepackage{siunitx}
\begin{filecontents*}{all.tex}
1.1 & 2.2 & 3.3 & 4.4 \\
\end{filecontents*}
\begin{document}
\begin{table}
  \sisetup{parse-numbers = false}
  \begin{center}
    \begin{tabular}{SSSS}
      \toprule
      \multicolumn{1}{c}{$na^3$} & \multicolumn{1}{c}{$V_0/T_c^0$} & 
        \multicolumn{1}{c}{$T_c/T_c^0$} & \multicolumn{1}{c}{$\nu$} \\
      \midrule
      \input{all.tex}
      \bottomrule
    \end{tabular}
    \caption{bla}
  \end{center}
\end{table}
\end{document}
Related Question