4

The following code fails to compile due to the use of rnd:

\documentclass{article}
\usepackage{tikz,pgfplots}
\pgfplotsset{width=7cm,compat=1.3}
\begin{document}

\begin{tikzpicture}
\begin{axis}
\addplot {x};
    \draw [] (axis cs: 0,0)
      \foreach \i in {1,...,10} {
        -- ++ (axis direction cs: 0.2,rnd)
        };
   \end{axis}
\end{tikzpicture}
\end{document}

Why is this happening and how can it be rewritten to work as intended?

niels
  • 3,295

1 Answers1

5

This is probably due to the coordinate parsing as rnd is not recognized as a math function but as a non-number instead. Bracing it or other syntax tricks might work but it's best to avoid them for such simple constructions.

Here is one option by removing the math parsing from the coordinate parsing. \pgfextra is used to pause the parsing and do some additional operations.

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.7}
\begin{document}
\begin{tikzpicture}
\begin{axis}
\addplot {x};
    \draw [] (axis cs: 0,0)
      \foreach \i in {1,...,10} {\pgfextra{\pgfmathparse{rnd}}
        -- ++ (axis direction cs: 0.2,\pgfmathresult)
        };
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

percusse
  • 157,807