如何在表格中找到单元格高度?

如何在表格中找到单元格高度?

这个问题的背景是我想在表格中垂直对齐文本。

即我想选择文本是否是:

  • 尽可能靠近电池顶部,或;
  • 尽可能靠近电池底部,或;
  • 在细胞的中间。

这不是 TeX 所说的“对齐”,但这是我在这里的意思。

一种方法是

\documentclass{article}
\usepackage[a4paper]{geometry}
\usepackage{array}
\usepackage{graphicx}

\begin{document}

\begin{tabular*}{4cm}{ p{4cm} }
  \hline
  \parbox[m][1.8cm][t]{4cm}{Some top-alignedcontent.}  \\
  \hline
  \parbox[m][1.8cm][c]{4cm}{Some middle-aligned content.}  \\
  \hline
  \parbox[m][1.8cm][b]{4cm}{Some bottom-aligned content.}  \\
  \hline
\end{tabular*}
\end{document}

这将 parbox 的中间与单元格基线对齐。然后将 parbox 内的文本与顶部、中间或底部对齐。

但是,这假设我可以将行高设置为 1.8 厘米。在其他情况下,表格中可能存在先前已固定行高的列。那么,有没有办法可以找出表格中的当前行高?

下面是一个示例来说明我的意思:

\documentclass{article}
\usepackage[a4paper]{geometry}
\usepackage{array}
\usepackage{graphicx}

\begin{document}

\begin{tabular*}{4cm}{ p{4cm} p{4cm} }
  \hline
  There could be any old stuff here... &
  \parbox[m][1.8cm][t]{4cm}{Some top-aligned content.}  \\
  \hline
  ... of any width or height... &
  \parbox[m][1.8cm][c]{4cm}{Some middle-aligned content.}  \\
  \hline
  ... is there a variable I can use in the height argument to parbox,
  which will ensure it fits the whole cell height? &
  \parbox[m][1.8cm][b]{4cm}{Some bottom-aligned content.}  \\
  \hline
\end{tabular*}
\end{document}

在 parbox 的高度参数中我应该用什么来代替“1.8cm”?

答案1

我不确定这是否会起作用,但除非你想玩比例胶水,否则我仍然不会打扰。

\strut注意,我喜欢在每个的开头和结尾添加内容\parbox

\documentclass{article}
\usepackage[a4paper]{geometry}
\usepackage{array}
\usepackage{graphicx}

\newlength{\boxsize}

\newcommand{\setboxsize}[2]% #1 = width, #2=text
{\sbox0{\parbox{#1}{\strut #2\strut}}%
 \global\boxsize=\ht0
 \global\advance\boxsize by \dp0
 \box0}

\begin{document}

\begin{tabular}{ll}
  \hline
  \setboxsize{4cm}{There could be any old stuff here...} &
  \parbox[c][\boxsize][t]{4cm}{\strut Some top-aligned content.\strut}  \\
  \hline
  \setboxsize{4cm}{... of any width or height...} &
  \parbox[c][\boxsize][c]{4cm}{\strut Some middle-aligned content.\strut}  \\
  \hline
  \setboxsize{4cm}{... is there a variable I can use in the height argument to parbox,
  which will ensure it fits the whole cell height?} &
  \parbox[c][\boxsize][b]{4cm}{\strut Some bottom-aligned content.\strut}  \\
  \hline
\end{tabular}
\end{document}

此解决方案用新环境 (Tabular) 替换了 tabular。它运行两次 tabular,第一次保存行高(无输出),第二次使用它们。它还引入了宏\cell。其功能类似于\parbox

这个解决方案的一部分是从这里

\documentclass{article}
\usepackage{environ}

\makeatletter
\newcount{\cell@row}
\newlength{\cell@size}

\newcommand{\@cell}[3][c]% (first time) #1=tcb (optional), #2=width, #3=text
{\sbox1{\parbox{#2}{\topstrut #3\bottomstrut}}%
 \dimen0=\ht1
 \advance\dimen0 by \dp1
 \ifdim\cell@size<\dimen0 \global\cell@size=\dimen0\fi
 \expandafter\xdef\csname cell@size\the\cell@row\endcsname{\the\cell@size}%
}
\newcommand{\@@cell}[3][c]% (second time) #1=tcb (optional), #2=width, #3=text
{\parbox[c][\csname cell@size\the\cell@row\endcsname][#1]{#2}{\topstrut #3\bottomstrut}}

\NewEnviron{Tabular}[2][c]% #1 = tcb (optional), #2 = columns (same as tabular)
{\let\cell=\@cell
 \global\cell@row=0
 \global\cell@size=0pt
 \def\topstrut{\rule{0pt}{\arraystretch\ht\strutbox}}%
 \def\bottomstrut{\rule[-\arraystretch\dp\strutbox]{0pt}{0pt}}%
 \let\old@arraycr\@arraycr% executes at end of line
 \def\@arraycr{\global\advance\cell@row by 1
   \global\cell@size=0pt
   \old@arraycr}%
 \sbox0{\begin{tabular}{#2}
 \BODY
 \end{tabular}}%
 \let\cell=\@@cell
 \global\cell@row=0
 \def\@arraycr{\global\advance\cell@row by 1
   \old@arraycr}%
 \begin{tabular}[#1]{#2}
   \BODY
 \end{tabular}}
\makeatother

\begin{document}

\noindent\begin{Tabular}{llll}
  \hline
  \cell[t]{3cm}{\raggedright Some top-aligned content.} &
  \cell[c]{3cm}{\raggedright Some middle-aligned content.} &
  \cell[b]{3cm}{\raggedright Some bottom-aligned content.} &
  \cell{1cm}{\raggedright I saved the largest cell for last} \\
  \hline
\end{Tabular}

\end{document}

演示

答案2

您的问题对于期望的结果不是很清楚,但我思考你要

在此处输入图片描述

\documentclass{article}
\usepackage[a4paper]{geometry}
\usepackage{array}
\usepackage{graphicx}

\begin{document}

\begin{tabular}{p{4cm}p{4cm} }
  \hline
  There could be any old stuff here... &
  Some content.  \\
  \hline
  \multicolumn{1}{m{4cm}}{... of any width or height...}&
  Some content.  \\
  \hline
  \multicolumn{1}{b{4cm}}{... is there a variable I can use in the height argument to parbox,
  which will ensure it fits the whole cell height?} &
  Some content.  \\
  \hline
\end{tabular}


\end{document}

原本猜测:

在此处输入图片描述

\documentclass{article}
\usepackage[a4paper]{geometry}
\usepackage{array}
\usepackage{graphicx}

\begin{document}

\begin{tabular}{m{4cm}m{4cm} }
  \hline
  There could be any old stuff here... &
  Some content.  \\
  \hline
  ... of any width or height... &
  Some content.  \\
  \hline
  ... is there a variable I can use in the height argument to parbox,
  which will ensure it fits the whole cell height? &
  Some content.  \\
  \hline
\end{tabular}
\end{document}

请注意,我tabular没有使用tabular*,因为您的规格是总宽度为 4cm,并且有两列,每列宽度为 4cm+12pt\tabcolsep填充,这显然无法容纳。

相关内容