GeneralCentering multicolumn headings

LaTeX specific issues not fitting into one of the other forums of this category.
outspokengolem
Posts: 4
Joined: Mon Jul 14, 2008 3:00 am

Centering multicolumn headings

Post by outspokengolem »

I want to centre a column heading. I've tried something like:

Code: Select all

\begin{tabular}{ll} \hline
\multicolumn{2}{c}{This is some very long text...} \\
Short text & Short text \\ \hline
\end{tabular}
but because the heading is longer than the text underneath it, the heading is not centred as one would expect. Is there any LaTeX trickery to fix this?

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

Stefan Kottwitz
Site Admin
Posts: 10345
Joined: Mon Mar 10, 2008 9:44 pm

Centering multicolumn headings

Post by Stefan Kottwitz »

Hi,

the heading will be centered in its cell, or do you get something different? What exactly do you expect? Should the width of the other columns be adjusted? Something like this?

Code: Select all

\usepackage{tabularx}
...
\begin{tabularx}{7cm}{>{\centering}X>{\centering\arraybackslash}X}
\hline
\multicolumn{2}{c}{This is some very long long long text...} \\
Short text & Short text \\ \hline
\end{tabularx}
Stefan
LaTeX.org admin
outspokengolem
Posts: 4
Joined: Mon Jul 14, 2008 3:00 am

Centering multicolumn headings

Post by outspokengolem »

Getting there Stefan, although you've fixed my specific example rather than helped out in the general case. For example, you've used tabularx, which is inappropriate because I want the table to be at its natural width (how did you know to use 7cm?) and you've changed the alignment of the table text to centre.

I should have been more specific in my example I guess (although I am still interested in a general fix). My specific problem is I have a table of figures and I want to centre the column heading over the figures, which are aligned at the decimal point.

Code: Select all

\begin{tabular}{r@{.}l}
\hline
\multicolumn{2}{c}{This is a very long heading} \\
1&2 \\
3&45 \\
--4&1 \\
14&6 \\ \hline
\end{tabular}
User avatar
gmedina
Posts: 2313
Joined: Wed Jul 11, 2007 11:45 pm

Centering multicolumn headings

Post by gmedina »

Perhaps you should consider using the dcolumn package. Refer to the documentation for further information:

Code: Select all

\documentclass{article}
\usepackage{dcolumn}

\begin{document}

\begin{tabular}{D..{2.2}}
  \hline
  \multicolumn{1}{c}{This is a very long heading} \\
  1.2 \\
  3.45 \\
  -4.1 \\
  14.6 \\ \hline
\end{tabular}

\end{document}
1,1,2,3,5,8,13,21,34,55,89,144,233,...
outspokengolem
Posts: 4
Joined: Mon Jul 14, 2008 3:00 am

Re: Centering multicolumn headings

Post by outspokengolem »

Yeah, I've stumbled on to dcolumn already (by accident mind you). There's also rccol that does a similar job. However they rely on specifying the number of digits in the column specifier in order to allow the right column width; they don't actually solve the general problem I have, which is being able to centre headings over multiple columns. Also neither dcolumn or rccol account for digit grouping symbols (or other symbols for that matter, except for a minus sign in the case of rccol) in calculating the column width.
Trikes120
Posts: 3
Joined: Wed Oct 29, 2008 8:58 am

Centering multicolumn headings

Post by Trikes120 »

So I stumbled upon this topic and I have the same problem. Here's a code example you guys can generate to see it. Essentially, the headings 'Beer Solids Content' and 'Stillage Solids Content' are wider than the data columns below them. As an odd consequence the $\pm$ do not line up with \% or the center of the heading (uses booktabs).

Code: Select all

\begin{table} [p] \centering
	\caption[Beer and stillage solids contents]{Beer and stillage solids contents (means of triplicate runs $\pm$ standard deviation).}
	\begin{tabular}{@{}>{\arraybackslash}llr@{\,$\pm$\,}lr@{\,$\pm$\,}l@{}} \toprule
		& & \multicolumn{2}{c}{\parbox{2.6cm}{\centering Beer \\ Solids Content}}
		& \multicolumn{2}{c}{\parbox{2.6cm}{\centering Stillage \\ Solids Content}} \\ \cmidrule(l){3-6}
		
		\multicolumn{2}{l}{Treatment}
		& \multicolumn{2}{c}{\%}
		& \multicolumn{2}{c}{\%} \\ \midrule
		
 		30\% & Conventional &  11.7 & 0.5 a &  18.4 & 1.2 g \\
      				 & Vacuum &  16.6 & 1.4 b &  20.9 & 1.0 h \\ \bottomrule
	\end{tabular}
	\label{tab:solidsContent}
