4

i want to know how to use the foreach loop for adding arcs using commands \path and edge. For instance, the following code generates a cycle and the task is to add the arcs not manually:

\documentclass[border=2pt,tikz]{standalone}
\usepackage{wasysym}
\usetikzlibrary{decorations.pathreplacing,automata,arrows,shadows,patterns,shapes}
\begin{document}

\newlength{\rnodo}
\newlength{\radio}
\setlength{\rnodo}{10pt}
\setlength{\radio}{3.00cm}
\tikzstyle{nondirected}=[thick]
\tikzstyle{labels}=[inner sep=0pt,font=\scriptsize,auto,circle]
\tikzstyle{main node}=[outer sep=1,inner sep=0,ellipse,thick,draw,minimum size=2\rnodo,fill=black!10]

\newcommand\n{10}
\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}$};
}

\path[nondirected]
(n1) edge node[labels] {$\oplus$}  (n2)
(n2) edge node[labels] {$\oplus$}  (n3)
(n3) edge node[labels] {$\ominus$}  (n4)
(n4) edge node[labels] {$\ocircle$}  (n5)
(n5) edge node[labels] {$\ocircle$}  (n6)
(n6) edge node[labels] {$\oplus$}  (n7)
(n7) edge node[labels] {$\oplus$}  (n8)
(n8) edge node[labels] {$\ominus$}  (n9)
(n9) edge node[labels] {$\ominus$}  (n10)
(n10) edge node[labels] {$\ocircle$}  (n1);
\end{tikzpicture}

\end{document}

In this LINK, I've been given some alternatives using command \draw. I've tried to adapt it, but with no luck so far.

1
  • Copy-paste lines of code is easier in this case, no much gain out of a loop here.
    – AboAmmar
    Commented Jul 17, 2015 at 23:58

3 Answers 3

4

All can be done with two simple loops: one to place the nodes and the other one to place the links and labels:

\def\n{10}
\begin{tikzpicture}
\foreach \x in {0,...,\the\numexpr\n-1\relax}
    \node (n\x) at ({(1+2*(\x+1))*180/\n-90}:\radio) {$n_{\the\numexpr\x+1\relax}$};
\foreach \Texto [count=\x]  in {\oplus,\oplus,\oplus,\ominus,\ocircle,\ocircle,\oplus,\oplus,\ominus,\ominus}
{
  \pgfmathsetmacro{\ttt}{int(mod(\numexpr\x+\n-1\relax,\n))}
  \pgfmathsetmacro{\mmm}{int(mod(\numexpr\x+\n-2\relax,\n))}
  \draw[nondirected] (n\mmm) -- node[labels] {$\Texto$} (n\ttt);
}
\end{tikzpicture}

enter image description here

The complete code:

\documentclass{article}
\usepackage{tikz}
\usepackage{wasysym}
\usetikzlibrary{decorations.pathreplacing,automata,arrows,shadows,patterns,shapes}

\newlength\rnodo
\newlength\radio
\setlength{\rnodo}{10pt}
\setlength{\radio}{3.00cm}

\tikzset{
  nondirected/.style={thick},
  labels/.style={
    inner sep=0pt,
    font=\scriptsize,
    auto,
    circle},
  main node/.style={
    outer sep=1,
    inner sep=0,
    ellipse,
    thick,
    draw,
    minimum size=2\rnodo,
    fill=black!10}
}

\begin{document}

\def\n{10}
\begin{tikzpicture}
\foreach \x in {0,...,\the\numexpr\n-1\relax}
    \node (n\x) at ({(1+2*(\x+1))*180/\n-90}:\radio) {$n_{\the\numexpr\x+1\relax}$};
\foreach \Texto [count=\x]  in {\oplus,\oplus,\oplus,\ominus,\ocircle,\ocircle,\oplus,\oplus,\ominus,\ominus}
{
  \pgfmathsetmacro{\ttt}{int(mod(\numexpr\x+\n-1\relax,\n))}
  \pgfmathsetmacro{\mmm}{int(mod(\numexpr\x+\n-2\relax,\n))}
  \draw[nondirected] (n\mmm) -- node[labels] {$\Texto$} (n\ttt);
}
\end{tikzpicture}

\end{document}

Explanation

