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 understand the following code:

\pgfplotsset{
    discard if not/.style 2 args={
        x filter/.code={
            \edef\tempa{\thisrow{#1}}
            \edef\tempb{#2}
            \ifx\tempa\tempb
            \else
                \def\pgfmathresult{inf}
            \fi
        }
    }
}

Here is my understanding after reading the manual:

  1. \pgfplotsset is used to set document-wise style.

  2. discard if not/.style 2 args defines a style named discard if not in the namespace /pgfplots and takes two arguments: #1 and #2.

  3. x filter/.code is used to change the behavior of the PGFPLOTS regarding coordinate filtering. Here, we are saying to use x filter for coordinate filtering based on a single coordinate. .code is similar to TeX command: \newcommand. x filter gets an input coordinate as #1 and applies filtering and finally writes the results into the macro \pgfmathresult. If we set \pgfmathresult to inf by \pgfmathresults{inf} this means the coordinate can be discarded (if unbounded coords=discard is set).

  4. \thisrow{#1} returns the active row in #1 column. In other words, \thisrow{#1} = table(active row,column=#1).

  5. \tempa{\thisrow{#1}} defines a macro named tempa which has a value of the currently active table row in the designated column, \tempa = table(active row,column=#1).

  6. \edef refers to expanded definition. It expands the macro to evaluate its value.

  7. \edef\tempa{\thisrow{#1}} evaluate the macro and assigns the value of the active row in the designated column to tempa.

  8. \edef\tempb{#2} assigns the value of the second argument to macro tempb.

  9. \ifx: I couldn't find this in the manual but I am assuming that \ifx\tempa\tempb is equivalent to logical condition: \tempa == \tempb. Maybe we have \ify?

  10. \pgfmathresult{inf}: up to this moment \pgfmathresult has the value of the x coordinate and setting it to inf discards the coordinate if unbounded coords=discard has been set.

  11. \def\pgfmathresult{inf} I couldn't find what exactly \def does but I assume it sets the value of \pgfmathresult to inf which can refer to discarding.

So, discard if not is a name of a style that when is called applies a coordinate discarding based on two input arguments.

I would be thankful if someone could correct me if I have not understood it correctly or give me a better interpretation.

share|improve this question
1  
Might be interesting: tex.stackexchange.com/questions/12668/… –  Christian Feuersänger May 17 at 12:31
    
Wonderful! I have been looking for such a documentation forever. Thanks for sharing it in here. –  A2009 May 17 at 20:09

2 Answers 2

up vote 8 down vote accepted

It is indeed correct but requires a few nuances to be clarified;

  1. \pgfplotsset is used to set key=value pairs specific to the pgfplots key family. It can be seen in the pgfkeys section in the TikZ manual. The main idea to use this is to avoid every time entering /pgfplots/ prefix to each key to be set. TikZ has \tikzset, pgfplotstable has \pgfplotstableset etc. Hence it sets the namespace automatically to pgfplots

  2. discard if not/.style 2 args defines a style named discard if not in the namespace /pgfplots/ and takes two arguments: #1 and #2. Yes and those inputs #1 and #2 are still available inside this style

  3. x filter/.code is used to change the behavior of the PGFPLOTS regarding coordinate filtering. Here, we are saying to use x filter for coordinate filtering based on a single coordinate. .code is a handler which executes a piece of code instead of a style setting up a value or other styles. similar to TeX command: \newcommand.

    x filter holds the code to evaluate the current value and we can alter the value of \pgfmathresult . We use the previously defined #1 and #2 to assess the values. If we set \pgfmathresult to inf by \pgfmathresults{inf} this means the coordinate can be discarded (unbounded coords=discard is set).

  4. \thisrow{#1} returns the active row in #1 column. In other words, \thisrow{#1} = table(active row,column=#1). Yes

  5. \edef\tempa{\thisrow{#1}} defines a macro named tempa which has the expanded value of the currently active table row in the designated column, \tempa = table(active row,column=#1).

  6. \edef refers to expanded definition. It expands the macro to evaluate its value. The difference is that the value of \thisrow{#1} is used immediately upon definition. If \def is used it will wait unexpanded until it is used and use the last value.

  7. \edef\tempa{\thisrow{#1}} evaluate the macro and assigns the value of the active row in the designated column to tempa.The same

  8. \edef\tempb{#2} assigns the value of the second argument to macro tempb.

  9. \ifx: I couldn't find this in the manual but I am assuming that \ifx\tempa\tempb is equivalent to logical condition. It is difficult to explain all the nuances here but for simplicity it is an expanded conditional. It expands the macros in the input stream until it finds two things that cannot be expanded further and compares them.

  10. \pgfmathresult{inf}: up to this moment \pgfmathresult has the value of the x coordinate and setting it to inf discards the coordinate if unbounded coords=discard has been set.

  11. \def\pgfmathresult{inf} I couldn't find what exactly \def does but I assume it sets the value of \pgfmathresult to inf which can refer to discarding.See \edef above These are TeX primitives and explained elsewhere.

So, discard if not is a name of a style that when is called applies a x coordinate filter based on two input arguments.

share|improve this answer

Yes, you understood almost all of the code correctly.

If I remember correctly, that code was first used in the answer to Is it possible to change the color of a single bar when the bar plot is based on symbolic values?. It's used for filtering symbolic coordinates (not numerical ones). That explains the use of \ifx: That macro compares two eXpanded arguments (the "x" doesn't refer to the x-axis, but to eXpansion). If they expand to the same string, some code is executed.

\def\pgfmathresult{inf} redefines the macro \pgfmathresult to evaluate to inf.

share|improve this answer

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.