如何调整表格中的 tikzpicture?

如何调整表格中的 tikzpicture?

我在表格中插入 tikzpicture,但图片总是不在单元格的中心,尤其是在多行时。这是我的代码:

\begin{table}[H]
\begin{center}
\begin{tabular}{cccccc} 
\hline 
Graph & Situation & EV.diff. & Real.diff. & Best response& \# of obs. \\
\hline 
 \multirow{2}[2]{*}[2mm]{
\begin{tikzpicture}
\tikzstyle{player}= [circle, draw=blue!50, fill=blue!20, inner sep= 0pt,  minimum size=6mm]
\tikzstyle{line}=[-,draw=blue,shorten >=1pt,>=stealth',semithick]
\node[player] (1) at (0,0) {$1$};
\node[player] (2)   at +(0: 1)  {$2$};
\node[player] (3)   at +(0: 2)  {$3$};
\draw (1) to (2)  [line];
\draw (2) to (3)  [line];
\end{tikzpicture}
}
&   1 (3) $\rightarrow$  2  & 4 & 3.7  & 0.854  & 151 \bigstrut\\
&   2 $\rightarrow$ 1 (3)  & -12   & -12    & 0.948 & 77 \bigstrut\\ 

\begin{tikzpicture}
\tikzstyle{player}= [circle, draw=blue!50, fill=blue!20, inner sep=0pt, minimum size=6mm]
\tikzstyle{line}=[-,draw=blue,shorten >=1pt,>=stealth',semithick]
\node[player] (1) at (0,0) {$1$};
\node[player] (2)   at (0.7, 1.08)  {$2$};
\node[player] (3)   at (1.4, 0)  {$3$};
\draw (1) to (2)  [line];
\draw (2) to (3)  [line];
\draw (1) to (3)  [line];
\end{tikzpicture} 

\cline{2-6}
& 2  (3)    $\rightarrow$  1  &  4     & 2.67    & 0.24 & 25  \bigstrut\\  

\multirow{5}[5]{*}{
\begin{tikzpicture}
\tikzstyle{player}= [circle, draw=blue!50, fill=blue!20, inner sep=0pt, minimum size=6mm]
\tikzstyle{line}=[-,draw=blue,shorten >=1pt,>=stealth',semithick]
\node[player] (1) at (0,0) {$1$};
\node[player] (2)   at (-0.7, -1.08)  {$2$};
\node[player] (3)   at (0.7, -1.08)  {$3$};
\node[player] (4)   at (1.4, 0)  {$4$};
\draw (1) to (2)  [line];
\draw (1) to (3)  [line];
\draw (1) to (4)  [line];
\draw (3) to (4)  [line];
\end{tikzpicture}
}
\cline{2-6}
& 2 $\rightarrow$ 1  &  -1.33    & 0.8  & 0.39 & 41 \bigstrut\\
& 3  (4)  $\rightarrow$ 1  & 4      & 4.17 & 0.889  & 27 \bigstrut\\
& 3  (4)  $\rightarrow$ 4  (3)  & -4.7   & -6.33  & 0.676  & 37 \bigstrut\\
& 1 $\rightarrow$  3   (4)  & 4    &  4.25 & 0.5 & 32 \bigstrut\\
& 1 $\rightarrow$ 2 & -12   & -12   & 1     & 13 \bigstrut\\          

\hline
\end{tabular}
\end{center}
\end{table}

该表如下所示: 在此处输入图片描述

我想将图片和文字放在表格每个单元格的中心。

答案1

显示您无法通过显示代码片段获得的图片...它有太多错误。

编辑: 在 Bernard 的帮助下,我成功地解决了给定 MWE 中的补救措施,现在我能够提供完整的解决方案。在其中,为了使图像居中,我使用 TikZ 图片选项[baseline=(current bounding box.center)]。在普通单元格中工作(在我看来)非常好,但在现在使用from 包\multirow{...}定义的单元格中,需要考虑增加的单元格高度(最低图片累积在一行的高度)。\multirowcell{...}makecell

除此之外,我还建议使用S列类型来对齐表格最后四列的数字。还进行了一些优化:\tizstyle{...}我定义了所有三张图片中使用的通用类型(已过时)\tikzset{...}。得到的图片是:

在此处输入图片描述

上图的代码为:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows}

\usepackage{booktabs, makecell, multirow}
\usepackage{siunitx}

\begin{document}
    \begin{table}[htb]
\tikzset{%
player/.style = {circle, draw=blue!50, fill=blue!20, 
                 inner sep= 0pt,  minimum size=6mm},
  line/.style = {draw=blue, -stealth', semithick, shorten >=1pt}    
                            }% end of tikzset
    \centering
    \setcellgapes{3pt}
    \makegapedcells
\begin{tabular}{cc *{4}{S[table-format=-2.2]}}
    \toprule
Graph & Situation & {EV.diff.} & {Real.diff.} & {Best response} & {\# of obs.} \\
  \midrule
 \multirowcell{2}{
\begin{tikzpicture}[baseline=(current bounding box.center)]
\node[player] (1) at (0,0) {$1$};
\node[player] (2) at (0:1)  {$2$};
\node[player] (3) at (0:2)  {$3$};
\draw[line] (1) edge (2)
            (2)  to  (3);
\end{tikzpicture}
                       }
    &   1 (3) $\rightarrow$ 2   & 4     & 3.7  & 0.854  & 151       \\
    &   2 $\rightarrow$ 1 (3)   & -12   & -12  & 0.948  & 77        \\
    \cmidrule{2-6}
\begin{tikzpicture}[baseline=(current bounding box.center)]
\node[player] (1) at (0,0)      {$1$};
\node[player] (2) at (0.7,1.08) {$2$};
\node[player] (3) at (1.4,0)    {$3$};
\draw[line] (1) edge (2)  
            (2) edge (3) 
            (1)  to  (3);
\end{tikzpicture}
    & 2 (3) $\rightarrow$ 1     &  4    & 2.67 & 0.24   & 25        \\
  \cmidrule{2-6}
 \multirowcell{6}{
\begin{tikzpicture}[baseline=(current bounding box.center)]
\node[player] (1) at (0,0)        {$1$};
\node[player] (2) at (-0.7,-1.08) {$2$};
\node[player] (3) at (0.7, -1.08) {$3$};
\node[player] (4) at (1.4, 0)     {$4$};
\draw[line] (1) edge (2) 
            (1) edge (3)
            (1) edge (4)
            (3)  to  (4);
  \end{tikzpicture}}
    & 2 $\rightarrow$ 1         & -1.33 & 0.8   & 0.39  & 41        \\
    & 3 (4) $\rightarrow$ 1     & 4     & 4.17  & 0.889 & 27        \\
    & 3 (4) $\rightarrow$ 4 (3) & -4.7  & -6.33 & 0.676 & 37        \\
    & 1 $\rightarrow$ 3 (4)     & 4     & 4.25  & 0.5   & 32        \\
    & 1 $\rightarrow$ 2         & -12   & -12   & 1     & 13        \\
  \bottomrule
\end{tabular}
    \end{table}
\end{document} 

答案2

解决方案是makecellbooktabs(在规则周围有一些垂直填充)和\raisebox

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows}
\usepackage{multirow, makecell, bigstrut, booktabs, float}

 \begin{document}

 \begin{table}[H]
\centering
\begin{tabular}{*{6}{c}}
  \toprule
  Graph & Situation & EV.diff. & Real.diff. & Best response & \# of obs. \\
  \midrule
  \multirowcell{2}[-2\bigstrutjot]{\begin{tikzpicture}
  \tikzstyle{player}= [circle, draw=blue!50, fill=blue!20, inner sep= 0pt, minimum size=6mm]
  \tikzstyle{line}=[-,draw=blue,shorten >=1pt,>=stealth',semithick]
  \node[player] (1) at (0,0) {$1$};
  \node[player] (2) at +(0: 1) {$2$};
  \node[player] (3) at +(0: 2) {$3$};
  \draw (1) to (2) [line];
  \draw (2) to (3) [line];
  \end{tikzpicture}
  }
        & 1 (3) $\rightarrow$ 2 & 4 & 3.7 & 0.854 & 151 \bigstrut \\
        & 2 $\rightarrow$ 1 (3) & -12 & -12 & 0.948 & 77 \bigstrut \\
  \cmidrule{2-6}
  \raisebox{-0.45\height}{\begin{tikzpicture}
  \tikzstyle{player}= [circle, draw=blue!50, fill=blue!20, inner sep=0pt, minimum size=6mm]
  \tikzstyle{line}=[-,draw=blue,shorten >=1pt,>=stealth',semithick]
  \node[player] (1) at (0,0) {$1$};
  \node[player] (2) at (0.7, 1.08) {$2$};
  \node[player] (3) at (1.4, 0) {$3$};
  \draw (1) to (2) [line];
  \draw (2) to (3) [line];
  \draw (1) to (3) [line];
  \end{tikzpicture}}
        & 2 (3) $\rightarrow$ 1 & 4 & 2.67 & 0.24 & 25 \bigstrut \\
  \cmidrule{2-6}
  \multirowcell{5}[-5\bigstrutjot]{%
  {\begin{tikzpicture}
  \tikzstyle{player}= [circle, draw=blue!50, fill=blue!20, inner sep=0pt, minimum size=6mm]
  \tikzstyle{line}=[-,draw=blue,shorten >=1pt,>=stealth',semithick]
  \node[player] (1) at (0,0) {$1$};
  \node[player] (2) at (-0.7, -1.08) {$2$};
  \node[player] (3) at (0.7, -1.08) {$3$};
  \node[player] (4) at (1.4, 0) {$4$};
  \draw (1) to (2) [line];
  \draw (1) to (3) [line];
  \draw (1) to (4) [line];
  \draw (3) to (4) [line];
  \end{tikzpicture}}
  }
        & 2 $\rightarrow$ 1 & -1.33 & 0.8 & 0.39 & 41 \bigstrut \\
        & 3 (4) $\rightarrow$ 1 & 4 & 4.17 & 0.889 & 27 \bigstrut \\
        & 3 (4) $\rightarrow$ 4 (3) & -4.7 & -6.33 & 0.676 & 37 \bigstrut \\
        & 1 $\rightarrow$ 3 (4) & 4 & 4.25 & 0.5 & 32 \bigstrut \\
        & 1 $\rightarrow$ 2 & -12 & -12 & 1 & 13 \bigstrut \\
  \bottomrule
\end{tabular}
 \end{table}

\end{document} 

在此处输入图片描述

相关内容