3

I'm experiencing some weirdness in making a simple TikZ diagram (a comb-shaped graph with variable number of nodes). The weirdness comes from the style applied to the edge paths. Here's the (minimal) example:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shapes,arrows}
\begin{document}

\begin{tikzpicture}[auto,scale=1.0,%
block/.style = {draw,circle,very thick,minimum size=0.5cm},%
directed/.style ={draw,-triangle 45, shorten >= 0pt, very thick}]

% Parameters
\pgfmathsetmacro{\numXnodes}{int(3)}
\pgfmathsetmacro{\strch}{2}

\foreach \i in {1,...,\numXnodes}{
    \path (\strch*\i-\strch, 0) node[block] (x\i) {\i};
    \path (x\i) ++(0,-\strch) node[block] (z\i) {\pgfmathparse{int(\numXnodes+\i)}\pgfmathresult};
    \path (x\i) [directed] -- (z\i);
}

\pgfmathsetmacro{\nnmo}{{int(\numXnodes-1)}}

\foreach \i in {1,...,\nnmo} {
    \pgfmathparse{int(\i+1)}
    \draw (x\i) [draw,-triangle 45,thick] -- (x\pgfmathresult);
}

\end{tikzpicture}
\end{document}

The above code draws a graph with the right structure/directionality. However, the edges between "x" nodes are very thin.

The issue is that if I change the style on the line \draw (x\i) [draw,-triangle 45] -- (x\pgfmathresult); to \draw (x\i) [draw,-triangle 45,thick] -- (x\pgfmathresult); I get an error that says Package pgf Error: No shape named x0 is known. } (followed by: )

Any thoughts on why this error would be occurring here and not in the first set of edges drawn with the defined directed style (i.e., in the first loop)?

Thanks in advance!

1 Answer 1

4

\pgfmathresult macro is overwritten at almost every step of code so you need to be quick to use its value. Most probably, thick option causes a temporary calculation and removes your result. Instead you can use the [count=\variable from number] option.

I would really recommend not using \i as the counter since if it doesn't get expanded properly or else, you will have the greek dotless i character iota in the variable which might go unnoticed until you debug for some time. Moreover, you can also use \pgfmathtruncatemacro to get the integer part automatically.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shapes,arrows}
\begin{document}

\begin{tikzpicture}[auto,scale=1.0,%
block/.style = {draw,circle,very thick,minimum size=0.5cm},%
directed/.style ={draw,-triangle 45, shorten >= 0pt, very thick}]

% Parameters
\pgfmathtruncatemacro{\numXnodes}{3} % \def or \newcommand would also be OK here. 
\pgfmathtruncatemacro{\strch}{2}

\foreach \x in {1,...,\numXnodes}{
    \path (\strch*\x-\strch, 0) node[block] (x\x) {\x};
    \path (x\x) ++(0,-\strch) node[block] (z\x) {\pgfmathparse{int(\numXnodes+\x)}\pgfmathresult};
    \path (x\x) [directed] -- (z\x);
}

\pgfmathtruncatemacro{\nnmo}{\numXnodes-1}

\foreach \x[count=\xi from 2] in {1,...,\nnmo} {
    \draw (x\x) [draw,-triangle 45,thick] -- (x\xi);
}

\end{tikzpicture}
\end{document}

enter image description here

1
  • Thank you very much; your answer was informative on many fronts. I would like to add that \pgfmathtruncatemacro, in addition to being useful, is actually required in this example for the numXnodes macro, as \pgfmathsetmacro{int(...)} will still store a decimal.
    – Dan Levine
    Aug 7, 2012 at 18:29

You must log in to answer this question.

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