12

I'm attempting to use \pgfplotstabletypeset but with no luck. I have a CSV file like the following:

Name, Age, stDev
Adam, 15, 3

When I output in this into my PDF, I would like the last two columns be combined such that I end up with 15 +/- 3. I would also like to name that column Age.

16

I don't know how to do it using pgfplotstable, but here's how to do it with datatool:

Edit: Updated to round to 2 d.p.

\documentclass{article}

\usepackage{datatool}

\begin{filecontents*}{test.csv}
Name, Age, stDev
Adam, 15.00201, 3.00601
\end{filecontents*}

\DTLloaddb{mydata}{test.csv}

% Round to 2 dp and display.
% (More efficient to use \dtlround rather than \DTLround as the
% data doesn't involve currencies so no conversion is needed)
\newcommand{\round}[1]{\dtlround{#1}{#1}{2}#1}

\begin{document}

\begin{tabular}{ll}
\bfseries Name & \bfseries Age%
\DTLforeach{mydata}{\Name=Name,\Age=Age,\stDev=stDev}%
{%
  \\\Name & $\round\Age\pm\round\stDev$
}%
\end{tabular}

\end{document}

Result:

Image of result

  • just a side question, how would you control the precision? in my actual csv file, there are 4 d.p., I need only 2 – sachinruk Jul 9 '13 at 0:58
  • @Sachin_ruk I've edited the example to include rounding to 2 d.p. – Nicola Talbot Jul 9 '13 at 7:25
16

You need to create a column specification for that.

\documentclass{article}
\usepackage{pgfplotstable}


\pgfplotstableread[col sep=comma]{
Name, Age, stDev
Adam, 15, 3
Eve, 12, 1
}\mytable

\begin{document}
\pgfplotstabletypeset[
create on use/unc age/.style={
    create col/assign/.code={%
        \edef\entry{\thisrow{Age}$\pm$\thisrow{stDev}}
        \pgfkeyslet{/pgfplots/table/create col/next content}{\entry}
    }
},
columns={Name,unc age},
columns/unc age/.style={column name=Age},
string type
]\mytable

\end{document}

enter image description here

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.