4

i'm getting an error in this code, can someone help me. I'm trying to create two concentric cycles, but it doesn't seems to work.

\def\n{10}
\defn\radio{3.00cm}
\begin{tikzpicture}
\foreach \x in {1,...,\n}
    {
    \coordinate (cn\x) at ({(1+2*\x)*180/\n-90}:\radio);
    \node (n\x) at (cn\x) {$n_\x$};
    \coordinate (cm\x) at ({(1+2*\x)*180/\n-45}:(\radio+1));
    \node (m\x) at (cm\x) {$m_\x$};
    }
\end{tickzpicture}

1 Answer 1

8

Be careful of spelling mistakes, you wrote \end{tickzpicture}. Also, you can specify \radio as \def\radio{3}. A tikzpicture is by default on 1 = 1cm, so no need to specify it.

Finally, the error comes out because of this: ...:(\radio+1)); If you want to enclose a calculation, then you need to use curly brackets, because otherwise Tikz is going to confuse them with the other ones.

Also, since your \x reaches 10, you need also enclose it within curly brackets, like $n_{\x}$, otherwise, you'll get only the first number properly displayed, like in this image:

enter image description here

Update

If you want to add arcs that link the nodes, there are two ways. The first one would be to add this code:

\foreach \x [remember=\x as \lastx (initially 1)] in {1,...,\n,1}{%
    \draw (n\lastx) to [bend right=10] (n\x);
    \draw (m\lastx) to [bend right=10] (m\x);
}

The curves will be very similar to a circle, but not 100%. And here the second method comes in handy. Basically you draw two circles in the background and then fill the nodes with a white color. Now you have a perfectly circular link to all nodes. I would suggest this method.

Output

figure 1

Code

\documentclass[tikz, margin=10pt]{standalone}
\begin{document}
\def\n{10}
\def\radio{3}
\begin{tikzpicture}
\draw (0,0) circle (\radio);
\draw (0,0) circle (\radio+1);
\foreach \x in {1,...,\n}{
    \coordinate (cn\x) at ({(1+2*\x)*180/\n-90}:\radio);
    \node[fill=white] (n\x) at (cn\x) {$n_{\x}$};
    \coordinate (cm\x) at ({(1+2*\x)*180/\n-45}:{\radio+1});
    \node[fill=white] (m\x) at (cm\x) {$m_{\x}$};
}

\node at (0,5) {Using circles};
\begin{scope}[xshift=10cm]
\node at (0,5) {Using a $\backslash$foreach statement};
\foreach \x in {1,...,\n}{
    \coordinate (cn\x) at ({(1+2*\x)*180/\n-90}:\radio);
    \node[fill=white] (n\x) at (cn\x) {$n_{\x}$};
    \coordinate (cm\x) at ({(1+2*\x)*180/\n-45}:{\radio+1});
    \node[fill=white] (m\x) at (cm\x) {$m_{\x}$};
}
\foreach \x [remember=\x as \lastx (initially 1)] in {1,...,\n,1}{%
    \draw (n\lastx) to [bend right=10] (n\x);
    \draw (m\lastx) to [bend right=10] (m\x);
}
\end{scope}
\end{tikzpicture}
\end{document}
4
  • Braces are needed if the calculation has parentheses in them.
    – percusse
    Jul 13, 2015 at 18:41
  • @LuisGómezGuzmán See updated answer for the arcs.
    – Alenanno
    Jul 14, 2015 at 9:34
  • Thanks for your answer! Is there a way to do something similar by using the \path and \edge commands, something like \path \foreach \x in {1,...,{\n-1}} { (n{\x}) edge[bend right=20] (n_{\x+1}); } It would be helpful not just for this cycle, but for other graphs i need to draw, and most of them have labels in the arcs Jul 15, 2015 at 17:38
  • @LuisGómezGuzmán Not sure what you mean, but if those are different cases, I suggest posting another question. You can link to this one as context.
    – Alenanno
    Jul 15, 2015 at 18:11

You must log in to answer this question.

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