1

I have the following code generating a colored subgraph:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{graphs,graphs.standard}

\usepackage{etoolbox}

\begin{document}

\begin{tikzpicture}[transform shape]
  \foreach \x in {1,...,6}{%
    \pgfmathparse{(\x-1)*360/6 + 90}
    \ifboolexpr{test {\ifnumcomp{\x}{=}{1}}}
    {\node[draw=cyan, circle, inner sep=0.15cm] (N-\x) at (\pgfmathresult:2cm) {$X_{\x}$};}
    {\node[draw, circle,inner sep=0.15cm] (N-\x) at (\pgfmathresult:2cm) {$X_{\x}$};}
  } 
  \foreach \x [count=\xi from 1] in {1,...,6}{%
    \foreach \y in {\x,...,6}{%
      \ifboolexpr{test {\ifnumcomp{\x}{=}{1}}}
      {\path (N-\x) edge[thick, draw=cyan,-] (N-\y);}
      {\path (N-\x) edge[thick, -] (N-\y);} 
  }
}
\end{tikzpicture}

\end{document}

If I change:

    {\node[draw=cyan, circle, inner sep=0.15cm] (N-\x) at (\pgfmathresult:2cm) {$X_{\x}$};}
    {\node[draw, circle,inner sep=0.15cm] (N-\x) at (\pgfmathresult:2cm) {$X_{\x}$};}

to:

    {\node[draw=cyan, thick, circle, inner sep=0.15cm] (N-\x) at (\pgfmathresult:2cm) {$X_{\x}$};}
    {\node[draw, thick, circle,inner sep=0.15cm] (N-\x) at (\pgfmathresult:2cm) {$X_{\x}$};}

The graph collapses like so: https://i.snipboard.io/95lKAD.jpg

What could be happening?

1 Answer 1

2

Welcome! This is because \pgfmathresult gets overwritten the routine that sets the line width. (In this case it is caused by thick, which is defined as

 \tikzset{thick/.style={line width=0.8pt}}%

where line width calls \pgfsetlinewidth, which contains

\pgfmathsetlength\pgflinewidth{#1}

where \pgfmathsetlength contains a \pgfmathparse, which overrides the "outside" value of \pgfmathresult.) This is a well-known issue, and the perhaps safest way to solve it is not to use \pgfmathparse followed by other stuff that parses expressions. Also, you do not need to preparse these expressions, TikZ will parse them anyway. (Also you are not using any of the libraries and you can work with \ifnum instead of the etoolbox stuff.) Here is one of many ways to solve the problem.

\documentclass{article}
\usepackage{tikz}
\begin{document}

\begin{tikzpicture}[transform shape]
  \foreach \x in {1,...,6}{%
    \ifnum\x=1
     \node[draw=cyan, thick,circle, inner sep=0.15cm] (N-\x) at ({(\x-1)*360/6 + 90}:2cm) {$X_{\x}$};
    \else
     \node[draw,thick, circle,inner sep=0.15cm] (N-\x) at ({(\x-1)*360/6 + 90}:2cm) {$X_{\x}$};
    \fi
  } 
  \foreach \x  in {1,...,6}{%
    \foreach \y in {\x,...,6}{%
      \ifnum\x=1
       \path (N-\x) edge[thick, draw=cyan,-] (N-\y);
      \else 
       \path (N-\x) edge[thick, -] (N-\y);
      \fi
  }
}
\end{tikzpicture}
\end{document}

enter image description here

You can also use \pgfmathsetmacro to store the result of the computation in a macro that does not get overwritten, or use the evaluate key that comes with foreach, just to mention some of the alternatives that exist. Here is a shorter version of that kind.

\documentclass{article}
\usepackage{tikz}
\begin{document}

\begin{tikzpicture}
  \foreach \x [evaluate=\x as \myangle using {(\x-1)*360/6 + 90}]in {1,...,6}{%
    \ifnum\x=1
     \node[draw=cyan, thick,circle, inner sep=0.15cm] (N-\x) at (\myangle:2cm) {$X_{\x}$};
    \else
     \node[draw,thick, circle,inner sep=0.15cm] (N-\x) at (\myangle:2cm) {$X_{\x}$};
    \fi
    \ifnum\x>1
    \foreach \y in {1,...,\x}{%
      \ifnum\y=1
       \path (N-\x) edge[thick, draw=cyan,-] (N-\y);
      \else 
       \path (N-\x) edge[thick, -] (N-\y);
      \fi}
    \fi  
}
\end{tikzpicture}
\end{document}

If you dig long enough, you can make it even shorter, perhaps at the expense of legibility.

One thing might be a bit more important: apart from the fact that [count=\xi from 1] didn't have a purpose here, I would not use \xi for the macro, if you ever want to print the corresponding Greek letter in the loop you will be surprised.

ADDENDUM: Some version for arbitrary highlighting.

\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[declare function={ihighlight=3;}]
  \foreach \x [evaluate=\x as \myangle using {(\x-1)*360/6 + 90}]in {1,...,6}{%
    \pgfmathtruncatemacro{\itest}{\x==ihighlight}
    \ifnum\itest=1
     \node[draw=cyan, thick,circle, inner sep=0.15cm] (N-\x) at (\myangle:2cm) {$X_{\x}$};
    \else
     \node[draw,thick, circle,inner sep=0.15cm] (N-\x) at (\myangle:2cm) {$X_{\x}$};
    \fi
    \ifnum\x>1
    \foreach \y in {1,...,\x}{%
      \pgfmathtruncatemacro{\itest}{\x==ihighlight || \y==ihighlight}
      \ifnum\itest=1
       \path (N-\x) edge[thick, draw=cyan,-] (N-\y);
      \else 
       \path (N-\x) edge[thick, -] (N-\y);
      \fi}
    \fi  
}
\end{tikzpicture}
\end{document}
3
  • Can you explain where the N is coming from?
    – user3180
    May 12, 2020 at 2:01
  • Also, I just realized, this code fails if you are trying to highlight an edge that is not coming into node 1
    – user3180
    May 12, 2020 at 2:06
  • 1
    @user3180 Which N? The node name N-\x is already in your code. And yes, it is true that the second code is specifically for the case that you want to highlight node number 1, but the first one does the same comparisons as yours. Anyway, I added a version that allows you to change the highlighted node at will.
    – user194703
    May 12, 2020 at 2:47

You must log in to answer this question.

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