改变节点厚度会导致子图折叠

改变节点厚度会导致子图折叠

我有以下代码生成彩色子图:

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

如果我改变:

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

到:

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

该图折叠如下: https://i.snipboard.io/95lKAD.jpg

会发生什么事?

答案1

欢迎!这是因为\pgfmathresult覆盖了设置线宽的例程。(在这种情况下,它是由 引起的thick,其定义为

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

其中线宽调用\pgfsetlinewidth,其中包含

\pgfmathsetlength\pgflinewidth{#1}

其中\pgfmathsetlength包含一个\pgfmathparse,它将覆盖 的“外部”值\pgfmathresult。)这是一个众所周知的问题,解决这个问题最安全的方法可能是不要使用\pgfmathparse后面跟着其他解析表达式的东西。此外,你不需要预解析这些表达式,Ti无论如何,Z 都会解析它们。(另外,您不使用任何库,而是可以使用\ifnum它们来代替etoolbox。)这是解决问题的众多方法之一。

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

在此处输入图片描述

您还可以使用\pgfmathsetmacro将计算结果存储在不会被覆盖的宏中,或者使用evaluate附带的键foreach,这只是一些存在的替代方案。以下是此类的较短版本。

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

如果你挖掘得足够长,你可以使它变得更短,但也许会以牺牲可读性为代价。

有一件事可能更重要一些:除了[count=\xi from 1]这里没有目的之外,我不会使用\xi宏,如果你想在循环中打印相应的希腊字母,你会感到惊讶。

附录:某些版本可进行任意突出显示。

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

相关内容