长表单元格中 tkzpicture 的垂直对齐

长表单元格中 tkzpicture 的垂直对齐

我正在努力让 tkzpicture 垂直居中或顶部与文本在 longtable 单元格中对齐。不知何故它与底部基线对齐:

图片显示图片与顶部对齐;而不是居中

我正在尝试让它们全部顶部对齐。

下面是简化的示例。

\documentclass{report}

\usepackage{tikz}
\usepackage{longtable}

\begin{document}

% define a square with text
\def\sqs{2.6cm}
\newcommand{\square}[3]{\node[inner sep=5pt, outer sep=5pt, minimum height=\sqs,minimum width=\sqs,fill=#3,draw,label={[align=center]center:#2}] at (#1){};}

\newcommand{\someset}[4]{
    \begin{tikzpicture}[baseline=(current bounding box.center)]
        \square{0,3}{A}{#1};
        \square{3,3}{B}{#2};
        \square{0,0}{C}{#3};
        \square{3,0}{D}{#4};
    \end{tikzpicture}
}
% Smaller version to use in tables.
\newcommand{\smallset}[4]{\resizebox{2.3cm}{1cm}{\someset{#1}{#2}{#3}{#4}}}

\begin{longtable}[t]{|p{2cm}||p{3cm}|p{2cm}|}
\hline 
foo & \smallset{red}{green}{green}{red} & some long text that will be on many lines and so on some long text that will be on many lines and so on  \\
\hline
foo & \smallset{red}{green}{green}{red} & boo \\
\hline
\end{longtable}
\end{document}

设置baseline=(current bounding box.center)baseline=(current bounding box.north)会产生除数为零的结果。设置为居中似乎不会使其与文本居中。

所需的输出是文本和 tkzpicture 全部顶部对齐。

答案1

\someset这使得和 的基线正确\smallset

\documentclass{report}

\usepackage{tikz}
\usepackage{longtable}

\begin{document}

% define a square with text
\def\sqs{2.6cm}
\newcommand{\square}[3]{\node[inner sep=5pt, outer sep=5pt, minimum height=\sqs,minimum width=\sqs,fill=#3,draw,label={[align=center]center:#2}] at (#1){};}

\newcommand{\someset}[4]{
    \begin{tikzpicture}[baseline=(BASE)]
        \square{0,3}{A}{#1};
        \square{3,3}{B}{#2};
        \square{0,0}{C}{#3};
        \square{3,0}{D}{#4};
      \path (current bounding box.north) ++(0pt,-\ht\strutbox) coordinate (BASE);
    \end{tikzpicture}
}
% Smaller version to use in tables.
\newcommand{\smallset}[4]{\raisebox{\dimexpr \ht\strutbox-\height}% set baseline
  {\resizebox{2.3cm}{2cm}%
    {\raisebox{\depth}% make height total height
      {\someset{#1}{#2}{#3}{#4}}}}}

\begin{longtable}{|p{2cm}||p{3cm}|p{2cm}|}
\hline 
foo & \smallset{red}{green}{green}{red} & some long text that will be on many lines and so on some long text that will be on many lines and so on  \\
\hline
foo & \smallset{red}{green}{green}{red} & boo \\
\hline
\end{longtable}
\end{document}

相关内容