13
\documentclass{standalone}
\usepackage{tikz}
\begin{document}
  \begin{tikzpicture}
    \foreach \x in{0,0.1,...,0.5}{
       \node[] at (20*\x,0) {\x};
       }
  \end{tikzpicture}
\end{document}

enter image description here

However, as follows:

\documentclass{standalone}
\usepackage{tikz}
\begin{document}
  \begin{tikzpicture}
    \foreach \x in{0,0.125,...,0.5}{
       \node[] at (20*\x,0) {\x};
       }
  \end{tikzpicture}
\end{document}

enter image description here

it is normally.

3
  • 2
    Hello and welcome to Tex.SE. This is due to an rounding error. See here for a full explanation.
    – Roland
    Commented Aug 12, 2021 at 1:53
  • 3
    Just use integers in in{1, ..., 10} whenever possible. You can always divide later
    – Symbol 1
    Commented Aug 12, 2021 at 3:51
  • In binary representation numbers of the form n * 2^-m are exact. Whereas n * 10^-m have inherent rounding errors in binary representation. I don't know pfg internal representation, but since 0.125, 0.25 do not give rounding errors I suppose internal representation is binary. Commented Aug 17, 2021 at 23:48

3 Answers 3

9

Welcome to TeX.SE. As said in comments, this is a precision issue due to how TikZ performs floating point computations (it uses TeX \dimen registers by default). Since the computed numbers are very close to the desired ones, you just need to use \pgfmathprintnumber in order to typeset the numbers rounded to the desired precision:

\documentclass[tikz,border=1mm]{standalone}
\begin{document}
\begin{tikzpicture}
  \foreach \x in {0, 0.1, ..., 0.5} {
    \node at (20*\x, 0) {%
      \pgfmathprintnumber[fixed, precision=1]{\x}%
  };
}
\end{tikzpicture}
\end{document}

enter image description here

Bottom line: computation is one thing, often not perfectly accurate. Number formatting is another thing that \pgfmathprintnumber does well.

1
  • Thanks for your answer! I already understand Commented Aug 12, 2021 at 12:15
8

As well as the precision of the displayed number (as answered in the other solutions), there is the issue with the missing node. In the first foreach loop the last node is not displayed because PGF thinks that the next value is 0.50003 which is more than 0.5 so is outside the bounds of the loop.

As Symbol1 said in the comments, it is best to use integers in the loop when using the ... syntax to ensure that this sort of thing doesn't happen. This then means that the coordinate calculation needs changing, but this is easy to incorporate. Slightly more complicated is computing the displayed number. This can be done either in the node text itself using pgfmathparse{\x/10}\pgfmathprintnumber[fixed, precision=1]{\pgfmathresult} or it can be done in the foreach loop itself (which is more useful if you are going to use the number more than once).

\documentclass{standalone}
%\url{https://tex.stackexchange.com/q/610368/86}
\usepackage{tikz}
\begin{document}
  \begin{tikzpicture}
    \foreach \x in{0,0.1,...,0.5}{
       \node[] at (20*\x,0) {\x};
       }
  \end{tikzpicture}

  \begin{tikzpicture}
    \foreach[evaluate=\x as \displayx using \x/10] \x in{0,1,...,5}{
       \node[] at (2*\x,0) {\pgfmathprintnumber[fixed, precision=1]{\displayx}};
       }
  \end{tikzpicture}
\end{document}

Loops with displayed values

2
  • 1
    Good point, you have my upvote! ;-)
    – frougon
    Commented Aug 12, 2021 at 16:00
  • @frougon And you have mine! Commented Aug 12, 2021 at 16:07
7

Borrowing and adapting the technique from frougon, Precision for semilogyaxis

\documentclass{standalone}
\usepackage{pgfplots, tikz}
\begin{document}
\begin{tikzpicture}
    \foreach \x in{0,0.1,...,0.5}{
       \node[] at (20*\x,0) {\pgfmathprintnumber{\x}};
}
\end{tikzpicture}
\end{document}

enter image description here

3
  • 1
    Thanks for the reference. Note that we only need to round the input numbers here (which are computed by the \foreach statement with imperfect precision before your \pgfset{fixed point arithmetic,...} kicks in); so, the \pgfmathparse{\x} step is useless and might even result in a loss of precision in some situations (depending on what was in \x before). On the other hand, \pgfmathprintnumber parses the input number with \pgfmathfloatparsenumber, which “allows arbitrary precision” (as written in the documentation of \pgfmathprintnumber). :-)
    – frougon
    Commented Aug 12, 2021 at 11:35
  • @frougon I have edited, hopefully capturing the essence of your comment. Thanks for your advice. Commented Aug 12, 2021 at 12:18
  • That's better, but the \pgfset{fixed point arithmetic} is still useless here, AFAICS: no computation whatsoever is performed with this engine! The only thing we need to do here is some rounding before typesetting the number... which is what my answer does.
    – frougon
    Commented Aug 12, 2021 at 12:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .