Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question
2  
It doesn't seem like the S style will allow you to input files (\input isn't expanded before parsing of the contents begin). This table can be quite easily produced by pgfplotstable if you are willing to slightly modify the way all.tex is generated. –  Mikael Öhman Jul 19 '11 at 1:01
add comment

1 Answer 1

up vote 5 down vote accepted

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}
share|improve this answer
    
Thank you very much. I chose the first solution. Adding an additional column to the data file is an easy fix. The typeset result looks exactly identical to including the data manually without an additional column. –  Lemming Jul 19 '11 at 10:17
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.