1

I'm currently using the pgfplots package to render tables from .csv files. The files are generated automatically.

I often use tables as follows:

\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\usepackage{pgfplotstable}


\makeatletter
\pgfplotstableset{
    discardrow if not/.style 2 args={
        row predicate/.code={
            \def\pgfplotstable@loc@TMPd{\pgfplotstablegetelem{##1}{#1}\of}
            \expandafter\pgfplotstable@loc@TMPd\pgfplotstablename
            \edef\tempa{\pgfplotsretval}
            \edef\tempb{#2}
            \ifx\tempa\tempb
            \else
                \pgfplotstableuserowfalse
            \fi
        }
    }
}
\makeatother


\usepackage{filecontents}
\begin{filecontents}{data.txt}
 Category  Value    Score
 A   2    1.1
 A   10   2.2
 A   20   1.1 
 A   30   4.2
 A   40   4.6
 B   2    1.2
 B   10   1.1
 B   20   1.1
 B   30   1.2 
 B   40   1.1
\end{filecontents}

\pgfplotstableread{data.txt}\loadedtable

\begin{document}
\pgfplotstabletypeset[columns={Value, Score},
                 columns/Category/.style={string type},
                  discardrow if not={Category}{A}]\loadedtable
                 
\pgfplotstabletypeset[columns={Value, Score},
                 columns/Category/.style={string type},
                  discardrow if not={Category}{B}]\loadedtable
  
\end{document}

Now I'd like to merge these two tables "Category" row and introduce virtual columns, i.e. the resulting table should only contain:

Value Score A Score B
2        1.1   1.2
10       2.2   1.2
20       1.1   1.1
30       4.2   1.2
40       4.6   1.1

Can we do that with pgfplots?

Thanks for any advice!

1 Answer 1

1

In order to avoid spurious spaces, remember to add a % sing at the end of the lines marked with <---- inside the definition of \pgfplotstableset.

enter image description here

\begin{filecontents}[overwrite]{data.txt}
 Category  Value    Score
 A   2    1.1
 A   10   2.2
 A   20   1.1 
 A   30   4.2
 A   40   4.6
 B   2    1.2
 B   10   1.1
 B   20   1.1
 B   30   1.2 
 B   40   1.1
\end{filecontents}

\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\usepackage{pgfplotstable}

\makeatletter
\pgfplotstableset{
 discardrow if not/.style 2 args={
  row predicate/.code={%  <-----
   \def\pgfplotstable@loc@TMPd{\pgfplotstablegetelem{##1}{#1}\of}%  <-----
   \expandafter\pgfplotstable@loc@TMPd\pgfplotstablename%  <-----
   \edef\tempa{\pgfplotsretval}%  <-----
   \edef\tempb{#2}%  <-----
   \ifx\tempa\tempb
   \else
   \pgfplotstableuserowfalse
   \fi
  }
 }
}
\makeatother

\pgfplotstableread{data.txt}\loadedtable

\begin{document}
 
 \pgfplotstabletypeset[columns={Value, Score},
 columns/Score/.style={column name=Score A},
 discardrow if not={Category}{A}
 ]\loadedtable
 % <- Add this or remove the line
 \pgfplotstabletypeset[columns={Score},
 columns/Score/.style={column name=Score B},
 discardrow if not={Category}{B}
 ]\loadedtable
 
\end{document}

Alternatively, this gives the same result, but without usgin \pgfplotstableset:

\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\usepackage{pgfplotstable}

\pgfplotstableread{data.txt}\loadedtable

\begin{document}

\pgfplotstabletypeset[columns={Value, Score},
columns/Score/.style={column name=Score A},
skip rows between index={5}{10},
]\loadedtable
% <- Add this or remove the line
\pgfplotstabletypeset[columns={Score},
columns/Score/.style={column name=Score B},
skip rows between index={0}{5},
]\loadedtable

\end{document}
1
  • Thanks! That is indeed the solution I was looking for. Amazing!
    – erstwhile
    Mar 11, 2021 at 14:18

You must log in to answer this question.

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