Menu

#230 bug when trying to store `\pgfplotstablegetrowsof` with `\pgfmathsetmacro`

v1.0 (example)
open
5
2019-11-30
2019-11-30
No

Actually this is a bug that originally was reported in the TikZ/pgf tracker (https://sourceforge.net/p/pgf/bugs/383/, https://github.com/pgf-tikz/pgf/issues/382) but should most likely have been reported here. So I am "repeating" it here with some minor modifications/updates. I also provide an MWE at the end.


For simplicity here I only copy some lines of emails in German which should act as a reminder for Christian.

My email:

[...] könntest du mal einen kurzen Blick auf
https://tex.stackexchange.com/revisions/299200/3
werfen und mir sagen, ob es ein Bug ist, dass ich \NoOfRows nicht in \pgfplotstablegetelem verwenden kann. [...]

His answer:

[...] das mit \NoOfRows ist eine Mischung aus der komplizierten Nutzung der FPU engine und einem fehlenden sanity check (=bug).

Was passiert ist dass \NoOfRows auf 1Y4.0e0 gesetzt wird, was soviel heisst wie "+ 4.0 * 10^0". Als Zeilennummer interpretiert scheint der aber nur die "1" genommen zu haben und den Rest stillschweigend weggeworfen zu haben. [...]

MWE:

% used PGFPlots v1.16
    % first put the data to an external file or a table,
    % so it can be handled by the `pgfplotstable' package
    \begin{filecontents}{data.txt}
        0   0
        1   1
        2   2
        3   3
        4   4
    \end{filecontents}
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
    \usetikzlibrary{
        fpu,
        positioning,    % <-- for debugging nodes only
    }
    \pgfplotsset{
        compat=1.16,
    }
\begin{document}
    \begin{tikzpicture}
        % to be sure that no numbers are interpreted as zero, activate `fpu'
        % library
        \tikzset{
            fpu=true,
        }
            %%% find `xmin' and `xmax'
            % for that first sort the table after the x values
            \pgfplotstablesort{\SortedTable}{data.txt}
            % then one can directly extract `xmin'
            \pgfplotstablegetelem{0}{[index] 0}\of\SortedTable
                \pgfmathsetmacro{\xmin}{\pgfplotsretval}
            %% now extract `xmax'
            % for that we first need to extract the number of rows in the table
            \pgfplotstablegetrowsof{\SortedTable}
                \pgfmathsetmacro{\NoOfRows}{\pgfplotsretval-1}
            % then we can extract the last entry in the sorted table
            % (I think because of a bug this doesn't work so one has to
            %  enter the index manually)
            \pgfplotstablegetelem{\NoOfRows}{[index] 0}\of\SortedTable
%            \pgfplotstablegetelem{4}{[index] 0}\of\SortedTable
                \pgfmathsetmacro{\xmax}{\pgfplotsretval}

        \tikzset{
            fpu=false,
        }

        \begin{axis}[
            % add the extra ticks from the extracted data
            extra x ticks={
                \xmin,
                \xmax
            },
            % just to make sure these are the extra ticks, draw them red
            extra x tick style={
                text=red,
            },
        ]
            \addplot +[black, mark options=fill=black] table {data.txt};

            % for debugging show the value of `xmax'
            \node [fill=black!25,anchor=north west]  (a) at (rel axis cs:0.02,0.98)
                {Rows = \pgfmathprintnumber{\NoOfRows}};
            \node [fill=black!25,below=1pt of a.south west,anchor=north west]
                {xmax = \pgfmathprintnumber{\xmax}};
        \end{axis}
    \end{tikzpicture}
\end{document}

which results in the attached picture.

1 Attachments

Discussion