2

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}
4
  • 1
    why the nested tabular ??? they prevent all linebreaking so stop tabularx or tabulary doing anything useful Nov 13, 2023 at 18:34
  • I was trying to create multirow cells.
    – AEW
    Nov 13, 2023 at 18:43
  • but tabularx and tabulary cells are multiple line parboxes, that is the whole point of the packages: to choose suitable widths for linebreaking within the columns., Nov 13, 2023 at 18:45
  • @DavidCarlisle: I'm not good with tables.
    – AEW
    Nov 13, 2023 at 18:47

2 Answers 2

2

(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}
6
  • Thanks, @Mico, but I need a continuous horizontal line break for the 4 middle columns, in addition to vertical line separation for the columns.
    – AEW
    Nov 13, 2023 at 18:41
  • @AEW - Then why did you use tabular environments in your sample code?
    – Mico
    Nov 13, 2023 at 18:44
  • I was trying to create multirow cells, following an example. I'm not good with tables.
    – AEW
    Nov 13, 2023 at 18:46
  • 1
    @AEW - Please see my updated answer. I think I know what a "continuous horizontal line" (across columns 2 thru 5) means. I'm afraid I don't know what a "continuous horizontal line break" might mean.
    – Mico
    Nov 13, 2023 at 18:59
  • 1
    @AEW - I've updated my answer some more. Speaking for myself, I can see no good reason for using vertical rules in a table.
    – Mico
    Nov 13, 2023 at 19:24
2

If I correctly understood your question, than you after something like this:

enter image description here

For this table I use \f0otnotesize font size, tblr of tabularray package and for first and last cell at bottom use multirow cells:

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

Addendum:
You may consider to use abbreviation for long column titles and explain their meaning in table notes, something like this:

enter image description here

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .