TikZ:在不移动锚点的情况下翻转形状

TikZ:在不移动锚点的情况下翻转形状

在 TikZ 中绘制游戏树时,我使用向上的三角形表示最大化玩家 1,使用向下的三角形表示最小化玩家 2。但是,我当前的解决方案不会使它们水平对齐(左图)。我可以使用 yshift 使它们对齐(中间),但这会弄乱它们的子元素 E、F,不会保留角度,并带来其他问题(右)。
如何定义 pl1 和 pl2 样式以对齐 B 和 C 而不会弄乱其他任何东西?

在此处输入图片描述

\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}

编辑:[anchor=north]当我想让树朝不同的方向生长时,该解决方案不起作用。有没有办法避免这种情况(或有forest帮助)?

\documentclass{独立} \usepackage{tikz} \usetikzlibrary{形状}

\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}

锚点=北解决方案的问题

答案1

Z 默认使用基线进行垂直对齐。如果我理解正确的话,您希望拥有形状的边界。为了实现这一点,您可能需要使用锚点。

\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}

在此处输入图片描述

然而,这个答案的主要目的是引起您的注意forest,这可能会大大简化这些事情。

答案2

正如建议的那样,forest可以更轻松地完成更好的工作。我不知道树应该是什么样子,所以这可能需要调整。例如,您可能希望最后一棵树中有一条直线或fit=band全局使用。

\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}

与森林对齐的节点

相关内容