2

I need to exclude every 0th, 3rd, 6th ... row from a table. \pgfmathparse{Mod(#1, 3) == 0} did not work inside row predicate/.code={}, it gives following error:

! Package PGF Math Error: Sorry, the operation 'Mod' has not yet been implemented in the floating point unit :-( (in 'Mod(0, 3) == 0').

I have copied following MWE from another question. I want to exclude rows according to their indices, not according to their values.

\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}
4

If you want to filter every nth row, you can use \pgfmathparse{int(mod(#1,3))} (note the lower case m) and then use \ifnum\pgfmathresult>0\relax to check whether to use the row or not:

\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}
  \pgfmathparse{int(mod(#1,3))}
  \ifnum\pgfmathresult>0\relax
  \else\pgfplotstableuserowfalse\fi}]{\data}
\end{document}


If you want to filter by the value of a cell, as in your code sample, you'll need to adjust your code slightly:

The \pgfplotstablegetelem macro does not expand to its return value; instead, it writes the return value to a macro called \pgfplotsretval. If you use that macro in the \ifnum condition, it works as expected:

\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}
  • 1
    Very thanks! In my original code, int(.) did not work somehow. I have used this code: row predicate/.code={\pgfmathparse{mod(#1,3)} \pgfmathtruncatemacro{\rem}{\pgfmathresult}\ifnum\rem=0\relax\pgfplotstableuserowfalse\fi}, – Kadir Apr 24 '13 at 7:41

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.