5

I am still new to using Tikz... I am trying to draw a wheel-like graph using TikZ. I have the basic graph done, but it is not turning out how I expected it to. The edges in my graph aren't centered/lined up well.

The edges seem to be following the coordinates rather than drawing a shortest-path straight line between the nodes stopping at their circle representations. Some of the edges are fine (like v1 to v2, v1 to v6, ...) but some others aren't (like v2 to v3).

Here is my code for the TikZ picture, and an image showing what I am getting.

\begin{tikzpicture}[auto, scale=0.9]
\tikzstyle{vertex}=[draw, circle, inner sep=0.55mm]
\node (v1) at (0,0) [vertex] {};
\node (v2) at (1,0) [vertex] {};
\node (v3) at  (1.5,-1) [vertex] {};
\node (v4) at (1,-2) [vertex] {};
\node (v5) at (0,-2) [vertex] {};
\node (v6) at (-.5,-1) [vertex] {};
\node (v7) at  (.5,-1) [vertex, fill=blue] {};

\foreach \x in {2, 3, 4, 5, 6, 7}{
    \pgfmathsetmacro\y{\x - 1}
    \draw (v\y) to (v\x);
}
\draw (v6) to (v1);
\draw (v5) to (v7);
\draw (v4) to (v7);
\draw (v3) to (v7);
\end{tikzpicture}

wheel like graph with bad looking edges

  • The issue is that \pgfmathsetmacro does not yield integers, but something like 2.0, where .0 gets interpreted as node anchor. – marmot Feb 26 at 17:26
8

That's because the calculation of \y doesn't give an integer. There are two possibilities:

  • the first is to use the macro \pgfmathtruncatemacro instead of \pgfmathsetmacro
  • the second is to evaluate \y within the foreach loop itself

\documentclass[tikz,border=5mm]{standalone}

\usetikzlibrary{calc}

\begin{document}
\begin{tikzpicture}[auto, scale=0.9]
\tikzstyle{vertex}=[draw, circle, inner sep=0.55mm]
\node (v1) at (0,0) [vertex] {};
\node (v2) at (1,0) [vertex] {};
\node (v3) at  (1.5,-1) [vertex] {};
\node (v4) at (1,-2) [vertex] {};
\node (v5) at (0,-2) [vertex] {};
\node (v6) at (-.5,-1) [vertex] {};
\node (v7) at  (.5,-1) [vertex, fill=blue] {};

\foreach \x[evaluate=\x as \y using int(\x-1)] in {2, 3, 4, 5, 6, 7}{
    %\pgfmathtruncatemacro\y{\x - 1}
    \draw (v\y) to (v\x);
}
\draw (v6) to (v1);
\draw (v5) to (v7);
\draw (v4) to (v7);
\draw (v3) to (v7);
\end{tikzpicture}
\end{document}

screenshot

  • On my computer the line \foreach \x[evaluate=\x as \y using int(\x-1)]☺ in {2, 3, 4, 5, 6, 7}{ looks like it has an smiley face in it... – Abraham Feb 26 at 17:24
  • ok, fixed now ! – AndréC Feb 26 at 17:25
  • This works, seems that \pgfmathsetmacro was the problem, as @marmot explained. Thank you! – Abraham Feb 26 at 17:29
  • I have given two possibilities to correct this problem. – AndréC Feb 26 at 17:32
  • This was the first answer, and now that an explanation was added, I am marking this as the accepted answer. The other answers were helpful as well! – Abraham Feb 26 at 21:14
7

\tikzstyle is deprecated and the issue is that \pgfmathsetmacro does not yield integers, but something like 2.0, where .0 gets interpreted as node anchor.

\documentclass[tikz,border=3.14mm]{standalone}
\begin{document}
\begin{tikzpicture}[auto, scale=0.9]
\tikzset{vertex/.style={draw, circle, inner sep=0.55mm}}
\node (v1) at (0,0) [vertex] {};
\node (v2) at (1,0) [vertex] {};
\node (v3) at  (1.5,-1) [vertex] {};
\node (v4) at (1,-2) [vertex] {};
\node (v5) at (0,-2) [vertex] {};
\node (v6) at (-.5,-1) [vertex] {};
\node (v7) at  (.5,-1) [vertex, fill=blue] {};

\foreach \x [remember =\x as \lastx (initially 1)] in {2, 3, 4, 5, 6, 7}{
    \draw (v\lastx) to (v\x);
}
\draw (v6) to (v1);
\draw (v5) to (v7);
\draw (v4) to (v7);
\draw (v3) to (v7);
\end{tikzpicture}
\end{document}

enter image description here

  • It is interesting to see how the accepted answer added the explanation why this happens to a revised version of their answer after my answer appeared. If this was not a repeating issue with the same user, one may think this is just a coincidence. – marmot Feb 26 at 23:04
  • In their original answer, they did comment out the part that was causing the issue, so I assume that they might have known about it but I agree that they should have cited you (the first person who commented about the problematic code) as I did when thanking that user. – Abraham Feb 28 at 18:14
  • @Abraham Yes, it is a well known thingy, see e.g. here and many other places. The issue is more that there are recurring patterns of behavior and practices by certain users. Either one thinks it is a useful information that should be added or not. And it is also fine to add it after seeing some other answer adds it, but IMHO then one should say that one has adopted the explanation from there. – marmot Feb 28 at 18:20
3

maybe you will like:

\documentclass[tikz,border=5mm]{standalone}
\usetikzlibrary{shapes.geometric}

\begin{document}
\begin{tikzpicture}[
vertex/.style = {circle, draw, fill=#1, inner sep=0.5mm}
                    ]
%
\node (s) [regular polygon,regular polygon sides=6,
           draw, minimum size=20mm, above] at (0.5,-2) {};
\draw (s.corner 3) -- (s.corner 6);
\node (c) [vertex=blue] at (s.center) {};
%
\foreach \i in {1,...,6}{\node (s\i) [vertex=white] at (s.corner \i) {}; }

\draw (c) -- (s4)
      (c) -- (s5);
\end{tikzpicture}
\end{document}

enter image description here

3

To get it more wheel like you can use polar coordinates, like (45:1) to draw a node at distance 1 and 45 degrees from origin. Here is an alternative version with a variable number of nodes. Change the number in \numNodes{6} to change the number of nodes n the circle.

\documentclass[border=5mm]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[
  auto, 
  scale=0.9,
  vert/.style={draw, circle, inner sep=0.55mm,fill=white}
  ]
  \newcommand\numNodes{6}
  \node[vert,fill=blue] (vC) at (0,0){};
  \draw (0:1) node[vert](v0) {}
  \foreach \n [evaluate = \n as \deg using {\n*360/\numNodes}] in {1,2,...,\numNodes}{
    -- (\deg:1) node[vert](v\n) {}
  };
  \foreach \n in {0,3,4,5}{
    \draw (vC) -- (v\n);
  }
\end{tikzpicture}

\end {document}

enter image description here

  • you have a typo (1,45) is not a polar coordinate – AndréC Feb 26 at 18:18

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.