将数学环境和 tikz 图片放置在表格中

将数学环境和 tikz 图片放置在表格中
\usepackage{tikz}


\begin{document}

\begin{tabular}[b]{@{}ccc@{}}
\begin{tikzpicture}[grow=right,sloped]
\tikzstyle{level 1}=[level distance=2cm, sibling distance=2cm]
\tikzstyle{level 2}=[level distance=2cm, sibling distance=1.5cm]

\node[rectangle,draw]{}
  child{node[circle,draw]{}
  child{node{$P_{T|s}$}} edge from parent node[below]{$t\neq s$}
  }
  child{node[circle,draw]{}
  child{node{$z$} edge from parent node[below]{$1-p$}} 
  child{node{$x$}edge from parent node[above]{$p$}}
  edge from parent node[above]{$t=s$}  
  };
\end{tikzpicture}
&
$\sim$
&
\begin{tikzpicture}[grow=right,sloped]
\tikzstyle{level 1}=[level distance=2cm, sibling distance=2cm]
\tikzstyle{level 2}=[level distance=2cm, sibling distance=1.5cm]

\node[rectangle,draw]{}
  child{node[circle,draw]{}
  child{node{$P_{T|s}$}} edge from parent node[below]{$t\neq s$}
  }
  child{node[circle,draw]{}
  child{node{$z$} edge from parent node[below]{$1-p$}} 
  child{node{$x$}edge from parent node[above]{$p$}}
  edge from parent node[above]{$t=s$}  
  };
\end{tikzpicture} \\
\end{tabular}

\end{document}

enter image description here

真的很抱歉,我搜索了很多次,但真的找不到解决方案。我想在表格中包含两张图片和一个数学符号。我以为默认的垂直位置是居中!但我无法将 \sim 符号置于中间!提前感谢您的帮助!我很抱歉我是菜鸟!

答案1

您可以将 tikz 图片的基线设置为基线的中心:

\begin{tikzpicture}[grow=right,sloped,baseline=(current bounding box.center)]

完整代码:

\documentclass{article}
\usepackage{tikz}


\begin{document}

\begin{tabular}[b]{@{}ccc@{}}
\begin{tikzpicture}[grow=right,sloped,baseline=(current bounding box.center)]   %% changed here
\tikzstyle{level 1}=[level distance=2cm, sibling distance=2cm]
\tikzstyle{level 2}=[level distance=2cm, sibling distance=1.5cm]

\node[rectangle,draw]{}
  child{node[circle,draw]{}
  child{node{$P_{T|s}$}} edge from parent node[below]{$t\neq s$}
  }
  child{node[circle,draw]{}
  child{node{$z$} edge from parent node[below]{$1-p$}}
  child{node{$x$}edge from parent node[above]{$p$}}
  edge from parent node[above]{$t=s$}
  };
\end{tikzpicture}
&
$\sim$
&
\begin{tikzpicture}[grow=right,sloped,baseline=(current bounding box.center)]   %%% changed here
\tikzstyle{level 1}=[level distance=2cm, sibling distance=2cm]
\tikzstyle{level 2}=[level distance=2cm, sibling distance=1.5cm]

\node[rectangle,draw]{}
  child{node[circle,draw]{}
  child{node{$P_{T|s}$}} edge from parent node[below]{$t\neq s$}
  }
  child{node[circle,draw]{}
  child{node{$z$} edge from parent node[below]{$1-p$}}
  child{node{$x$}edge from parent node[above]{$p$}}
  edge from parent node[above]{$t=s$}
  };
\end{tikzpicture} \\
\end{tabular}

\end{document}

enter image description here

答案2

而不是中心\sim相对于两个决策树的整个高度,符号的垂直位置可能更适合将符号放置得稍微低一些,以便它与带有方框的符号对齐。毕竟,方框构成了树结构的“入口点”,因此将符号放置在入口点旁边是最“合乎逻辑”的。\sim手动调整符号高度的一种方法是“原始”TeX 命令,如下例所示\raise

\sim通过反复试验,可以相对快速地找到符号的正确高度。在手头的例子中, 的值7.9ex似乎是正确的。

enter image description here

\documentclass{article}
\usepackage{tikz}
\begin{document}

\begin{tabular}{@{}ccc@{}}
\begin{tikzpicture}[grow=right,sloped]
\tikzstyle{level 1}=[level distance=2cm, sibling distance=2cm]
\tikzstyle{level 2}=[level distance=2cm, sibling distance=1.5cm]

\node[rectangle,draw]{}
  child{node[circle,draw]{}
  child{node{$P_{T|s}$}} edge from parent node[below]{$t\neq s$}
  }
  child{node[circle,draw]{}
  child{node{$z$} edge from parent node[below]{$1-p$}} 
  child{node{$x$}edge from parent node[above]{$p$}}
  edge from parent node[above]{$t=s$}  
  };
\end{tikzpicture}
&
\raise7.9ex\hbox{$\sim$} % determine visually the amount by which argument is to be raised
&
\begin{tikzpicture}[grow=right,sloped]
\tikzstyle{level 1}=[level distance=2cm, sibling distance=2cm]
\tikzstyle{level 2}=[level distance=2cm, sibling distance=1.5cm]

\node[rectangle,draw]{}
  child{node[circle,draw]{}
  child{node{$P_{T|s}$}} edge from parent node[below]{$t\neq s$}
  }
  child{node[circle,draw]{}
  child{node{$z$} edge from parent node[below]{$1-p$}} 
  child{node{$x$}edge from parent node[above]{$p$}}
  edge from parent node[above]{$t=s$}  
  };
\end{tikzpicture} \\
\end{tabular}
\end{document}

答案3

我的方法与其他答案略有不同。我会设置节点位置,然后将其用作基线。我还会将其放在\sim自己的tikz环境中,并以相同的节点位置为中心:

\documentclass{article}
\usepackage{tikz}

\begin{document}

\begin{tabular}[b]{@{}ccc@{}}
  \begin{tikzpicture}[baseline={(0,0)},grow=right,sloped]
    \tikzstyle{level 1}=[level distance=2cm, sibling distance=2cm]
    \tikzstyle{level 2}=[level distance=2cm, sibling distance=1.5cm]

    \node[rectangle,draw]  at (0,0) {}
      child
        {
          node[circle,draw]{}
          child{node{$P_{T|s}$}} edge from parent node[below]{$t\neq s$}
        }
      child
        {
          node[circle,draw]{}
          child{ node {$z$} edge from parent node[below] {$1-p$} } 
          child{ node {$x$} edge from parent node[above] {$p$}   }
          edge from parent node[above]{$t=s$}  
        };
  \end{tikzpicture}
&
\tikz[baseline={(0,0)}]{\node at (0,0){$\sim$}}
&
  \begin{tikzpicture}[baseline={(0,0)},grow=right,sloped]
    \tikzstyle{level 1}=[level distance=2cm, sibling distance=2cm]
    \tikzstyle{level 2}=[level distance=2cm, sibling distance=1.5cm]

    \node[rectangle,draw] at (0,0) {}
      child{node[circle,draw]{}
      child{node{$P_{T|s}$}} edge from parent node[below]{$t\neq s$}
      }
      child{node[circle,draw]{}
      child{node{$z$} edge from parent node[below]{$1-p$}} 
      child{node{$x$}edge from parent node[above]{$p$}}
      edge from parent node[above]{$t=s$}  
      };
  \end{tikzpicture}  \\
\end{tabular}

\end{document}

enter image description here 这样可以使所有内容在正方形上对齐,但无需猜测将其中一张或另一张图片提升多远\sim

相关内容