TikZ 将矩形分成 2 个

TikZ 将矩形分成 2 个

我正在绘制一幅图,其中有不同形状和颜色的节点。对于椭圆形节点,我按预期得到 2 个半部分,但对于矩形节点,我得到 4 个部分。下半部分的文本未显示。我不明白为什么。这是我的 MWE:

\documentclass[11pt]{beamer}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usetheme{default}

\usepackage{tikz} 
\usetikzlibrary{shapes} 
\begin{document}
   \begin{frame}[plain]
       \maketitle
   \end{frame}
   
   \begin{frame}
       \frametitle{sth}
       \begin{figure}
           \tikzset{
               attacker/.style = {
                   color = red,
                   shape = ellipse split,
                   text  = black},
               defender/.style = {
                   color = green,
                   shape = rectangle split,
                   text  = black}
           }
           \begin{tikzpicture}[
           every node/.style = {
               draw, 
               text width = },
           grow = down, 
           level 1/.style = {sibling distance=2cm},
           level 2/.style = {sibling distance=1cm},
           level distance = 2.5cm,
           ]
           \node[attacker] (Goal) {X \nodepart{lower} {$\vee$}} 
               child { node[defender] (partition) {Y \nodepart{lower}{$\wedge$}}
               };
           \end{tikzpicture}
       \end{figure}
   \end{frame}
\end{document}

答案1

欢迎来到 TeX.SE!

抱歉,但是代码存在许多问题:

  • 对于多部分形状,您应该加载tikzshapes.multipart
  • rectangle split尚未定义\nodepart{lower}。您应该定义有多少个部分以及您希望在哪个部分包含文本(\vedge
  • 要有tikzset框架内部,框架必须有选项fragile

我猜你希望的是以下这些:

\documentclass[11pt]{beamer}
\usepackage{tikz}
\usetikzlibrary{shapes.multipart}


\begin{document}
\begin{frame}[fragile]
\frametitle{sth}
\begin{figure}
\tikzset{
   attacker/.style = {
       color = red,
       shape = ellipse split,
       text  = black},
   defender/.style = {
       color = green,
       shape = rectangle split,
       rectangle split parts=#1,
       text  = black}
        }
   \begin{tikzpicture}[
every node/.style = {draw},
             grow = down,
   level 1/.style = {sibling distance=2cm},
   level 2/.style = {sibling distance=1cm},
   level distance = 2.5cm,
                        ]
\node[attacker] (Goal) {X \nodepart{lower} {$\vee$}}
    child { node[defender=2] (partition) {Y \nodepart{two}{$\wedge$}} };
           \end{tikzpicture}
       \end{figure}
   \end{frame}
\end{document}

在此处输入图片描述

然而,不清楚为什么对于这个简单的图像您要tree构造......

编辑: 正如我在你的评论中提到的那样(作为对你的评论的回复), rectangle split没有定义部分。你需要明确说明哪个部分(第一部分除外,这不是必须的,但如果你将其明确声明为或lower,也不会造成损害)是节点内容。所以你想做的事情是不可能的。textone

但是,如果由于某种原因您只喜欢使用两部分multipart形状,其中第二部分您喜欢命名lower,那么您可以做的是:

\documentclass[11pt]{beamer}
\usepackage{tikz}
\usetikzlibrary{shapes.multipart}
\newcommand\Lower{\nodepart{two}}% you should be aware, 
                                 % that`lower` is already defined in `tikz`

\begin{document}
\begin{frame}[fragile]
\frametitle{sth}
\begin{figure}
\tikzset{
   attacker/.style = {
       color = red,
       shape = ellipse split,
       text  = black},
   defender/.style = {
       color = green,
       shape = rectangle split,
       rectangle split parts=2,
       text  = black}
        }
   \begin{tikzpicture}[
every node/.style = {draw},
             grow = down,
   level 1/.style = {sibling distance=2cm},
   level 2/.style = {sibling distance=1cm},
   level distance = 2.5cm,
                        ]
\node[attacker] (Goal) {X \nodepart{lower} {$\vee$}}
    child { node[defender] (partition) {Y \Lower $\wedge$} };
           \end{tikzpicture}
       \end{figure}
   \end{frame}
\end{document}

结果和以前一样。

相关内容