4

When drawing game trees in TikZ, I use upwards-pointing triangle to denote the maximizing player 1 and downwards-pointing triangle for the minimizing player 2. However, my current solution doesnt make them horizontally aligned (left picture). I can get them aligned by using yshift (middle), but this messes up their children E, F, doesn't preserve angles, and poses other problems (right).
How to define the pl1 and pl2 styles which aligns B and C without messing up anything else?

enter image description here

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes}

\begin{document}
\tikzset{
    pl1/.style = {draw, inner sep = 0, minimum size = 2.5em,
        shape = regular polygon, regular polygon sides = 3
    },
    pl2/.style = {pl1, shape border rotate=180}
}
% The children Y and Z are not aligned
\begin{tikzpicture}
\node[pl1]{X}
    child{node[pl2]{Y}}
    child{node[pl1]{Z}};
\end{tikzpicture}
% B and C are aligned...
\begin{tikzpicture}
\node[pl1]{A}
    child{node[pl2,yshift=0.13*2.5em]{B}}
    child{node[pl1,,yshift=-0.13*2.5em]{C}};
\end{tikzpicture}

% ...but this creates other problems
\begin{tikzpicture}
\node[pl1]{A}
    child{node[pl2,yshift=0.13*2.5em]{B} child{node{E}} }
    child{node[pl1,,yshift=-0.13*2.5em]{C} child{node{F}} };
\end{tikzpicture}
\end{document}

EDIT: The [anchor=north] solution does not work when I want the tree to grow in a different direction. Is there a way to avoid that (or would forest help)?

\documentclass{standalone} \usepackage{tikz} \usetikzlibrary{shapes}

\begin{document}
\tikzset{
    pl1/.style = {draw, inner sep = 0, minimum size = 2.5em,
        shape = regular polygon, regular polygon sides = 3, anchor=north
    },
    pl2/.style = {pl1, shape border rotate=180}
}
% The children Y and Z are not aligned
\begin{tikzpicture}[level distance=1cm]
\node(1)[pl1]{$h_1$}
    child[grow=right]{node(2)[pl2]{$h_2$}}
;
\end{tikzpicture}
\end{document}

Issue with the anchor=north solution

  • You only have to add anchor=north to have the very left pic symmetric. – user121799 Nov 30 '18 at 16:03
2

TikZ by default uses the baseline to do the vertical alignment. If I understand you correctly, you want to have the boundary of the shape. To achieve this, you might want to play with anchors.

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes}

\begin{document}
\tikzset{
    pl1/.style = {draw, inner sep = 0, minimum size = 2.5em,
        shape = regular polygon, regular polygon sides = 3,anchor=north
    },
    pl2/.style = {pl1, shape border rotate=180}
}
% The children Y and Z are not aligned
\begin{tikzpicture}[level distance=1cm]
\node[pl1]{X}
    child{node[pl2]{Y}}
    child{node[pl1]{Z}};
\end{tikzpicture}
% B and C are aligned...
\begin{tikzpicture}
\node[pl1]{A}
    child{node[pl2]{B}}
    child{node[pl1]{C}};
\end{tikzpicture}

% ...but this creates other problems
\begin{tikzpicture}
\node[pl1]{A}
    child{node[pl2]{B} child{node[anchor=north](E){E}} }
    child{node[pl1]{C} child{node{F}} };
\end{tikzpicture}
\end{document}

enter image description here

The main purpose of this answer is, however, to draw your attention to forest, which may simplify these things a lot.

  • Thank you, that does solve this particular example. Is there a way of doing this without having to add the [anchor=north] part to F? Modifying the child for pl2 nodes? Would forest avoid this hassle? – Vojtěch Kovařík Dec 3 '18 at 11:42
  • This solution also has an issue when I need the tree to grow in a different direction -- see the edit of the original question. – Vojtěch Kovařík Dec 3 '18 at 11:55
1

As suggested, forest can do a better job more easily. I don't know just what the trees should look like, so this may need adjustment. For example, you might want a straight line in the last tree or to use fit=band globally.

\documentclass[border=9pt]{standalone}
\usepackage[]{forest}
\usetikzlibrary{shapes}
\tikzset{
  pl1/.style = {draw, inner sep = 0, minimum size = 2.5em, shape = regular polygon, regular polygon sides = 3, anchor=north },
  pl2/.style = {pl1, shape border rotate=180}
}

\begin{document}
\begin{forest}
  [X, pl1 [Y, pl2][Z, pl1]]
\end{forest}
\begin{forest}
  [A, pl1 [B, pl2 [E]][C, pl1 [F]]]
\end{forest}
\begin{forest}
  for tree={math content, grow'=0}
  [h_1, pl1 [h_2, pl2]]
\end{forest}
\end{document}

aligned nodes with Forest

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.