16

I have a table, read from a file, and I would like to display only rows which have a specific value in a given column.

Here is an ECM:

\documentclass{minimal}
\usepackage{pgfplotstable}
\begin{document}
\pgfplotstableread{
  num       value
  1         2
  2         5
  1         3
  3         2
  1         4
  2         1
}\data
\pgfplotstabletypeset[row predicate/.code={%
  \ifnum\pgfplotstablegetelem{#1}{num}\of{\data}=1\relax
  \else\pgfplotstableuserowfalse\fi}]{\data}
\end{document}

I would expect to seen only rows whose num value is 1. But I get an error when compiling:

ERROR: Missing number, treated as zero.

--- TeX said ---

                   \begingroup 
l.14 ...after\pgfplotstableuserowfalse\fi}]{\data}

Do you have any idea on what to do so that only specific rows are displayed?

mikael-s
  • 395

1 Answers1

15

You're almost there! However, the macro \pgfplotstablegetelem doesn't "evaluate" to the value you're after, but rather stores it in a macro called \pgfplotsretval. So your code works if you write it like

\documentclass{article}
\usepackage{pgfplotstable}
\begin{document}
\pgfplotstableread{
  num       value
  1         2
  2         5
  1         3
  3         2
  1         4
  2         1
}\data
\pgfplotstabletypeset[row predicate/.code={%
  \pgfplotstablegetelem{#1}{num}\of{\data}
  \ifnum\pgfplotsretval=1\relax
  \else\pgfplotstableuserowfalse\fi}]{\data}
\end{document}

Jake
  • 232,450
  • Gosh I had the pgfplotstable manual opened on the page talking about pgfplotstablegetelem, and I don't know how I managed not to see it! Thanks a lot for your help! – mikael-s Feb 08 '12 at 14:48
  • How to do this when some cells contain text? – u17 Apr 29 '19 at 16:21