如何让表格高度自动最紧凑

如何让表格高度自动最紧凑

我想自动调整 tabularx 行的高度,使其占用最小的空间。

考虑以下代码:

\small
\begin{center}
\begin{tabularx}{\textwidth}{|X|X|X|}
\hline
\textbf{AA} & \textbf{[BB]-[CC]} & \textbf{DD \& general EE}\\
\hline 
aa aa  aa aa  aa aa  aa aa  aa aa  & aa aa  aa aa  aa aa  aa aa  aa aa  & aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  \\
\hline
aa aa  aa aa  aa aa  aa aa  aa aa  & aa aa  aa aa  aa aa  aa aa  aa aa  & aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  \\
\hline
aa aa  aa aa  aa aa  aa aa  aa aa  & aa aa  aa aa  aa aa  aa aa  aa aa  & aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  \\
\hline
aa aa  aa aa  aa aa  aa aa  aa aa  & aa aa  aa aa  aa aa  aa aa  aa aa  & aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  \\
\hline
\end{tabularx}
\end{center}
\normalsize

如果你显示它,你会发现类似这样的内容:

在此处输入图片描述

我希望第三列更宽,这样整行占用的高度更小。但我希望 latex 能自动帮我完成这个任务(我不想考虑第三列应该有多宽,这样整行占用的高度更小)。我该怎么做?

在这个例子中,每一行包含相同的元素,但通常行可能具有不同的内容。

答案1

通过使用tabulary包:

\documentclass{article}
\usepackage{tabulary}
    
\begin{document}
    \begin{center}
    \small
\begin{tabulary}{\textwidth}{|L|L|L|}
    \hline
\textbf{AA} & \textbf{[BB]-[CC]} & \textbf{DD \& general EE}\\
    \hline
aa aa  aa aa  aa aa  aa aa  aa aa  & aa aa  aa aa  aa aa  aa aa  aa aa  & aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  \\
    \hline
aa aa  aa aa  aa aa  aa aa  aa aa  & aa aa  aa aa  aa aa  aa aa  aa aa  & aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  \\
    \hline
aa aa  aa aa  aa aa  aa aa  aa aa  & aa aa  aa aa  aa aa  aa aa  aa aa  & aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  \\
    \hline
aa aa  aa aa  aa aa  aa aa  aa aa  & aa aa  aa aa  aa aa  aa aa  aa aa  & aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  \\
    \hline
\end{tabulary}
    \end{center}
\end{document}

在此处输入图片描述

如果您喜欢将列标题放在一行中,xopu 应该定义列的最小宽度。为此,在您的案例中,请\tymin=5em在前面添加\begin{tabulary}

\documentclass{article}
\usepackage{tabulary}
    
\begin{document}
    \begin{center}
    \small
    \tymin=5em
\begin{tabulary}{\textwidth}{|L|L|L|}
% table body is the same as before
\end{tabulary}
    \end{center}
\end{document}

在此处输入图片描述

答案2

直观地讲,您可以通过设置列宽以使单元格的深度(大致)相等来实现您的目标。如果您希望继续使用环境tabularx,我建议您将第三列的可用宽度设置为前两列可用宽度的十倍 [10!]。

在此处输入图片描述

\documentclass{article} % or some other suitable document class
\usepackage{tabularx,ragged2e}
\newcolumntype{L}[1]{>{\hsize=#1\hsize\RaggedRight\hspace{0pt}}X}
\begin{document}

\begin{center}
\small
\setlength\tabcolsep{3pt} % default: 6pt
\begin{tabularx}{\textwidth}{|L{0.25}|L{0.25}|L{2.50}|} % 0.25+0.25+2.5=3
\hline
\textbf{AA} & \textbf{[BB]-[CC]} & \textbf{DD \& general EE}\\
\hline 
aa aa aa aa aa aa aa aa aa aa & aa aa aa aa aa aa aa aa aa aa & aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa  \\
\hline
aa aa aa aa aa aa aa aa aa aa & aa aa aa aa aa aa aa aa aa aa & aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa  \\
\hline
aa aa aa aa aa aa aa aa aa aa & aa aa aa aa aa aa aa aa aa aa & aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa  \\
\hline
aa aa aa aa aa aa aa aa aa aa & aa aa aa aa aa aa aa aa aa aa & aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa  \\
\hline
\end{tabularx}
\end{center}

\end{document}

答案3

您可以使用\hsize=<factor>\hsize更具可定制性的技巧。

\documentclass{article}
\usepackage{tabularx}

\newcolumntype{Y}[1]{>{\hsize=\fpeval{#1}\hsize}X}

\begin{document}

\begin{center}
\small

\begin{tabularx}{\textwidth}{|Y{1/2}|Y{1/2}|Y{2}|}
\hline
\textbf{AA} & \textbf{[BB]-[CC]} & \textbf{DD \& general EE}\\
\hline 
aa aa  aa aa  aa aa  aa aa  aa aa  &  aa aa  aa aa  aa aa  aa aa  aa aa  &
  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa
  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa
  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa
  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa
  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa
  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa
  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa
  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa
  aa aa  \\
\hline
aa aa  aa aa  aa aa  aa aa  aa aa  &  aa aa  aa aa  aa aa  aa aa  aa aa  &
  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa
  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa
  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa
  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa
  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa
  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa
  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa
  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa  aa aa
  aa aa  \\
\hline
\end{tabularx}

\end{center}

\end{document}

参数的总和Y应等于所需列的数量X,在本例中为 3。

在此处输入图片描述

这些参数可以是分数,这简化了计算,但Y{0.5}对于这种情况您也可以输入。

|Y{1/2}|Y{1}|Y{3/2}|

你得到

在此处输入图片描述

注意\small里面的内容center。很少(如果有的话)需要\normalsize在 LaTeX 文档中发布。

相关内容