
Ich versuche, aus einer anderen Spalte eine neue zu erstellen, indem ich von jeder Zeile die erste subtrahiere. Dann möchte ich eine weitere Spalte mit der relativen Differenz erstellen.
\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}
Mein Ziel ist:
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
Die bisherigen Probleme sind:
- Wie kann man die Festcodierung vermeiden
35089
und es stattdessen aus der ersten Zelle lesen? - Ich erhalte FehlermeldungenUnbekannte Funktion `LocalDistance__column_not_found'wenn ich versuche, die Spalte DifferenceDistance zu setzen, auch wenn die Spalte LocalDistance problemlos erstellt würde.
Antwort1
Den ersten Zellenwert können Sie mit auslesen
\pgfplotstablegetelem{<row>}{<col>}\of\mytable
. Der Rückgabewert wird in geschrieben\pgfplotsretval
.Wie vorgeschlagen vonSchlagzeug, hier ist ein Beispiel, bei dem
\pgfplotstablecreatecol
anstelle von verwendet wird\pgfplotstabletypeset
, um Spalten zur Tabelle hinzuzufügen:
\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}