# Tikz command disrupted inside tikz macro

I need to draw some diagrams in tikz for which the labels have trees as indices. I render these trees as tikz diagrams, too. Even with simple diagrams, such as an arrow between two nodes, if a tree is inserted as label of the arrow, then the result is very unpleasant.

Here is a MWE.

\documentclass{standalone}

\usepackage{tikz}

\newcommand{\TreeW}
{
\tikz
{
\draw[-] (-.1,.2) -- (0,0) -- (.1,.2); \draw[-] (0,0) -- (0,.2);
}
}

\newcommand{\TreeV}
{%
{%
\mathchoice%
{\tikz \draw[-]                   (-.1,.2) -- (0,0) -- (.1,.2);}%
{\tikz \draw[-]                   (-.1,.2) -- (0,0) -- (.1,.2);}%
{\tikz \draw[-] [thin, scale=.75] (-.1,.2) -- (0,0) -- (.1,.2);}%
{\tikz \draw[-] [thin, scale= .5] (-.1,.2) -- (0,0) -- (.1,.2);}%
}
}

\begin{document}
\begin{tikzpicture}[scale=1.5]
\node (s0) at (180:2) {$\TreeW_{\TreeV}$};
\node (s1) at (0,0) {$\TreeV$};
\draw[->] [shorten >=10pt, shorten <=10pt] (s0) --
node [above, near start] {$\TreeW$} node [below, near end] {\hbox{$\TreeV$}} (s1);
\end{tikzpicture}
\end{document}


This gives me the following result.

I tried to search a bit for similar problems, but I haven't encountered any; although it is most likely a well-known issue.

• Instead of defining a command, you can define a pic. See TikZ manual 3.1.4 on page 259. – AndréC Aug 19 at 16:13

The problem with your approach is that it nests tikzpictures. This is to be avoided. The standard way to avoid the problems is to use \saveboxes. (A pic won't work here since you use \mathchoice and from your previous question I take that the reason why you do not use tikz-cd for this diagram is that you want to keep the example minimal. The following works also with tikz-cd.)

\documentclass{standalone}
\usepackage{tikz}
\newsavebox\TreeWbox
\sbox\TreeWbox{\tikz{%
\draw[-] (-.1,.2) -- (0,0) -- (.1,.2); \draw[-] (0,0) -- (0,.2);
}}
\newsavebox\TreeVbox
\sbox\TreeVbox{\tikz \draw[-]  (-.1,.2) -- (0,0) -- (.1,.2);}

\begin{document}
\newcommand{\TreeW}{\usebox\TreeWbox}
\newcommand{\TreeV}{\mathchoice{\usebox\TreeVbox}{%
\usebox\TreeVbox}{%
\scalebox{0.8}{\usebox\TreeVbox}}{%
\scalebox{0.6}{\usebox\TreeVbox}}}

\begin{tikzpicture}[scale=1.5]
\node (s0) at (180:2) {$\TreeW_{\TreeV}$};
\node (s1) at (0,0) {$\TreeV$};
\draw[->] [shorten >=10pt, shorten <=10pt] (s0) --
node [above, near start] {$\TreeW$} node [below, near end] {\hbox{$\TreeV$}} (s1);
\end{tikzpicture}
\end{document}


Your code works perfectly as long as you add the tips=never option that prevents you from drawing the arrow and don't forget to delete the [-] option you placed to delete this arrow without success. The arrow drawing options are explained on page 188 of the TikZ 3.1.4b manual.

\documentclass[border=5mm,tikz]{standalone}

%\usepackage{tikz}

\newcommand{\TreeW}
{
\tikz[tips=never]
{
\draw[] (-.1,.2) -- (0,0) -- (.1,.2); \draw[] (0,0) -- (0,.2);
}
}

\newcommand{\TreeV}
{%
{%
\mathchoice%
{\tikz[tips=never] \draw                   (-.1,.2) -- (0,0) -- (.1,.2);}%
{\tikz[tips=never] \draw                   (-.1,.2) -- (0,0) -- (.1,.2);}%
{\tikz[tips=never] \draw [thin, scale=.75] (-.1,.2) -- (0,0) -- (.1,.2);}%
{\tikz[tips=never] \draw [thin, scale= .5] (-.1,.2) -- (0,0) -- (.1,.2);}%
}
}

\begin{document}
\begin{tikzpicture}[scale=1.5]
\node (s0) at (180:2) {$\TreeW_{\TreeV}$};
\node (s1) at (0,0) {$\TreeV$};
\draw[->] [shorten >=10pt, shorten <=10pt
] (s0) --
(s1) node [above, near start] {$\TreeW$} node [below, near end] {\hbox{$\TreeV$}};
\end{tikzpicture}
\end{document}