\end{table}
I can identify the specific issue but am not sure how to resolve it:
There are two columns under each "Solids" heading. Instead of splitting the two columns evenly under the heading, the first column is sized according to the data, followed by the column separator $\pm$, and the second column fills the rest of the space. This effect shifts all the data under each Solids heading to the left, ruining the centering job.

Lemme know if you need further clarification. I feel like i'm missing something simple.
User avatar
Juanjo
Posts: 657
Joined: Sat Jan 27, 2007 12:46 am

Centering multicolumn headings

Post by Juanjo »

Try this:

Code: Select all

   \begin{tabular}{@{}ll@{\hspace{3.5em}}r@{\,$\pm$\,}l@{\hspace{3.5em}}r@{\,$\pm$\,}l@{}} 
      \toprule
      & & \multicolumn{2}{@{\hspace{-1.2em}}p{2.6cm}}{\centering Beer \\ Solids Content}
      & \multicolumn{2}{@{\hspace{-1.2em}}p{2.6cm}}{\centering Stillage \\ Solids Content} \\ 
      \cmidrule(l{-1.2em}){3-6}
      \multicolumn{2}{l}{Treatment} & \multicolumn{2}{@{\hspace{-3.3em}}c}{\%}
      & \multicolumn{2}{@{\hspace{-1.6em}}c}{\%} \\ \midrule
      30\% & Conventional &  11.7 & 0.5 a &  18.4 & 1.2 g \\
                   & Vacuum &  16.6 & 1.4 b &  20.9 & 1.0 h \\ \bottomrule
   \end{tabular}
The CTAN lion is an artwork by Duane Bibby. Courtesy of www.ctan.org.
Trikes120
Posts: 3
Joined: Wed Oct 29, 2008 8:58 am

Centering multicolumn headings

Post by Trikes120 »

That certainly helped. I took a cue from your technique and tried adding added >{\hspace{0.8em} in the header:

Code: Select all

\begin{tabular}{@{}ll>{\hspace{0.8em}}r@{\,$\pm$\,}l>{\hspace{0.8em}}r@{\,$\pm$\,}l@{}}
and that fixed it right up. Many thanks for the idea.
diegos
Posts: 1
Joined: Mon Jun 01, 2009 7:28 pm

Centering multicolumn headings

Post by diegos »

I have just uploaded a package to CTAN that solves the problem in a general way: http://tug.ctan.org/tex-archive/macros/ ... /tabularew. It's almost untested, so beware of bugs and incompatibilities. Anyway, it works for the last example in this thread:

Code: Select all

\usepackage{array, booktabs}
\usepackage{tabularew}

...

\newcolumntype{w}{>{\spew{.5}{+1}}}

\begin{table} [p] \centering
   \caption[Beer and stillage solids contents]{Beer and stillage solids contents (means of triplicate runs $\pm$ standard deviation).}
   \begin{tabularew}{@{}>{\arraybackslash}llwr@{\,$\pm$\,}lwr@{\,$\pm$\,}l@{}} \toprule
      & & \multicolumn{2}{c}{\parbox{2.6cm}{\centering Beer \\ Solids Content}}
      & \multicolumn{2}{c}{\parbox{2.6cm}{\centering Stillage \\ Solids Content}} \\ \cmidrule(l){3-6}
      
      \multicolumn{2}{l}{Treatment}
      & \multicolumn{2}{c}{\%}
      & \multicolumn{2}{c}{\%} \\ \midrule
      
      30\% & Conventional &  11.7 & 0.5 a &  18.4 & 1.2 g \\
                   & Vacuum &  16.6 & 1.4 b &  20.9 & 1.0 h \\ \bottomrule
   \end{tabularew}
   \label{tab:solidsContent}
\end{table}
and (of course) for the basic one:

Code: Select all

\usepackage{tabularew}

...

\newcolumntype{w}{>{\spew{.5}{+1}}}

\begin{tabularew}{wr@{.}l}
\hline
\multicolumn{2}{c}{This is a very long heading} \\
1&2 \\
3&45 \\
--4&1 \\
14&6 \\ \hline
\end{tabularew}
The idea is to add space in the preamble, in much the same way as Juanjo and Trikes120 have done above. The tabularew package helps to compute the exact space needed. It evaluates the whole table three times to this purpose.

Please read the documentation to understand how tabularew works and should be used. And let me know your opinion.
Trikes120
Posts: 3
Joined: Wed Oct 29, 2008 8:58 am

Re: Centering multicolumn headings

Post by Trikes120 »

Diegos,

Many thanks for the package. I haven't had the chance to try it. Frankly, all of my work was for a thesis, which was done and printed 6 months ago. If I'm stuck with doing another thesis or need to use LaTeX again, I will definitely add this package first. Hours and hours of frustration preceded it.

Best,
-J
Post Reply