Automatically adjust the width of a table column to the cell content

tablestabularxtabulary

I would like to create a table and automatically adjust the width of the different columns to fit their content. With the traditional tabular environment, the table exceeds the page borders. tabulary generates too narrow columns and overlapped texts. I was not successful in implementing tabularx. Note that I need a multirow cell for the first and last columns, and a continuous horizontal line for the other columns.

Any solution would be greatly appreciated.

enter image description here

\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}

Best Answer

(I updated my answer after the OP stated that they need a continuous line to span columns 2 thru 5 in the first data row. Further update after learning the OP wants vertical rules as well.)

No problems when using a tabularx setup. Note that columns 2 thru 6 all have the same width.

I'd also recommend doing away with the borderline vulgar-looking bold-facing in the header row and the left-hand column.

enter image description here

\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}
Related Question