Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question

1 Answer 1

up vote 7 down vote accepted

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}
share|improve this answer
    
That's so simple I never would have thought about it :-) –  Archibald Oct 30 '12 at 12:41

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.