包含森林的表格中單元格的垂直和水平對齊

包含森林的表格中單元格的垂直和水平對齊

這是一個 MWE,其中我創建了一個表,其中一行是純文本,另一行是 tikz 節點,另一行是森林單元格。我嘗試使用表環境的 [t] 選項將單元格頂部對齊,並使用列說明符 c 將單元格垂直居中。

\documentclass{standalone}
\usepackage{forest}
\begin{document}
\forestset{c/.style={circle,draw},t/.style={},}
\def\bb{ \draw[fill=blue,opacity=0.2] (current bounding box.south west) rectangle (current bounding box.north east);}
\def\xx#1#2{
  #1
  \begin{tikzpicture}
    \node [fill=green,opacity=0.2,draw,minimum width=#1cm, minimum height=#2cm] {#1#2};
  \end{tikzpicture}
  #2
}
\begin{tabular}[t]{|c|c|c|c|c|c|}
  $(ab.cd)$                                                           &
  $(abcd.\bot)$                                                       &
  $(ab.(c.d))$                                                        &
  $((a.b).(c.d))$                                                     &
  $(a.(b.(c.d)))$                                                     &
  $(((a.b).c).d)$                                                       \\
  \hline
  \xx23                                                               &
  \xx34                                                               &
  \xx24                                                               &
  \xx14                                                               &
  \xx41                                                               &
  \xx43                                                                 \\
  \hline
  \scriptsize
  \Forest{for tree [{},c[$ab$,t][$cd$,t]]\bb}                         &
  \scriptsize
  \Forest{for tree [{},c[$abcd$,t][$\bot$,t]]\bb}                     &
  \scriptsize
  \Forest{for tree [{},c[$ab$,t][{},c[$c$,t][$d$,t]]]\bb}             &
  \scriptsize
  \Forest{for tree [{},c[{},c[$a$,t][$b$,t]][{},c[$c$,t][$d$,t]]]\bb} &
  \scriptsize
  \Forest{for tree [{},c [{},c [{},c[a,t][b,t]] [c,t]] [d,t] ] \bb}   &
  \scriptsize
  \Forest{for tree [{},c [{}, c [{}, c
        [a,t][b,t]] [c,t] ] [d,t]] \bb}
\end{tabular}
\end{document}

然而輸出表明這不起作用

在此輸入影像描述

答案1

有兩個問題。

  1. 首先也是最重要的,pgf 中存在一個插入虛假空格的錯誤。已提供該錯誤的臨時修復這裡。在 pgf 的未來版本中,該錯誤將被修復。
  2. 其次,[t]in\begin{tabular}[t]{...}不會將表格中的儲存格頂部對齊。我新增了一個使用和T來執行此操作的列類型。collcelladjustbox

代碼:

\documentclass{standalone}
\usepackage{forest}
\usepackage{adjustbox}
\usepackage{collcell}
\makeatletter
% remove the stray space https://tex.stackexchange.com/a/513549
\patchcmd{\pgfutilsolvetwotwoleqfloat}
  { \noexpand\pgfmathfloatdivide@}
  {\noexpand\pgfmathfloatdivide@}
  {}{}
\makeatother
\newcommand{\TopAlign}[1]{\adjustbox{valign=t}{#1}}
\newcolumntype{T}{>{\collectcell{\TopAlign}}c<{\endcollectcell}}

\begin{document}
\forestset{c/.style={circle,draw},t/.style={},}
\def\bb{ \draw[fill=blue,opacity=0.2] (current bounding box.south west) rectangle (current bounding box.north east);}
\def\xx#1#2{
  #1
  \begin{tikzpicture}
    \node [fill=green,opacity=0.2,draw,minimum width=#1cm, minimum height=#2cm] {#1#2};
  \end{tikzpicture}
  #2
}
\begin{tabular}{*{6}{|T}|}
  $(ab.cd)$                                                           &
  $(abcd.\bot)$                                                       &
  $(ab.(c.d))$                                                        &
  $((a.b).(c.d))$                                                     &
  $(a.(b.(c.d)))$                                                     &
  $(((a.b).c).d)$                                                       \\
  \hline
  \xx23                                         &
  \xx34                                                              &
  \xx24                                                             &
  \xx14                                                              &
  \xx41                                                              &
  \xx43                                                                \\
  \hline
  \scriptsize
  \Forest{for tree [{},c[$ab$,t][$cd$,t]]\bb}                         &
  \scriptsize
  \Forest{for tree [{},c[$abcd$,t][$\bot$,t]]\bb}                     &
  \scriptsize
  \Forest{for tree [{},c[$ab$,t][{},c[$c$,t][$d$,t]]]\bb}             &
  \scriptsize
  \Forest{for tree [{},c[{},c[$a$,t][$b$,t]][{},c[$c$,t][$d$,t]]]\bb} &
  \scriptsize
  \Forest{for tree [{},c [{},c [{},c[a,t][b,t]] [c,t]] [d,t] ] \bb}   &
  \scriptsize
  \Forest{for tree [{},c [{}, c [{}, c
        [a,t][b,t]] [c,t] ] [d,t]] \bb}
\end{tabular}
\end{document}

在此輸入影像描述

相關內容