Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

MWE of my table:

\documentclass[a4paper,10pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{graphicx}
\usepackage{multirow}
\usepackage{array}
\begin{document}

\begin{table}
\hspace{-3cm}
\footnotesize{
\begin{tabular}{|c|c|c|c|c|c|c|c|c|c|}
\hline
 & \multicolumn{3}{p{3cm}}{\centering Very long column title that needs to be centered} &
\multicolumn{3}{p{3cm}}{\centering Very long column title that needs to be centered 2} &
\multicolumn{3}{p{3cm}|}{\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 \\
 & & & 3'' & & & 3'' & & & 3'' \\
\hline
1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 & 10 \\ 
\hline
\end{tabular}
}
\end{table}

\end{document}

which gives me:

table

You'll notice that the column titles are centered but not along the three columns they occupy. How could I make them be centered?

share|improve this question
1  
Most likely your p-columns are too narrow. You should not mix variable-length columns (like "c") and fixed-length columns ("p"). –  Martin Schröder Nov 16 '12 at 13:02
    
But if I lift the 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 with p? –  Gabriel Nov 16 '12 at 13:04
add comment

2 Answers

up vote 7 down vote accepted

TeX isn't really very good at this. It's simplest if you are prepared to break the titles manually:

enter image description here

\documentclass[a4paper,10pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{graphicx}
\usepackage{multirow}
\usepackage{array}
\begin{document}

\begin{table}
\hspace{-3cm}
\footnotesize{
\begin{tabular}{|c|c|c|c|c|c|c|c|c|c|}
\hline
 & \multicolumn{3}{c|}{Very long column title}&
 \multicolumn{3}{c|}{Very long column title}&
 \multicolumn{3}{c|}{Very long column title}\\
 & \multicolumn{3}{c|}{that needs to be}&
 \multicolumn{3}{c|}{that needs to be}&
 \multicolumn{3}{c|}{that needs to be}\\
 & \multicolumn{3}{c|}{centered}&
 \multicolumn{3}{c|}{centered 2}&
 \multicolumn{3}{c|}{centered 3}\\
\hline
 \multirow{2}{*}{Cooooooool1} & \multirow{2}{*}{Cooooool2} & ``Col &     \multirow{2}{*}{Cooooool1} &
\multirow{2}{*}{Cooooool2} & ``Coooooool & \multirow{2}{*}{Coooooool1} &     \multirow{2}{*}{Coooooool2} & ``Coooooool& \\
 & & & 3'' & & & 3'' & & & 3'' \\
\hline
1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 & 10 \\ 
\hline
\end{tabular}
}
\end{table}

\end{document}
share|improve this answer
    
Well that's exactly what I was trying to avoid haha. I'll wait to see if someone comes up with a less "hacky" solution (no offense intended), if not I'll mark this answer as accepted. Than you! Regards. –  Gabriel Nov 16 '12 at 13:17
2  
If you don't do that then basically you need a multi-pass alogorihm where tex first does the table then you calculate the widths then put in the headings with the right p{} value. Either saving the widths in the aux file and taking multiple runs (like longtable) or saving in a box and doing multiple trials in the same run (like tabularx) It may be that there is already a package that does this (tabu perhaps has something?) but I don't know one without searching ctan... –  David Carlisle Nov 16 '12 at 13:24
add comment

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:

enter image description here

Alternatively I would recommend using the booktabs package:

enter image description here

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}
share|improve this answer
    
This is an amazing answer and clearly your LaTeX coding is superb. However if you don't mind I'll stick to the answer by Mr Carlisle due to the complexity of this one, even though your answer is the correct one. Regards. –  Gabriel Nov 17 '12 at 21:40
    
@Gabriel: No problem, you should use the one you are comfortable with. But the only complexity here is in automatically determining the widths of the columns. If this it not something that you do often, you could just manually adjust the p{} column widths until you got a solution that works. I will update the answer to show the solution as well. –  Peter Grill Nov 17 '12 at 21:44
    
@Gabriel: Have updated answer. See if the updated version seems less compilcated. –  Peter Grill Nov 17 '12 at 22:04
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.