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.

I have the following table, but the multicolumn cell width is not the same as the sum of the cells it is combined from.

I assume, this is caused by some margins in each cell, i.e. that the cel width (distance between the two vertical lines) is equal to the set cell width plus some margin - but what is this margin, or what is the easiest way to set the width of the multicolumn cell to be equal to the sum of the widths of the cells it is combined of?

Thanks

\documentclass[10pt]{article}
\usepackage{array}

\begin{document}

\begin{tabular}
    {|>{\raggedright}p{2cm}l|>{\centering}p{0.9cm}|>{\centering}p{0.9cm}|>{\centering}p{0.9cm}|}
    \hline 
     &  & {March} & {April} & {May}\tabularnewline
    \hline 
    \multicolumn{2}{|c|}{{Species}} & \multicolumn{3}{>{\centering}p{2.7cm}|}{{This is the problem cell, as it has not the width as the ones combined}}\tabularnewline
    \hline 
\end{tabular}

\end{document}

results in the following table:

results in this table

share|improve this question

1 Answer 1

up vote 2 down vote accepted

There is 2*\tabcolsep between each column, and in addition (if using array package) \arrayrulewidth for each vertical rule.

If you load the calc package you could use

  p{2.7cm+4\tabcolsep+2\arrayrulewidth}
share|improve this answer
    
Nice - works perfectly. But I have many multicolumn cells consisting of different number of cells - Can I use the 'calc' package to write a formula in the preamble, so that I simply have to use 'p{\TheFormula{x}}' where I simply replace x with the number of columns? –  Rainer Sep 14 '12 at 12:04
    
well you can write a formula for the correction \def\theformula#1{(#1-1)*(2*\tabcolsep + \arrayrulewidth)} but latex tables don't by default record the p-column widths anywhere so you would need in addition to supply 2.7cm here which here is 3*0.9 but in general would be the sum of the spanned p column widths. –  David Carlisle Sep 14 '12 at 12:32
    
OK. I have now \def\mcolWidth#1{(#1*real{0.9})+(#1-1)*(2*\tabcolsep + \arrayrulewidth)} where the 0.9 is the original colum width. In my table I am trying to use p{\mcolWidth{3}} but it doesn't work. What am I missing? –  Rainer Sep 14 '12 at 13:12
    
well a \ before real is missing, not sure if calc follows * + precedence it wouldn't hurt to put an extra () from the + to the end. Rather than test it in the table first do \setlength{\dimen0}{....}\showthe\dimen0 and check you get the length expected –  David Carlisle Sep 14 '12 at 13:52
    
I am giving up for now with the definition of a formula - I am enter it manually - it is anyway only copy / paste. Thanks a lot. –  Rainer Sep 14 '12 at 14:08

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.