3

I am trying to draw some alignements between blue objects and red objects in tikz. I figured out how to draw some basic figures (even if the style might not be the best tikz you'll read, it seems to work ok).

However when I try to program a little to obtain random opacities on my edges, I observe a peculiar behaviour: when compiled with XeLaTeX, the figure renders fine but when compiled when pdflatex, the figure is messed up (the edges are not connecting nodes and go out of the node space and random 0s appear).

I have no idea where the problem comes from and would appreciate any help. I joined code for a basic working figure and for the problematic one.

I'd also appreciate advice on how to produce distributions so that the edges are randomly colored. At the moment, even on XeLaTeX, due to the algorithm I use, the edges are more likely to be strongly colored when they come at the left of the figure. I'm not familiar enough in TikZ to implement more sophisticated algorithms.

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

Don't make a global definition for \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}

enter image description here

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.