5

To create points with names P1, P2, ..., P8 at coordinates (1,0), (2,0), ..., (8,0) in TikZ, I can use

\begin{tikzpicture}[scale=1]

  \foreach \i in {1,...,8}
  {
    \path (\i,0) coordinate (P\i);
    \fill (P\i) circle (2pt);
  }

\end{tikzpicture}

Now, what if I want to create the same eight points, but name them P2, P3, ..., P9 instead?

I've tried changing it to

\path (\i,0) coordinate (P(\i+1));

but it didn't work.

1
  • Welcome to the TeX.SX and thank you for your question. Could you please extend your example to make it minimum working example, starting with \documentclass... and ending with ...\end{document}. It helps solvers a lot to actually start solving the problem. It would be also great if you could change your display name from userxxx to something more descriptive, we are all human beings, right? :-)
    – Malipivo
    Apr 6 '14 at 6:25
4

This would work. Use an additional variable \t that relates, via a formula, to \i, as shown below. Here a node is added to print the label for visual expression.

[evaluate=\i as \t using int(\i+1)] 

More: proposed by percusse, via

[count=\t from 2]

which means start counting from 2,3,4,...

enter image description here

Code

\documentclass[a4paper,10pt]{article}
\usepackage{tikz}

\begin{document}

Before:
\bigskip

\begin{tikzpicture}[scale=1]

  \foreach \i in {1,...,8}
  {
    \path (\i,0) coordinate (P\i) node[above]{P\i};
    \fill (P\i) circle (2pt);
  }

\end{tikzpicture}

\bigskip
After:
\bigskip

\begin{tikzpicture}[scale=1]

  \foreach \i[evaluate=\i as \t using int(\i+1)] in {1,...,8}
  {
    \draw (\i,0) coordinate (P\t) node[above]{P\t};
    \fill (P\t) circle (2pt);
  }

\end{tikzpicture}

\bigskip
More: proposed by percusse. 
\bigskip    

\begin{tikzpicture}[scale=1]

  \foreach \i[count=\t from 2] in {1,...,8}
  {
    \draw (\i,0) coordinate (P\t) node[above]{P\t};
    \fill (P\t) circle (2pt);
  }

\end{tikzpicture}


\end{document}
6
  • 1
    Maybe also including the possibility of [count=\xi from 2] ?
    – percusse
    Apr 6 '14 at 8:49
  • Hi, @percusse -- Great tips and allow me to quote your name to express my appreciation.
    – Jesse
    Apr 6 '14 at 9:52
  • Oh please, it's my pleasure
    – percusse
    Apr 6 '14 at 10:13
  • @Jesse I tried copying your example exactly and get Runaway argument? int(\i +1)] in {1,...,8} { \draw (\i ,0) coordinate (P\t ) node[above\ETC. ! Paragraph ended before \pgffor@next was complete. <to be read again> \par l.30 ? ! Use of \i doesn't match its definition. <argument> \i , 0 l.43 } ? ! Package pgf Error: No shape named 0 is known. See the pgf package documentation for explanation. Type H <return> for immediate help.
    – boaten
    Apr 6 '14 at 13:54
  • @boaten I just did that copy and paste to run. It is fine. Please try again and I will check again.
    – Jesse
    Apr 6 '14 at 14: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.