1. Automatic Determination of Column Widths:
Here is a version that does not require multiple passes nor guess work as to the widths, but does require you to know what the widest elements of each of the sub-columns is. Using the the widest elements of the sub-columns you can compute the correct width to use for the p{}
column type. This yields:
Alternatively I would recommend using the booktabs
package:
Notes:
- With the automatic width determination, your headers were able to fit into two rows.
- A trailing
&
was missing one one line, have added that.
- One of the rows seemed to have data that was overlapping the
\multirow
so tweak that as well.
- Even with
\footnotesize
your table is still too wide for normal usage, so unless you are making it landscape of for a wider \paperwidth
you should revisit how you are doing the table.
Code:
\documentclass[a4paper,10pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{graphicx}
\usepackage{multirow}
\usepackage{array}
\usepackage{booktabs}
\newlength{\ColumnWidthBCD}
\newlength{\ColumnWidthEFG}
\newlength{\ColumnWidthHIJ}
% These should contain the widet elements of each column
\newcommand{\ColumnATitle}{Cooooooool1}
\newcommand{\ColumnBTitle}{Cooooool2}
\newcommand{\ColumnCTitle}{``Col}
\newcommand{\ColumnDTitle}{Cooooool1}
\newcommand{\ColumnETitle}{Cooooool2}
\newcommand{\ColumnFTitle}{``Coooooool}
\newcommand{\ColumnGTitle}{Coooooool1}
\newcommand{\ColumnHTitle}{Coooooool2}
\newcommand{\ColumnITitle}{``Coooooool}
\newcommand{\ColumnJTitle}{3''}
\begin{document}
\hspace{-3cm}
\footnotesize{
\settowidth{\ColumnWidthBCD}{\ColumnBTitle\ColumnCTitle\ColumnDTitle}%
\settowidth{\ColumnWidthEFG}{\ColumnETitle\ColumnFTitle\ColumnGTitle}%
\settowidth{\ColumnWidthHIJ}{\ColumnHTitle\ColumnITitle\ColumnJTitle}%
% Now adjust for the intercolumn spacing
\addtolength{\ColumnWidthBCD}{6\tabcolsep}
\addtolength{\ColumnWidthEFG}{6\tabcolsep}
\addtolength{\ColumnWidthHIJ}{6\tabcolsep}
\noindent
\begin{tabular}{|c|c|c|c|c|c|c|c|c|c|}
\hline
& \multicolumn{3}{p{\ColumnWidthBCD} |}{\centering Very long column title that needs to be centered} &
\multicolumn{3}{p{\ColumnWidthEFG} |}{\centering Very long column title that needs to be centered 2} &
\multicolumn{3}{p{\ColumnWidthHIJ}|}{\centering Very long column title that needs to be centered 3} \\
\hline
\multirow{2}{*}{\ColumnATitle} & \multirow{2}{*}{\ColumnBTitle} & \ColumnCTitle & \multirow{2}{*}{\ColumnDTitle} &
\multirow{2}{*}{\ColumnETitle} & \ColumnFTitle & \multirow{2}{*}{\ColumnGTitle} & \multirow{2}{*}{\ColumnHTitle} & \ColumnITitle & \\% <--- trailing "&" was missing.
% & & & 3'' & & & 3'' & & & 3'' \\
& & & & & & & & & 3'' \\% <--- changed this line as well, seemed wrong.
\hline
1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 & 10 \\
\hline
\end{tabular}
}
\bigskip
\hspace{-3cm}
\footnotesize{
% This would not need to be recomputed here as we could use the above
% determined values. But to keep this second example useable standalong
% without the above example decided to leave these here as well
\settowidth{\ColumnWidthBCD}{\ColumnBTitle\ColumnCTitle\ColumnDTitle}%
\settowidth{\ColumnWidthEFG}{\ColumnETitle\ColumnFTitle\ColumnGTitle}%
\settowidth{\ColumnWidthHIJ}{\ColumnHTitle\ColumnITitle\ColumnJTitle}%
% Now adjust for the intercolumn spacing
\addtolength{\ColumnWidthBCD}{6\tabcolsep}
\addtolength{\ColumnWidthEFG}{6\tabcolsep}
\addtolength{\ColumnWidthHIJ}{6\tabcolsep}
\noindent
\begin{tabular}{cccccccccc} \toprule
& \multicolumn{3}{p{\ColumnWidthBCD}}{\centering Very long column title that needs to be centered} &
\multicolumn{3}{p{\ColumnWidthEFG}}{\centering Very long column title that needs to be centered 2} &
\multicolumn{3}{p{\ColumnWidthHIJ}}{\centering Very long column title that needs to be centered 3} \\
\cmidrule(l{2pt}r{4pt}){1-1}
\cmidrule(l{4pt}r{4pt}){2-4}
\cmidrule(l{4pt}r{4pt}){5-7}
\cmidrule(l{4pt}r{2pt}){8-10}
\multirow{2}{*}{\ColumnATitle} & \multirow{2}{*}{\ColumnBTitle} & \ColumnCTitle & \multirow{2}{*}{\ColumnDTitle} &
\multirow{2}{*}{\ColumnETitle} & \ColumnFTitle & \multirow{2}{*}{\ColumnGTitle} & \multirow{2}{*}{\ColumnHTitle} & \ColumnITitle & \\% <--- trailing "&" was missing.
% & & & 3'' & & & 3'' & & & 3'' \\
& & & & & & & & & 3'' \\% <--- changed this line as well, seemed wrong.
1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 & 10 \\
\bottomrule
\end{tabular}
}
\end{document}
2. Manual Setting of Column Widths
The above version may seem complicated to use due to the automatic determination of the column widths, which are useful if one was typesetting this kind of table often. For occasional use, you can manually tweak the column widths until the desired results were obtained.
The MWE below produces identical results, but does require you to take an initial guess at the widths of the columns and then tweak as desired:
Code:
\documentclass[a4paper,10pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{graphicx}
\usepackage{multirow}
\usepackage{array}
\usepackage{booktabs}
\begin{document}
\hspace{-3cm}
\footnotesize{
\noindent
\begin{tabular}{|c|c|c|c|c|c|c|c|c|c|}
\hline
& \multicolumn{3}{p{4.63cm}|}{\centering Very long column title that needs to be centered} &
\multicolumn{3}{p{5.73cm}|}{\centering Very long column title that needs to be centered 2} &
\multicolumn{3}{p{4.65cm}|}{\centering Very long column title that needs to be centered 3} \\
\hline
\multirow{2}{*}{Cooooooool1} & \multirow{2}{*}{Cooooool2} & ``Col & \multirow{2}{*}{Cooooool1} &
\multirow{2}{*}{Cooooool2} & ``Coooooool & \multirow{2}{*}{Coooooool1} & \multirow{2}{*}{Coooooool2} & ``Coooooool & \\% <--- trailing "&" was missing.
% & & & 3'' & & & 3'' & & & 3'' \\
& & & & & & & & & 3'' \\% <--- changed this line as well, seemed wrong.
\hline
1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 & 10 \\
\hline
\end{tabular}
}
\bigskip
\hspace{-3cm}
\footnotesize{
\noindent
\begin{tabular}{cccccccccc} \toprule
& \multicolumn{3}{p{4.63cm}}{\centering Very long column title that needs to be centered} &
\multicolumn{3}{p{5.73cm}}{\centering Very long column title that needs to be centered 2} &
\multicolumn{3}{p{4.65cm}}{\centering Very long column title that needs to be centered 3} \\
\cmidrule(l{2pt}r{4pt}){1-1}
\cmidrule(l{4pt}r{4pt}){2-4}
\cmidrule(l{4pt}r{4pt}){5-7}
\cmidrule(l{4pt}r{2pt}){8-10}
\multirow{2}{*}{Cooooooool1} & \multirow{2}{*}{Cooooool2} & ``Col & \multirow{2}{*}{Cooooool1} &
\multirow{2}{*}{Cooooool2} & ``Coooooool & \multirow{2}{*}{Coooooool1} & \multirow{2}{*}{Coooooool2} & ``Coooooool & \\% <--- trailing "&" was missing.
% & & & 3'' & & & 3'' & & & 3'' \\
& & & & & & & & & 3'' \\% <--- changed this line as well, seemed wrong.
1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 & 10 \\
\bottomrule
\end{tabular}
}
\end{document}
p{3cm}
restriction then the large titles make the table extend beyond the page margins. How could I restrain the length of the column titles without fixing the length withp
?