7

In the following MWE, I'm trying to create one on use column based on the definition of another on use column. PGF generates an error in this case:

PGFPlots: reading {test1.dat}
PGFPlots: reading {test2.dat}

! Package PGF Math Error: Unknown function `value__column_not_found' (in 'value
__column_not_found.*0.75').

Although the example (taken from here) is trivial, I would like to keep my original data files separated, but show some of the results together, along with some simple calculations. Is there any way to create a column based on another created column in pgfplotstable?

\documentclass{article}
\usepackage{pgfplotstable}
\pgfplotstableset{
    create on use/lb/.style={
        create col/expr={\thisrow{value}*0.75}
    },
}
\begin{filecontents*}{test1.dat}
parameter
1
2
3
4
\end{filecontents*}
\begin{filecontents*}{test2.dat}
value
0.00034
0.0053
0.07
0.0027
\end{filecontents*}
\begin{document}
\pgfplotstableread{test1.dat}\parameter
\pgfplotstableread{test2.dat}\value
\pgfplotstabletypeset[
    columns={parameter, value, lb},
    columns/parameter/.style={
        column name={Parameter},
    },
    columns/value/.style={
        column name={Default Value},
    },
    create on use/value/.style={create col/copy column from table={\value}{value}},
    columns/lb/.style={column name={Lower Bound}}.
]\parameter
\end{document}
4

The solution is to copy the column into the table outside the \pgfplotstabletypeset macro using the \pgfplotstablecreatecol macro. Any additional columns that are based on the copied column can then be added by the usual create on use option. Jake's answer to an unrelated question put me on the right track.

% arara: pdflatex
\documentclass{article}
\usepackage{pgfplotstable}
\begin{filecontents*}{test1.dat}
parameter
1
2
3
4
\end{filecontents*}
\begin{filecontents*}{test2.dat}
value
0.00034
0.0053
0.07
0.0027
\end{filecontents*}
\begin{document}
\pgfplotstableread{test1.dat}\parameter
\pgfplotstableread{test2.dat}\value
% This gets added
\pgfplotstablecreatecol[copy column from table={\value}{value}]{value}{\parameter}
\pgfplotstablecreatecol[expr={\thisrow{value}/\thisrow{parameter}}]{ub}{\parameter}
\pgfplotstableset{
    create on use/lb/.style={
        create col/expr={\thisrow{ub}*0.6}
    },
}
%
\pgfplotstabletypeset[
    columns={parameter, value, lb, ub},
    columns/parameter/.style={
        column name={Parameter},
    },
    columns/value/.style={
        column name={Default Value},
    },
    columns/lb/.style={column name={Lower Bound}},
]\parameter
\end{document}
  • I believe there is a \foreach option for this, at least using the \pgfkeys version. – John Kormylo Aug 28 '14 at 13:39
  • @JohnKormylo I don't understand how a \foreach loop is relevant here... can you write an answer (if you have the time :-))? – darthbith Aug 28 '14 at 14:04
1

Going through the manual again, the \pgfplotstablemodifyeachcolumnelemen command doesn't seem any more useful than "create col/expr=". But the "set list" approach has some advantages.

\documentclass{article}
\usepackage{pgfplotstable}

\begin{document}

\def\parameters{1,2,3,4}
\def\values{0.00034,0.0053,0.07,0.0027}
%demonstration of how to use \foreach:  expr={} is more elegant.
\def\ub{}
\foreach \i in \values {\pgfmathmultiply{1.25}{\i}%
  \ifx\ub\empty\global\edef\ub{\pgfmathresult}
  \else\global\edef\ub{\ub,\pgfmathresult}
  \fi}

\pgfplotstableset{create on use/parameter/.style={create col/set list/.expanded={\parameters}}}
\pgfplotstableset{create on use/value/.style={create col/set list/.expanded={\values}}}
\pgfplotstableset{create on use/lb/.style={create col/expr={\thisrow{value}*0.75}}}
\pgfplotstableset{create on use/ub/.style={create col/set list/.expanded={\ub}}}

\pgfplotstablenew[columns={parameter,value,lb,ub}]{4}\mytable

\pgfplotstabletypeset[
    columns={parameter, value, lb, ub},
    columns/parameter/.style={
        column name={Parameter},
    },
    columns/value/.style={
        column name={Default Value},
    },
    columns/lb/.style={column name={Lower Bound}},
    columns/ub/.style={column name={Upper Bound}},
]\mytable
\end{document}

table

  • Thank you! Unfortunately, I don't see how this answers the question... the macros \parameter and \value are \deffed, not read using \pgfplotstableread... is there a way to assign a column to a macro? My brief look at the manual didn't turn up anything. – darthbith Sep 3 '14 at 20:35
  • 1
    You can copy a column to a macro using a loop similar to the one shown but using \pgfplotstablegetelem. But if you are reading the data from a table (as opposed to creating it in latex), it doesn't really gain you much. – John Kormylo Sep 3 '14 at 22:06

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.