The first loop

\foreach \x in {0,...,\the\numexpr\n-1\relax}
    \node (n\x) at ({(1+2*(\x+1))*180/\n-90}:\radio) {$n_{\the\numexpr\x+1\relax}$};

is clear; it's almost the original, but I looped from 0 to n-1 instead of from 1 to n because in the second loop some modular arithmetic will be used.

The second loop (forgetting the labels in this explanation, which are not important here) could be simplified to

\foreach \x in {1,...,\n}
{
  \draw[nondirected] (n int(mod(\x+\n-2,\n))) -- (n int(mod(\x+\n-1,\n)));
}

so it joins n(n-1) to n0, n0 to n1,...,n(n-2) to n(n-1). So, simply changing \n to 20 and providing the labels for the additional elements, as in

\def\n{20}
...
\foreach \Texto [count=\x]  in {\oplus,\oplus,\oplus,\ominus,\ocircle,\ocircle,\oplus,\oplus,\ominus,\ominus,\oplus,\oplus,\oplus,\ominus,\ocircle,\ocircle,\oplus,\oplus,\ominus,\ominus}
{...}

you get

enter image description here

4
  • I don't know why, but the math labels weren't working in a foreach for me.
    – Alenanno
    Commented Jul 18, 2015 at 8:29
  • Thanks!!! Just one question: how to make the "9" in the foreach loop depend on \n? Like \foreach \x in {0,...,\n-1}, but it didn't work Commented Jul 19, 2015 at 2:01
  • @LuisGómezGuzmán You're welcome! Please see my updated answer. Now, everything is dependant on \n so you just have to change \n and provide enough labels in the second loop to easily get the shape with a variable number of vertices. See the example at the bottom. Commented Jul 19, 2015 at 15:30
  • thankz!!, i also solved it replacing \pgfmathsetmacro{\ed}{int(mod(\x+9,10))} by \pgfmathsetmacro{\ed}{int(mod(\x+9,10)+1)}. Commented Jul 20, 2015 at 18:47
2

Somewhat crude, but serviceable...

\documentclass[tikz,border=5]{standalone}
\usepackage{wasysym}
\usetikzlibrary{shapes.geometric}
\begin{document}
\tikz\node [regular polygon, regular polygon sides=10, 
  draw, minimum size=5cm, rotate=-108] (p) {} 
  \foreach \l [count=\i] in {\oplus,\oplus,\ominus,\ocircle,\ocircle,
      \oplus,\oplus,\ominus,\ominus,\oplus}{ 
    node [circle, inner sep=0, fill=white] at (p.corner \i) {\strut$n_{\i}$}
    node [circle, font=\scriptsize, anchor=\i*36+54-108, inner sep=0] 
      at (p.side \i) {$\l$} };
\end{document}

enter image description here

0
0

I'm having a bit of trouble with the labels because I wanted to use an array list, but it refuses to work although the code is fine. However it's getting late here, so I'll take a look at it tomorrow. For now, here's the foreach for the lines.

Output

figure 1

Code

\documentclass{article}
\usepackage{tikz}
\usepackage{wasysym}
\usetikzlibrary{decorations.pathreplacing,automata,arrows,shadows,patterns,shapes}

\newcommand{\rnodo}{10}
\newcommand{\radio}{3.00cm}
\tikzstyle{nondirected}=[thick]
\tikzstyle{labels}=[inner sep=0pt,font=\scriptsize,auto,circle]
\tikzstyle{main node}=[outer sep=1,inner sep=0,ellipse,thick,draw,minimum size=2pt*\rnodo,fill=black!10]



\begin{document}
\def\n{10}
%\newcommand\mylist{{"$\oplus$","$\oplus$","$\ominus$","$\ocircles$","$\ocircles$","$\oplus$","$\oplus$","$\ominus$","$\ominus$"}}

\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}$};
}

\foreach \lab [count=\xi, remember=\xi as \lastx (initially 1)] in {1,...,11}{
\ifnum\xi<11
\path[nondirected] (n\lastx) edge node[labels] {} (n\xi);
\else
\path[nondirected] (n10) edge node[labels] {$\ocircle$} (n1);
\fi
}
\end{tikzpicture}
\end{document}
0

You must log in to answer this question.

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