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