根据单元格内容自动调整表格列的宽度

根据单元格内容自动调整表格列的宽度

我想创建一个表格并自动调整不同列的宽度以适应其内容。在传统tabular环境中,表格超出了页面边框。tabulary生成太窄的列和重叠的文本。我没有成功实现tabularx。请注意,我需要一个多行单元格作为第一列和最后一列,以及一条连续的水平线作为其他列。

任何解决方案都将不胜感激。

在此处输入图片描述

\documentclass{article}
\usepackage{tabularx}
\usepackage{tabulary}

\begin{document}
    \begin{table}[h]
        \centering
        \renewcommand{\arraystretch}{1.25}
        \caption{TABLE TITLE}
        %\begin{tabular}{|c|c|c|c|c|c|}
        \begin{tabulary}{\textwidth}{|C|C|C|C|C|C|}         
            \hline
            \textbf{Col. 1} & \textbf{Col. 2: Very very very long title} & \textbf{Col. 3: Very very long title} & \textbf{Col. 4: Very long title} & \textbf{Col. 5: Very very long title} & \textbf{Col. 6: Very very very long title} \\   \hline  \hline
            
            \textbf{Line 1} &
            \begin{tabular}{@{}c@{}} Sensor1 \\ \hline Sensor2 \end{tabular} &
            \begin{tabular}{@{}c@{}} $(10.0, 12.3)$ \\ \hline $(4.2, 9.3)$ \end{tabular} &
            \begin{tabular}{@{}c@{}} 12.4 \\ \hline 9.9 \end{tabular} &
            \begin{tabular}{@{}c@{}} 2.0 \\ \hline 3.3 \end{tabular}  &
            0.4 \\   \hline
        \end{tabulary}
    \end{table}
\end{document}

答案1

(在 OP 声明他们需要一条连续的线来跨越第一个数据行中的第 2 列到第 5 列后,我更新了我的答案。在了解到 OP 也需要垂直规则后,我进行了进一步的更新。)

使用设置时没有问题tabularx。请注意,第 2 列至第 6 列的宽度均相同。

我还建议废除那些看起来粗俗的大胆的在标题行和左侧列中。

在此处输入图片描述

\documentclass{article}
\usepackage{multirow}
\usepackage{tabularx} % for 'tabularx' env. and 'X' col. type
\usepackage{ragged2e} % for '\Centering' macro
\newcolumntype{C}{>{\Centering}X}
\usepackage{booktabs} % for well-spaced horizontal rules

\begin{document}

% First table: no vertical rules, well-spaced horizontal rules
\begin{table}[h]
\caption{Table Title \strut}
\begin{tabularx}{\textwidth}{@{} l CCCCC @{}}         
\toprule
Col.\ 1 & 
Col.\ 2\newline Very very very long title & 
Col.\ 3\newline Very very long title & 
Col.\ 4\newline Very long title & 
Col.\ 5\newline Very very long title & 
Col.\ 6\newline Very very very long title \\  
\midrule
   \multirow{2.3}{*}{Line 1} 
   & Sensor 1 & (10.0, 12.3) & 12.4 & 2.0 & 
   \multirow{2.3}{*}{0.4} \\
   \cmidrule(lr){2-5}
   & Sensor 2 & (4.2, 9.3) & 9.9 & 3.3 \\
\bottomrule
\end{tabularx}
\end{table}

%% Second table: vertical rules
\begin{table}[h]
\setlength\tabcolsep{5pt}      % default: 6pt
\setlength\extrarowheight{2pt} % for a less-cramped "look"
\caption{Same table as before, but now with vertical rules \strut}
\begin{tabularx}{\textwidth}{|l|C|C|C|C|C|}         
\hline
Col.\ 1 & 
Col.\ 2\newline Very very very long title & 
Col.\ 3\newline Very very long title & 
Col.\ 4\newline Very long title & 
Col.\ 5\newline Very very long title & 
Col.\ 6\newline Very very very long title \\  
\hline
   \multirow{2.1}{*}{Line 1} 
   & Sensor 1 & (10.0, 12.3) & 12.4 & 2.0 & 
   \multirow{2.1}{*}{0.4} \\
   \cline{2-5}
   & Sensor 2 & (4.2, 9.3) & 9.9 & 3.3 & \\
\hline
\end{tabularx}
\end{table}

\end{document}

答案2

如果我正确理解了你的问题,那么你会得到这样的结果:

在此处输入图片描述

对于此表,我使用包的\f0otnotesize字体大小以及底部第一个和最后一个单元格使用多行单元格:tblrtabularray

\documentclass{article}
%   \usepackage{geometry}   % for setting page layout
\usepackage[skip=1ex]{caption}  % added
\usepackage{tabularray}

\begin{document}
    \begin{table}[ht]
\caption{TABLE TITLE}
    \footnotesize
    \begin{tblr}{hlines, vlines,
                 colsep ={3pt},
                 colspec={Q[l, font=\bfseries] *{5}{X[c]}},
                 cell{Y}{1,Z} = {r=2}{},    % defined multi row cells
                 row{1} ={font=\bfseries}
                 }
Col. 1 
    & Col. 2: Very very very long title 
        & Col. 3: Very very long title
            & Col. 4: Very long title
                & Col. 5: Very very long title 
                    & Col. 6: Very very very long title \\
Line 1 
    &   Sensor1
        & (10.0, 12.3)
            &   12.4 
                &   2.0 
                    &   0.4 \\   
    &   Sensor2
        &   (4.2, 9.3)
            &   9.9
                &   3.3
                    &   0.4 \\
        \end{tblr}
    \end{table}
\end{document}

附录:
您可以考虑使用长列标题的缩写,并在表格注释中解释其含义,如下所示:

在此处输入图片描述

\documentclass{article}
%   \usepackage{geometry}   % for setting page layout
\usepackage[inline]{enumitem}
\usepackage{tabularray}

\begin{document}
    \begin{table}[ht]
    \begin{talltblr}[
caption={TABLE TITLE},
 note{} = {\footnotesize
            \begin{enumerate*}[label=\textsuperscript{\alph*}, itemjoin={{; }}]
           \item    Very very very long title
           \item    Very very long title
           \item    Very long title
           \item    Very very long title
           \item    Very very very long title
           \end{enumerate*}}
                ]{hlines, vlines,
                 colsep=3pt,
                 colspec={Q[l, font=\bfseries] *{5}{X[c]}},
                 cell{Y}{1,Z} = {r=2}{},    % defined multi row cells
                 row{1} ={font=\bfseries}
                 }
Col. 1 
    & Col. 2\TblrNote{a} 
        & Col. 3\TblrNote{b}
            & Col. 4\TblrNote{c}
                & Col. 5\TblrNote{d}
                    & Col. 6\TblrNote{e} \\
Line 1 
    &   Sensor1
        & (10.0, 12.3)
            &   12.4 
                &   2.0 
                    &   0.4 \\   
    &   Sensor2
        &   (4.2, 9.3)
            &   9.9
                &   3.3
                    &   0.4 \\
        \end{talltblr}
    \end{table}
\end{document}

相关内容