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.

1

2 Answers 2

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

2
  • 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, 2013 at 0:58
  • @Sachin_ruk I've edited the example to include rounding to 2 d.p. Jul 9, 2013 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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .