2

I am trying to draw a path and position nodes on it and fill them using this code and it works

\documentclass[varwidth=20cm]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\draw [dotted](0,0)--(2.5,0.5)
  node(2)[pos=0.1]{} node(3)[pos=0.2]{} node(4)[pos=0.3]{} node(5)[pos=0.4]{} node(6)[pos=0.5]{} node(7)[pos=0.6]{} node(8)[pos=0.7]{} node(9)[pos=0.8]{}  node(1)[pos=0.9]{} node(10)[pos=1]{};
%  \draw [red,dotted] plot [smooth] coordinates {(0,0) (1,0.1) (2,0.3) (2.5,0.5)};

  \fill (1) circle[radius=1pt];
  \fill (2) circle[radius=1pt];
  \fill (3) circle[radius=1pt];
  \fill (4) circle[radius=1pt];
  \fill (5) circle[radius=1pt];
  \fill (6) circle[radius=1pt];
  \fill (7) circle[radius=1pt];
  \fill (8) circle[radius=1pt];
  \fill (9) circle[radius=1pt];
  \fill (10)circle[radius=1pt];
  \end{tikzpicture}
\end{document}

Now, i want to make it more efficient and not to repeat the same line of code 10 times by using \foreach. I stared by this piece of code put it gives this error Illegal unit of measure (pt inserted)

\documentclass[varwidth=20cm]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\draw [dotted](0,0)--(2.5,0.5)
  \foreach \x [evaluate=\x] in {1,2,...,10}
    node(\x) [pos={\x/10}]{}
    \fill (\x) circle[radius=1pt];
\end{tikzpicture}
\end{document}
  • No, i am still wanting to draw the nodes on the path not by their own. – Misaki Jul 15 '14 at 8:37
  • Which version of tikz are you using? Insert \listfiles in your preamble and read out the log. I do not get the error you are getting. – LaRiFaRi Jul 15 '14 at 8:39
  • What is the expected output when using \listfiles, i put it and nothing changed in the log. – Misaki Jul 15 '14 at 8:44
  • it should show the version of tikz and pgf. You have to use the first example to get it written in your log. (full compilable). – LaRiFaRi Jul 15 '14 at 8:54
  • 1
    OK, you could update your system to version 3.0 and try Claudios answer which will work for sure. – LaRiFaRi Jul 15 '14 at 9:00
2

The problems are two basically:

  1. The \foreach needs braces if there are more than one operation to be performed;
  2. It is not possible to use \draw and \fill in the same path.

The point n.2 is the most important: what one does while using a \foreach statement inside a \drawn path is to append things. Of course it is possible to append nodes, but it is not possible, in the same statement, to add nodes and fill circles drawn as a separate path.

The explanation suggests there is a simpler way to make it: nodes can be filled themselves, so why not use this possibility?

An example:

\documentclass[border=10pt,tikz]{standalone}
\begin{document}
\begin{tikzpicture}
\draw [dotted](0,0)--(2.5,0.5)
  \foreach \x in {1,2,...,10}{
    node[fill,circle,inner sep=0.75pt](\x) [pos={\x/10}]{}
  };
\end{tikzpicture}
\end{document}
  • I used node before adding fill and the same error occurred. This code also gives the same error. But thanks for the node filling tip. – Misaki Jul 15 '14 at 8:47
  • @Misaki: Did you compile the exact code of mine? – Claudio Fiandrino Jul 15 '14 at 8:50
  • Yes i did. same error. – Misaki Jul 15 '14 at 8:52
  • @Misaki: I really don't see why it should not compile, even with previous version of TikZ 2.10. Do you have the 2.10 version or the new 3.0.0? – Claudio Fiandrino Jul 15 '14 at 8:54
  • tikz.sty 2012/08/29 v2.10-cvs (rcs-revision 1.100) pgf.sty 2008/01/15 v2.10-cvs (rcs-revision 1.12) – Misaki Jul 15 '14 at 9:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.