TikZ 不透明度计算出错

TikZ 不透明度计算出错

我正在尝试在 tikz 中绘制蓝色对象和红色对象之间的一些对齐。我弄清楚了如何绘制一些基本图形(即使样式可能不是您读过的最好的 tikz,但它似乎工作正常)。

然而,当我尝试编写一些程序来获得边缘上的随机不透明度时,我观察到一种奇怪的行为:当用 XeLaTeX 编译时,图形呈现良好,但在 pdflatex 编译时,图形混乱了(边缘不是连接节点并且超出了节点空间并且出现随机的 0)。

我不知道问题出在哪里,希望得到任何帮助。我加入了一个基本工作图形和有问题的图形的代码。

我也希望得到关于如何生成分布以便边缘随机着色的建议。目前,即使在 XeLaTeX 上,由于我使用的算法,当边缘位于图形左侧时,它们更有可能被强烈着色。我对 TikZ 不够熟悉,无法实现更复杂的算法。

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{arrows}

\begin{document}
\tikzset{%
  r/.style = {circle, red, draw=red, very thick},%
  b/.style = {circle, blue, draw=blue, very thick},%
  > = triangle 60%
}

works:\\

\begin{tikzpicture}
  \node[r] (X-A) {};
  \node[b] (Y-A) [below of=X-A] {};
  \newcommand\previous{A}
  \foreach \current in {B, C, D, E} {
    \node[r] (X-\current) [right of=X-\previous] {};
    \node[b] (Y-\current) [right of=Y-\previous] {};
    \xdef\previous{\current}
  }
  \foreach \x/\y in {A/A, B/C, C/E, D/D, E/E} {
    \path (X-\x) edge [->] (Y-\y);
  }
  \foreach \x/\y in {A/A, B/D, C/B, D/E, E/C} {
    \path (Y-\x) edge [->] (X-\y);
  }
\end{tikzpicture}

doesn't work:\\

\begin{tikzpicture}
  \node[r] (X-A) {};
  \node[b] (Y-A) [below of=X-A] {};
  \renewcommand\previous{A}
  \foreach \current in {B, C, D, E} {
    \node[r] (X-\current) [right of=X-\previous] {};
    \node[b] (Y-\current) [right of=Y-\previous] {};
    \xdef\previous{\current}
  }
  \newcommand\total{}
  \foreach \x in {A, B, C, D, E} {
    \renewcommand\total{100}
    \foreach \y in {A, B, C, D} {
      \pgfmathsetmacro{\r}{random(0,\total)}
      \pgfmathsetmacro{\o}{\r / 100}
      \pgfmathsetmacro{\newtotal}{\total - \r}
      \xdef\total{\newtotal}
      \path (X-\x) edge [-,opacity=\o] (Y-\y);
    }
    \pgfmathsetmacro{\o}{\total / 100}
    \path (X-E) edge [-,opacity=\o] (Y-E);
  }
\end{tikzpicture}
\end{document}

答案1

不要对以下内容做出全局定义\total

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{arrows}

\begin{document}
\tikzset{%
  r/.style = {circle, red, draw=red, very thick},%
  b/.style = {circle, blue, draw=blue, very thick},%
  > = triangle 60%
}

works:

\begin{tikzpicture}
  \node[r] (X-A) {};
  \node[b] (Y-A) [below of=X-A] {};
  \newcommand\previous{A}
  \foreach \current in {B, C, D, E} {
    \node[r] (X-\current) [right of=X-\previous] {};
    \node[b] (Y-\current) [right of=Y-\previous] {};
    \xdef\previous{\current}
  }
  \foreach \x/\y in {A/A, B/C, C/E, D/D, E/E} {
    \path (X-\x) edge [->] (Y-\y);
  }
  \foreach \x/\y in {A/A, B/D, C/B, D/E, E/C} {
    \path (Y-\x) edge [->] (X-\y);
  }
\end{tikzpicture}

works:

\begin{tikzpicture}
  \node[r] (X-A) {};
  \node[b] (Y-A) [below of=X-A] {};
  \renewcommand\previous{A}
  \foreach \current in {B, C, D, E} {
    \node[r] (X-\current) [right of=X-\previous] {};
    \node[b] (Y-\current) [right of=Y-\previous] {};
    \xdef\previous{\current}
  }
  \newcommand\total{}
  \foreach \x in {A, B, C, D, E} {
    \renewcommand\total{100}
    \foreach \y in {A, B, C, D} {
      \pgfmathsetmacro{\r}{random(0,\total)}
      \pgfmathsetmacro{\o}{\r / 100}
      \pgfmathsetmacro{\newtotal}{\total - \r}
      \def\total{\newtotal}
      \path (X-\x) edge [-,opacity=\o] (Y-\y);
    }
    \pgfmathsetmacro{\o}{\total / 100}
    \path (X-E) edge [-,opacity=\o] (Y-E);
  }
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容