5

In this post I found some working code that I am now using quite frequently:

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

However, I would like to make a \newcommand that implements this, but where the table it should use (\data in this example) is an argument.

Of course, this conflicts with #1's other meaning, being the first argument of my function. Is there a remedy?

Archibald
  • 463

1 Answers1

8

If you're referring to an argument of a macro within a macro (within the /.code, in this case), you'll need to use ##1 instead of #1:

\documentclass{article}
\usepackage{pgfplotstable}
\begin{document}

\pgfplotstableread{
  num       value
  1         2
  2         5
  1         3
  3         2
  1         4
  2         1
}\data

\newcommand{\filteredtable}[2]{
\pgfplotstabletypeset[row predicate/.code={%
  \pgfplotstablegetelem{##1}{num}\of{#1}
  \ifnum\pgfplotsretval=#2\relax
  \else\pgfplotstableuserowfalse\fi}]{#1}
}

\verb|\filteredtable{\data}{1}|

\filteredtable{\data}{1}\vspace{1cm}

\verb|\filteredtable{\data}{2}|

\filteredtable{\data}{2}

\end{document}
Jake
  • 232,450