2

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. The specific value is a string (e.g. Agricultural). I am able to do it using the following code if the value is an integer:

\pgfplotstabletypeset[row predicate/.code={%
\pgfplotstablegetelem{#1}{variable}\of{test.txt}
\ifnum\pgfplotsretval=3\relax
\else\pgfplotstableuserowfalse\fi}]{test.txt}

The value I am looking to match is "Agricultural." I have tried to use \if and \ifx but this was not successful.

Any input helps! Thanks!

Edit:

MWE:

\documentclass{article}
\usepackage{float}
\usepackage{pgfplotstable}

\begin{document}

\pgfplotstabletypeset[row predicate/.code={%
\pgfplotstablegetelem{#1}{variable}\of{test.txt}
\ifnum\pgfplotsretval=3\relax
\else\pgfplotstableuserowfalse\fi}]{test.txt}

\end{document}

test.txt:

variable    2016    2017    2018
2   0.16519 0.1519385   0.1924408
Agricultural    0.4751213   0.3346962   0.2863951
3   0.0868261   0.1613692   0.2666448
3   0.4510618   0.5404901   0.5006156
  • 1
    @Raaja I added a fully compilable MWE and input file test.txt which works if I want to only display the rows with a value of "3" in the column "variable." I would like to be able to only display the rows with a value of "Agricultural" in the column "variable." – gatornuke Jan 09 '19 at 16:55

1 Answers1

2

You can use string type style for column variable and then test it as a string, like in this egreg's answer.

\documentclass{article}
\usepackage{float}
\usepackage{pgfplotstable}
\usepackage{etoolbox}
\usepackage{filecontents}
\begin{filecontents*}{test.txt}
variable    2016    2017    2018
2   0.16519 0.1519385   0.1924408
Agricultural    0.4751213   0.3346962   0.2863951
3   0.0868261   0.1613692   0.2666448
3   0.4510618   0.5404901   0.5006156
\end{filecontents*}

\begin{document}
\pgfplotstableread{test.txt}\mytable
\pgfplotstabletypeset[
  columns/variable/.style={string type},
  row predicate/.code={%
  \pgfplotstablegetelem{#1}{variable}\of{\mytable}
  % code from https://tex.stackexchange.com/a/22924/101651
  \ifnum\pdfstrcmp{\pgfplotsretval}{Agricultural}=0\relax
  \else\pgfplotstableuserowfalse\fi}
]{\mytable}

\end{document}

enter image description here

CarLaTeX
  • 62,716