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.

I am trying to filter points according to its x values. The data may be from a table-formatted text file, or directly typed in coordinates{...}. I tried to use x filter/.code, but for data from text file it seems fine, while the same code yield compiling error if the data are directly typed through coordinates{...}.

\addplot[scatter, only marks] table[x=xx, y=yy, col sep=comma]{tmp.txt}; 
% Seems working

vs

\addplot[scatter, only marks] coordinates{(0,0) (1,1) (1,1.5) (2,2)}; 
%|16 error| Missing = inserted for \ifnum. Y ...s] coordinates{(0,0) (1,1) (1,1.5) (2,2)};

I suspect it might be my problematic usage of \pgfmathresult in x filter, but not sure how to fix. The following are the MWE along with the compilation error. Assuming file tmp.txt is:

xx, yy
0,0 
1,1 
1,1.5 
2,2 

And the code is:

\documentclass{article}
\usepackage{pgfplots}   

\begin{document}

\begin{tikzpicture}[scale=0.8]
\begin{axis}[
  x filter/.code= {
    \ifnum\pgfmathresult=1
      \def\pgfmathresult{}
    \fi
 }]
  % This seems working fine
  \addplot[scatter, only marks] table[x=xx, y=yy, col sep=comma]{tmp.txt}; 

  % This cause compilation error, which is
  % |16 error| Missing = inserted for \ifnum. Y ...s] coordinates{(0,0) (1,1) (1,1.5) (2,2)};

  %\addplot[scatter, only marks] coordinates{(0,0) (1,1) (1,1.5) (2,2)};
\end{axis} 
\end{tikzpicture}

\end{document}
share|improve this question

1 Answer 1

up vote 2 down vote accepted

It's because coordinates are handled via internal floating point representation which is Klingon for me such 1.1Y0.e1 (or something like that) so you see that Y in the error message. The solution is to convert it to regular decimal.

Also \ifnum works only with integers but pgfmath tends to spit out 1.0 even for integer 1 so better make a dimension comparison to get rid of the complications.

\documentclass{article}
\usepackage{pgfplots}   

\pgfplotstableread{
xx yy
0 0
1 1
1 1.5
2 2
}\mytable
\begin{document}

\begin{tikzpicture}[scale=0.8]
\begin{axis}
\addplot[scatter, only marks,  x filter/.code= {
    \ifnum\pgfmathresult=1
      \def\pgfmathresult{}
    \fi
 }] table[x=xx, y=yy]{\mytable}; 

  \addplot+[no marks,  x filter/.code={
\pgfkeys{/pgf/fpu,/pgf/number format/.cd,fixed,verbatim}
\pgfmathprintnumberto{\pgfmathresult}{\mytempvar}
    \ifdim\mytempvar pt=1pt%
      \def\pgfmathresult{}
    \fi
}] coordinates{(0,0) (1,1) (1,1.5) (2,2)};
\end{axis} 
\end{tikzpicture}
\end{document}

enter image description here

share|improve this answer
    
No wonder the "Y" error in my code. By the way, I assume \pgfmathresult refer to this value: \pgfkeys{/pgf/fpu,/pgf/number format/.cd,fixed,verbatim}, which decode numbers like this "1.1Y0.e1". But could you briefly explain \pgfkeys{...} and its parameters here? –  Causality Aug 31 '12 at 0:15
    
@Causality Actually no. \pgfmathresult corresponds to that weird number representation. And \pgfkeys sets how the number should be printed after conversion, you can choose for example sci instead of fixed to print as the scientific output x 10^z. If you check the manual of pgfplotstable manual these are explained much better than I would do. –  percusse Aug 31 '12 at 0:20
    
perfect. Thanks. –  Causality Aug 31 '12 at 0:31
    
Meanwhile, is it possible to use logical operators in "ifdim" such as "ifdim \myval pt=5pt or \myval pt >8pt"? –  Causality Aug 31 '12 at 0:43
    
@Causality Yes you can use > and <. –  percusse Aug 31 '12 at 0:47

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.