5

I am trying to create a new column from another one by subtracting from each row the first one. Then I want to create another column with the relative difference.

\documentclass{article}
\usepackage{pgfplotstable}
\begin{document}
\pgfplotstableread{
TotalDistance
35089
35182
35410
35523
35694
35789
35895
35984
36006
36068
}{\mytable}
\pgfplotstabletypeset[
    columns={TotalDistance,LocalDistance},
    create on use/LocalDistance/.style={
        create col/expr={\thisrow{TotalDistance} - 35089},
    },
    create on use/DifferenceDistance/.style={
        create col/expr={\thisrow{LocalDistance} - \prevrow{LocalDistance}},
    },
    ]{\mytable}
\end{document}

My goal is to obtain:

  TD  | LD  | DD
------+-----+----
35089 |   0 |   0
35182 |  93 |  93
35410 | 321 | 228
35523 | 434 | 113
35694 | 605 | 171
35789 | 700 |  95
35895 | 806 | 106
35984 | 895 |  89
36006 | 917 |  22
36068 | 979 |  62

The problems so far are:

  1. How to avoid to hardcode 35089, but instead read it from the first cell?
  2. I get errors Unknown function `LocalDistance__column_not_found' when I try to typeset the column DifferenceDistance, even if the column LocalDistance would be created without problems.
  • 3
    You need to get the LD row in place to be able to refer to \prevrow. You can use \pgfplotstablecreatecol instead. – percusse May 9 '14 at 20:35
3
  1. You can read the first cell value with \pgfplotstablegetelem{<row>}{<col>}\of\mytable. The return value will be written to \pgfplotsretval.

  2. As proposed by percusse, here is an example using \pgfplotstablecreatecol instead of \pgfplotstabletypeset to add columns to the table:

enter image description here

\documentclass{article}
\usepackage{pgfplotstable}

\begin{document}

\pgfplotstableread{
TotalDistance
35089
35182
35410
35523
35694
35789
35895
35984
36006
36068
}{\mytable}

% read the first cell value -> \pgfplotsretval
\pgfplotstablegetelem{0}{TotalDistance}\of\mytable

% add column LocalDistance
\pgfplotstablecreatecol
    [expr={\thisrow{TotalDistance} - \pgfplotsretval}]
    {LocalDistance}{\mytable}

% add column DifferenceDistance
\pgfplotstablecreatecol
    [expr={\thisrow{LocalDistance} - \prevrow{LocalDistance}}]
    {DifferenceDistance}{\mytable}

% typeset the table
\pgfplotstabletypeset
    [columns={TotalDistance,LocalDistance,DifferenceDistance}]
    {\mytable}

\end{document}

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.