1

I am using the trees tikzlibrary combined with some pgfmath commands to draw the ternary tree below:

enter image description here

To each vertex u_i, a certain v_i is associated. So far, all of these v_i's are labelled with v_1, but I would actually like the numbering to correspond to the u_i's, so that v_1 is adjacent to u_1, v_2 is adjacent to u_2, and so on.

Code:

\documentclass{minimal}
\usepackage{tikz}
\usetikzlibrary{trees}
\begin{document}
\pagestyle{empty}

\def\d{3} %degree \newlength{\leveldist} %level distance

\centering \begin{tikzpicture}[grow cyclic,circle,sibling angle=360/\d,draw] \setlength{\leveldist}{10mm}; \tikzstyle{level 1}=[level distance=1.5\leveldist] \tikzstyle{level 2}=[level distance=\leveldist,sibling angle=60] \tikzstyle{level 3}=[level distance=\leveldist,sibling angle=40] \tikzstyle{every node}=[inner sep=0mm,minimum size=1mm,draw] \node (Ac) {$u$} child foreach \A in {1,...,\d} { node (A\A) {} child foreach \B in {2,...,\d}
{ node (A\A-\B) {$u_{\pgfmathparse{(\A-1)2+\B-1}\pgfmathprintnumber{\pgfmathresult}}$} child foreach \C in {2,...,\d} { node (A\A-\B-\C) {\pgfmathparse{ifthenelse(array({0,1,1,1,0,1},(\A-1)2+\B-2)==\C-2,"$v_1$","")} \pgfmathresult} } } }; \end{tikzpicture}

\end{document}

Here, using "$v_{\pgfmathparse{(\A-1)*2+\B-1}\pgfmathprintnumber{\pgfmathresult}}$" instead of "$v_1$" to label the v_i (as it worked for the u_i in the line above) results in the compilation not terminating, probably because of the nested use of the two \pgfmathparse commands.

An alternative approach could be to introduce some variable to count the index instead of doing the computation in situ, but I was not able to make this work inside this structure.

  • Don't use the minimal documentclass. It is way too minimal for most purposes. – Henri Menke Oct 29 '21 at 12:10
  • Thank you for the advice - I have changed the documentclass to article (in my file) now. The problem persists, however. – user631620 Oct 29 '21 at 12:17
  • Note that it is advised not to use \tikzstyle anymore, but instead use \tikzset{<key-name>/.style={<stuff>}} (this will most likely not fix your problem though). – Skillmon Oct 29 '21 at 19:08

1 Answers1

2

Description

As you already noticed, it seems to be impossible or at least difficult to use nested \pgfmathparse. The problem is, that \pgfmathparse is mainly used for arithmetic calculations and not for string building, but in this case a string must be concatenated with a calculated number. Therefore, I make use of another package etoolbox and it's command \ifdefstring.

Adaptations

Code

\documentclass{article}

\usepackage{etoolbox} \usepackage{tikz} \usetikzlibrary{trees}

\begin{document} \pagestyle{empty}

\def\d{3} %degree \newlength{\leveldist} %level distance

\centering \begin{tikzpicture}[ grow cyclic,circle,sibling angle=360/\d,draw, level 1/.style={level distance=1.5\leveldist}, level 2/.style={level distance=\leveldist,sibling angle=60}, level 3/.style={level distance=\leveldist,sibling angle=40}, every node/.style={inner sep=0mm,minimum size=1mm,draw}, ]

\setlength{\leveldist}{10mm};

\node (Ac) {$u$} child foreach \A in {1,...,\d} {
    node (A\A) {} child foreach \B in {2,...,\d} {
        node (A\A-\B) {$u_{\pgfmathparse{(\A-1)*2+\B-1}\pgfmathprintnumber{\pgfmathresult}}$} child foreach \C in {2,...,\d} {
            node (A\A-\B-\C) {%
                \pgfmathparse{ifthenelse(array({0,1,1,1,0,1},(\A-1)*2+\B-2)==\C-2, (\A-1)*2+\B-1, 0)}
                \ifdefstring{\pgfmathresult}{0}{}{$v_{\pgfmathprintnumber{\pgfmathresult}}$}
            }
        }
    }
};

\end{tikzpicture}

\end{document}

Result

enter image description here

dexteritas
  • 9,161