2

I have a text file with 2 columns. The first column is the principal value and the second column is the uncertainty. How to read and display them together in a single column in a table using pgfplotstable ?

What I have now:

\documentclass[varwidth,border=1pt]{standalone}
\usepackage{pgfplotstable}
\usepackage{booktabs}
\usepackage{siunitx}

\begin{document}

\begin{table}[h!]
\begin{center}
    \caption{Table caption}
    \label{table1}
    \pgfplotstabletypeset[
    display columns/0/.style={     
        column name=Voltage (\si{\volt}), % name of first column
    },        
    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}, % rule at bottom
    ]{table1.txt} % filename/path to file
\end{center}
\end{table} 

\end{document}

And the table1.txt contains

1.0000000e+01   3.4916800e-01   
2.0000000e+01   3.6918400e-01   
3.0000000e+01   3.8920000e-01   
4.0000000e+01   4.0921600e-01   
5.0000000e+01   4.2923200e-01  

This would produce:

enter image description here

What I want is:

enter image description here

2

You could use Datatool instead of pgfplotstable.

enter image description here

\documentclass[varwidth,border=1pt]{standalone}
\usepackage{pgf}
\usepackage{booktabs}
\usepackage{siunitx}
\usepackage{datatool}
\DTLsetseparator{   }
\DTLloaddb[noheader,keys={Voltage,Error}]{voltages}{test.dat}
\begin{document}
\begin{table}[h!]
\begin{center}
    \caption{Table caption}
    \label{table1}
    \begin{tabular}{c}
    \toprule Voltage [\si{\volt}] 
    \DTLforeach{voltages}{\voltage=Voltage,\error=Error}
    {\DTLiffirstrow{\\ \midrule}{\\}\pgfmathprintnumber\voltage~$\pm$~\pgfmathprintnumber\error}
    \\\bottomrule
    \end{tabular}
\end{center}
\end{table}
\end{document}
  • Hi @daniel. Thanks for your answer. However I encountered the following error. ! Package datatool Error: Can't assign \error : there is no key Error' in data base voltages'. ! ! Package PGF Math Error: Could not parse input '1.0000000e+01 3.4916800e-01' as a floating point number, sorry. The unreadable part was near '3.4916800e-01 ' .. – Indra Yudhistira Jun 9 '15 at 13:54
  • 1
    I replaced the three spaces with commas in my file. I added \DTLsetseparator{ } to my solution, that should fix the problem. – Daniel Schlegel Jun 9 '15 at 14:06
  • Hi @Daniel, i tried to have a second column with also has uncertainty (the text file now has 4 column). I tried to change the following lines in the code but it doesn't work as expected. \DTLloaddb[noheader,keys={Voltage,Error,Voltageb,Errorb}]{voltages}{table2.txt} \DTLforeach{voltages}{\voltage=Voltage,\error=Error,\voltageb=Voltageb,\errorb=Errorb} {\DTLiffirstrow{\\ \midrule}{\\} \pgfmathprintnumber\voltage~$\pm$~\pgfmathprintnumber\error} & \pgfmathprintnumber\voltageb~$\pm$~\pgfmathprintnumber\errorb} Could you point me what's wrong with this ? – Indra Yudhistira Jun 10 '15 at 5:18
  • 1
    There is an } after \error which shouldn't be there. Did you add the new column to the tabular environment? – Daniel Schlegel Jun 10 '15 at 6:50
  • Hi @Daniel. Thanks for spotting the } mistake. Now it works. – Indra Yudhistira Jun 10 '15 at 10:12

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.