1

Consider the following figure:

\begin{tikzpicture}[
    selected/.style={circle, draw=black, fill=yellow!60, very thick},
    unselected/.style={circle, draw=black, very thick}
]
    \node[selected]   (0) at (0, 0) {$x_1$};
    \node[unselected] (1) at (1.5, 0) {$\bar x_1$};
\node[selected]   (2) at (4, 0) {$x_2$};
\node[unselected] (3) at (5.5, 0) {$\bar x_2$};

\node[unselected] (4) at (8, 0) {$x_3$};
\node[selected]   (5) at (9.5, 0) {$\bar x_3$};

\node[unselected] (6) at (-1.5, -1.5) {$x_1$};
\node[selected]   (7) at (-1.5, -3) {$\bar x_2$};
\node[selected]   (8) at (-1.5, -4.5) {$\bar x_3$};

\node[selected]   (9)  at (0, -6) {$\bar x_1$};
\node[unselected] (10) at (1.5, -6) {$x_2$};
\node[selected]   (11) at (3, -6) {$\bar x_3$};

\draw[-] (0) -- (1)
         (2) -- (3)
         (4) -- (5);

\draw[-] (6) -- (7)
         (7) -- (8)
         (6) to [bend right=45] (8);

\draw[-] (9) -- (10)
         (10) -- (11)
         (9) to [bend right=45] (11);

\draw[-] (6) -- (0)
         (7) -- (3)
         (8) -- (5)
         (9) -- (1)
         (10) -- (2)
         (11) -- (5);

\end{tikzpicture}

Which looks like: Expected output

Now, I try using a foreach loop and some math:

\begin{tikzpicture}[
    selected/.style={circle, draw=black, fill=yellow!60, very thick},
    unselected/.style={circle, draw=black, very thick}
]
    \node[selected]   (0) at (0, 0) {$x_1$};
    \node[unselected] (1) at (1.5, 0) {$\bar x_1$};
\node[selected]   (2) at (4, 0) {$x_2$};
\node[unselected] (3) at (5.5, 0) {$\bar x_2$};

\node[unselected] (4) at (8, 0) {$x_3$};
\node[selected]   (5) at (9.5, 0) {$\bar x_3$};

\node[unselected] (6) at (-1.5, -1.5) {$x_1$};
\node[selected]   (7) at (-1.5, -3) {$\bar x_2$};
\node[selected]   (8) at (-1.5, -4.5) {$\bar x_3$};

\node[selected]   (9)  at (0, -6) {$\bar x_1$};
\node[unselected] (10) at (1.5, -6) {$x_2$};
\node[selected]   (11) at (3, -6) {$\bar x_3$};

\draw[-] (0) -- (1)
         (2) -- (3)
         (4) -- (5);

\foreach \x in {0,...,1}{
    \pgfmathsetmacro\a{6+3*\x}
    \pgfmathsetmacro\b{7+3*\x}
    \pgfmathsetmacro\c{8+3*\x}
    \draw[-] (\a) -- (\b)
             (\b) -- (\c)
             (\a) to [bend right=45] (\c);
}

\draw[-] (6) -- (0)
         (7) -- (3)
         (8) -- (5)
         (9) -- (1)
         (10) -- (2)
         (11) -- (5);

\end{tikzpicture}

Which looks like: Weird output

Why do the lines in the latter plot all connect to the right-most corner of the nodes rather than how they connected beforehand?

And help would be most appreciated!

Ofek Gila
  • 111
  • Please write a minimal working example. See link for more details what it means. -- although I think there's indeed a problem here. – user202729 Nov 09 '21 at 05:30
  • 6
    You should use \pgfmathtruncatemacro instead of \pgfmathsetmacro because otherwise pgf will come up with numbers like 2.0, where 0 is interpreted as node anchor. –  Nov 09 '21 at 05:33
  • @ABC thanks, this worked! – Ofek Gila Nov 09 '21 at 05:34
  • 4
    I’m voting to close this question because solved in comments. – Roland Nov 09 '21 at 07:42
  • @Roland I don't think it's a good idea to close question because it's solved in comment, because someone may come post a "better" answer (instead, someone could repost the comment in an answer) // although in this case there are duplicates anyway. – user202729 Nov 09 '21 at 13:41
  • @user202729 Closing questions when they were solved in the comments is common practice here in TeX.SE. There was only a minor problem in the code which was solved by user ABC. He decided to give his answer as a comment, which is also common practice. Repost a comment as an answer of someone else is at least in my opinion inappropriate. It is the author´s choice whereas he posts it as an answer or not.

    Secondly, I cannot close questions. I can only suggest to close them. Other users then have to vote if the closure is justified or not. They can contradict by voting to “leave it open”.

    – Roland Nov 09 '21 at 22:54
  • @user202729 Finally, you can still leave an answer as a comment or if you think that you have an answer that really helps OP and is too long for a comment you can vote to reopen. – Roland Nov 09 '21 at 22:55
  • @Roland ??? only user with some (3k?) rep can vote to reopen. Besides the site isn't really designed to work like that. – user202729 Nov 10 '21 at 00:12
  • @user202729 3 question marks is always a good way skipping all my arguments. You could for example ask in the comments that a user with >3k reputation casts a reopen vote if you give a good reason in the comments. Regardless how it is designed, it is not forbidden, there is no rule against it, it is widely practiced and you can (of course) have a different opinion on that. If you want to complain about how things are handled here and you want to change that, you can start a discussion on Meta. – Roland Nov 10 '21 at 00:37
  • @user202729 Besides that, as a said, I do not close questions alone, other people can vote against it. Therefore, it makes absolutely no sense complaining under closure votes (which is an expression of my opinion, which I am also allowed to express). This is also the last thing I am writing to this issue, since we should avoid starting long discussions in comments – Roland Nov 10 '21 at 00:38

1 Answers1

0

Mostly off-topic ...

By use of matrix for nodes placement, math function int at determining nodes coordinates:

\documentclass[margin=3mm]{standalone}
%Tikz
\usepackage{tikz}
\usetikzlibrary{matrix}

\begin{document} \begin{tikzpicture}[ fw/.style = {fill=white}, ] \matrix (m) [matrix of math nodes, nodes={circle, draw, semithick, fill=yellow!30, inner sep=0pt, minimum size=1.5em, anchor=west}, column sep = 1em, row sep=1em, ] { & x_1 & |[fw]| \bar{x}_1 & & x_2 & |[fw]| x_2 & & |[fw]| x_3 & \bar{x}_3\ |[fw]| x_1 & & & & & & & & \ \bar{x}_2 & & & & & & & & \ \bar{x}_3 & & & & & & & & \ & x_1 & |[fw]| x_2 & \bar{x}_3 & & & & & \ }; \foreach \i [evaluate=\i as \j using int(\i+1)] in {2,5,8} \draw (m-1-\i) -- (m-1-\j);

\draw (m-2-1) -- (m-3-1) -- (m-4-1) to [bend left=45] (m-2-1);

\draw (m-5-2) -- (m-5-3) -- (m-5-4) to [bend left=45] (m-5-2);

\draw (m-1-2) -- (m-2-1) (m-1-6) -- (m-3-1) (m-1-9) -- (m-4-1); \draw (m-1-3) -- (m-5-2) (m-1-5) -- (m-5-3) (m-1-9) -- (m-5-4);

\end{tikzpicture} \end{document}

enter image description here

Note: using \foreach loop image code is not significant shorter ...

Zarko
  • 296,517