2

I'm building a tree graph and started from creating vertices

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\usepackage[active,tightpage]{preview}
\PreviewEnvironment{tikzpicture}
\setlength\PreviewBorder{10pt}

\definecolor{lavander}{cmyk}{0,0.48,0,0}
\definecolor{violet}{cmyk}{0.79,0.88,0,0}
\definecolor{burntorange}{cmyk}{0,0.52,1,0}

\def\lav{lavander!90}
\def\oran{orange!30}

\tikzstyle{vertices}=[draw,circle,violet,bottom color=\lav,
                  top color= white, text=violet,minimum width=10pt]
\tikzstyle{namedvertices}=[draw,circle,burntorange, left color=\oran,
                       text=violet,minimum width=12pt]
\begin{document}
\begin{tikzpicture}[auto, thick]
    \pgfmathsetmacro{\xoffset}{1}
    \pgfmathsetmacro{\yoffset}{1.5}
    % Place named vertices
    \foreach \vertex/\name in
        {
            {(0,0)/t}, 
            {(0 * \xoffset, 2 * \yoffset)/v}, 
            {(1 * \xoffset, -3 * \yoffset)/a}, 
            {(-4 * \xoffset, -4 * \yoffset)/u}, 
            {(0 * \xoffset, -4 * \yoffset)/b}
        }
        \node[namedvertices] (\name) at \vertex {};

\end{tikzpicture}
\end{document}

Unfortunately, compiling this i'm getting

Package tikz Error: Cannot parse this coordinate.

in the line of last vertex. And the result looks like enter image description here

If i remove just the last vertex, i.e. stay with 4 instead of 5, everything compiles fine.

What am I doing wrong? Or what should i change in order to handle more than 4 nodes?

Update: I've solved the problem by doing

{(0 * \xoffset, -4 * \yoffset)/b}%

So maybe now,

I want to understand why it solved something? Initially no hidden symbols were written after the closing } in the first place.

6
  • In your foreach tha first \vertex is set to {(0,0)/t} and your \name is empty. You should delete {} or keep them like this {(0,0)}/t.
    – Kpym
    Mar 30, 2018 at 6:54
  • Why it didn't affect me when i've used numbers instead of \offset's?
    – dEmigOd
    Mar 30, 2018 at 7:07
  • 1
    Into the list of the \foreach, add % just after the last }: [...]* \yoffset)/b}% Mar 30, 2018 at 7:09
  • @Kpym, evidently (after i started adding edges) your solution is not working - it claims b is not known, reversing to the }% solution indeed solves. Strange.
    – dEmigOd
    Mar 30, 2018 at 7:17
  • Yes, as @PaulGaborit says, you have to add % after the last one to remove the white spaces (new line). But in any case you do not need all this {}.
    – Kpym
    Mar 30, 2018 at 14:48

1 Answer 1

4

TikZ works by parsing the input stream via looking for patterns. And a brace pair {} is to group the input arguments. When you are bracing the items, you basically make them a single argument for vertex.

When TikZ sees at it switches to coordinate parsing mode and it (note that it strips the braces because they are the input to the foreach macro) and finds the parenthesis ( now it takes everything until the first ) as the coordinate expression and the rest is left in the input stream namely /b and because inside TikZ environment \nullfont is on nothing that TikZ don't care gets printed. That's why b is not taken as the name of the node because it is thrown away.

Regarding the % issue, again TikZ decides on the loop array by comparing things literally even to the whitespaces and a linebreak is a whitespace character. A similar issue can be seen here PGF's \foreach when list requires completion. Hence this works:

\draw (b ) -- (a);% Notice the whitespace after b

A good practice is to avoid bracing multiparameter foreach patterns, or bracing each item in the ../../.. stream individually.

You must log in to answer this question.

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