3

I am trying to improve my workflow in LaTeX using pgfplotstable to also include automatic error calculation.

Right now, when I run a simulation, I save the output to a csv table, which I load into LaTeX using pgfplotstable, e.g. something like

\documentclass{standalone}
\usepackage{pgfplots,pgfplotstable}
\pgfplotsset{compat=newest}

\begin{document}
\pgfplotstableread{
x y
1 1.0
2 2.0
3 3.0
}\sim

\begin{tikzpicture}
    \begin{axis}
        \addplot table[x=x, y=y]\sim;
    \end{axis}
\end{tikzpicture}
\end{document}

However, now I want to compare the result of this simulation to some reference solution (or another method). So is it possible to find the difference between the y-values of two different tables?

I imagine that something like this, should be possble

\documentclass{standalone}
\usepackage{pgfplots,pgfplotstable}
\pgfplotsset{compat=newest}

\begin{document}
\pgfplotstableread{
x y
1 1.0
2 2.0
3 3.0
}\sim

\pgfplotstableread{
x y
1 1.5
2 1.5
3 2.0
}\ref

\begin{tikzpicture}
    \begin{axis}
        \addplot table[x=x, y expr=\thisrow{y}{\sim}-\thisrow{y}{\ref}]; %Not working
    \end{axis}
\end{tikzpicture}
\end{document}

Though note that y expr=\thisrow{y}{\sim}-\thisrow{y}{\ref} does not work as the usage of \thisrow is wrong, the second input is used for output and not input, it is merely to illustrate the idea.

1 Answers1

2

You could just add the y column from the second table to the first one, and then operate with one table as usual.

\documentclass{standalone}
\usepackage{pgfplots,pgfplotstable}
\pgfplotsset{compat=newest}

\begin{document}
\pgfplotstableread{
x y
1 1.0
2 2.0
3 3.0
}\sim

\pgfplotstableread{
x y
1 1.5
2 1.5
3 2.0
}\ref

% https://tex.stackexchange.com/a/166691/121799
% add y column from ref to sim and call it y2
\pgfplotstablecreatecol[
  copy column from table={\ref}{[index] 1},
  ]{y2}{\sim}

% uncomment this to see the table
%\pgfplotstabletypeset{\sim}

\begin{tikzpicture}
    \begin{axis}
         \addplot table[x index=0,
         y expr=\thisrow{y}-\thisrow{y2}]{\sim}; %Now working
    \end{axis}
\end{tikzpicture}
\end{document}

enter image description here