如何在(不同的)表格单元格中垂直和水平居中文本和图像?

如何在(不同的)表格单元格中垂直和水平居中文本和图像?

我有一张表格,其中一列包含图像。我希望第一列的文本垂直和水平居中。我希望第二列的文本左对齐并垂直居中。我希望第三列包含一张垂直和水平居中的小图像。最后两列都应左对齐并在单元格顶部对齐。

我最接近此情况的是下面的代码:

\documentclass[10pt]{article}

\usepackage{tikz}
\usepackage{array}
\usepackage{booktabs}

\newcommand*{\tabbox}[4][t]{%
    \vspace{#3}\parbox[#1][3.7\baselineskip]{#2}{\strut#4\strut}}


\begin{document}

\begin{tabular}{c p{2cm} c p{3cm} p{3cm}} 
        \toprule 
            \multicolumn{1}{c}{Header 1} & 
            \multicolumn{1}{c}{Header 2} & 
            \multicolumn{1}{c}{Header 3} & 
            \multicolumn{1}{c}{Header 4} & 
            \multicolumn{1}{c}{Header 5} \\ 
    \midrule
            Short Text &    
            Slightly Longer Text & 
            \tabbox[t]{3cm}{0pt}{
                \begin{tikzpicture}
                  % Draw beam and end lines
                  \draw (-1.5,-0.75) rectangle (1.5,0.75) ;
              \end{tikzpicture}
          } 
          & 
          Some text that extends onto more than one line as there's plenty of it. & 
          Some text that extends onto more than one line as there's plenty of it. Some text that extends onto more than one line as there's plenty of it. \\
            Short Text  & 
            Slightly Longer Text & 
            \tabbox[t]{3cm}{0pt}{
                \begin{tikzpicture}
                  % Draw beam and end lines
                  \draw (-1.5,-0.75) rectangle (1.5,0.75) ;
              \end{tikzpicture}
          }  & 
          Short Text & \\
            Short Text & Slightly Longer Text & Short Text & Short Text & \\
            Short Text & Slightly Longer Text & Short Text & Short Text & \\
    \bottomrule 
\end{tabular}

\end{document}

这部分是基于建议这里。“m”列说明符似乎对第一列没有任何影响。此外,图片不会自动垂直容纳在表格单元格中。

我怎样才能达到我想要的效果?

编辑

这是另一个尝试,几乎根据这个问题

\documentclass[10pt]{article}

\usepackage{tikz}
\usetikzlibrary{calc}
\usepackage{array}
\usepackage{booktabs}

\begin{document}

\begin{tabular}{c p{2cm} p{3cm} p{3cm} p{3cm}} 
        \toprule 
            \multicolumn{1}{c}{Header 1} & 
            \multicolumn{1}{c}{Header 2} & 
            \multicolumn{1}{c}{Header 3} & 
            \multicolumn{1}{c}{Header 4} & 
            \multicolumn{1}{c}{Header 5} \\ 
    \midrule
            Short Text &    
            Slightly Longer Text & 
                \begin{tikzpicture}[baseline={($(current bounding box.center)+(0,-0.5ex)$)}]
                  % Draw beam and end lines
                  \draw (-1.5,-0.75) rectangle (1.5,0.75) ;
                  \useasboundingbox (current bounding box.north west) ++(0,1ex) (current bounding box.south east) ++(0,-1ex);
              \end{tikzpicture}
          & 
          Some text that extends onto more than one line as there's plenty of it. & 
          Some text that extends onto more than one line as there's plenty of it. Some text that extends onto more than one line as there's plenty of it. \\
            Short Text  & 
            Slightly Longer Text & 
                \begin{tikzpicture}[baseline={($(current bounding box.center)+(0,-0.5ex)$)}]
                  % Draw beam and end lines
                  \draw (-1.5,-0.75) rectangle (1.5,0.75) ;
                  \useasboundingbox (current bounding box.north west) ++(0,1ex) (current bounding box.south east) ++(0,-1ex);
              \end{tikzpicture} 
          & 
          Short Text & \\
            Short Text & Slightly Longer Text & Short Text & Short Text & \\
            Short Text & Slightly Longer Text & Short Text & Short Text & \\
    \bottomrule 
\end{tabular}

\end{document}

答案1

问题在于准确理解其tabular工作原理。

每个单元格都有一个参考点,并且所有参考点最终都放在同一条水平线上。对于lcr单元格,参考点位于基线上;对于p单元格,参考点位于第一行的基线上;对于m单元格,参考点位于文本顶部和底部的中间位置;对于b单元格,参考点位于最后一行的基线上。

因此,tabular诸如

{l m{3cm} p{3cm}}

将要不是将行中间的单元格与最后一个单元格对齐,但其内容将停留在最后一列的垂直尺寸的一半以上(略小于)。

如果不测量细胞的内容,就无法获得你想要获得的那种对齐方式,正如 Stefan Kottwitz 的回答所示\tabbox:这3.7\baselineskip例如,一般来说不是。

\adjustbox针对您的具体情况,这里有一个解决方法(请注意图片的使用):

\documentclass{article}

\usepackage{tikz}
\usepackage{array}
\usepackage{booktabs,adjustbox}

\newcommand{\finalcells}[2]{%
  \begingroup\sbox0{\begin{minipage}{3cm}\raggedright#1\end{minipage}}%
  \sbox2{\begin{minipage}{3cm}\raggedright#2\end{minipage}}%
  \xdef\finalheight{\the\dimexpr\ht0+\dp0+\smallskipamount\relax}%
  \xdef\finalheightB{\the\dimexpr\ht2+\dp2+\smallskipamount\relax}%
  \ifdim\finalheightB>\finalheight
    \global\let\finalheight\finalheightB
  \fi\endgroup
  \begin{minipage}[t][\finalheight][t]{3cm}\raggedright#1\end{minipage}&
  \begin{minipage}[t][\finalheight][t]{3cm}\raggedright#2\end{minipage}}

\begin{document}

\begin{tabular}{c m{2cm} m{3cm}  m{3cm} m{3cm}} 
\toprule 
\multicolumn{1}{c}{Header 1} & 
\multicolumn{1}{c}{Header 2} & 
\multicolumn{1}{c}{Header 3} & 
\multicolumn{1}{c}{Header 4} & 
\multicolumn{1}{c}{Header 5} \\ 
\midrule
Short Text &    
Slightly Longer Text & 
\adjustbox{valign=c}{%
  \begin{tikzpicture}
  % Draw beam and end lines
  \draw (-1.5,-0.75) rectangle (1.5,0.75) ;
  \end{tikzpicture}%
}
& 
\finalcells{Some text that extends onto more than one line as there's plenty of it.}
  {Some text that extends onto more than one line as there's plenty of it. 
   Some text that extends onto more than one line as there's plenty of it.} \\
\bottomrule 
\end{tabular}

\end{document}

在此处输入图片描述

相关内容