4

I'm creating a table but I'm not getting the correct alignment in the last row of the table. It's a multirow and muticolumn example. Of course, I'm putting some fictitious information and data in the table :) but my original table has the same format. I'm putting a minimal code below. There are two problems:

  1. I can't get the numbers 12 and 1000 in the last column aligned in the center position as the other numbers are. Now I have simple small numbers as 12 and 1000 but the alignment becomes worse if I have bigger numbers and with decimals.

  2. A second problem is setting the 10 and 3 in the "sex ratio between second and first value" rows to be aligned in the middle between "male" and "female".

    \documentclass[10pt]{article}
    \usepackage[latin1]{inputenx}
    \usepackage[T1]{fontenc}
    \usepackage{amssymb}
    \usepackage{amsmath}
    \usepackage{booktabs}
    \usepackage[below]{placeins}
    \usepackage{threeparttable}
    \usepackage{multirow}
    
    \begin{document}
    \begin{table}[htbp]
    \begin{center}
    \begin{threeparttable}
    \caption{\label{tab:freqcompa} Testing for table}
    
    \begin{tabular}{@{}lcccc@{}}
    \toprule
    \multicolumn{1}{c}{\multirow{2}{2cm}{Case\tnote{2}}} & \multicolumn{4}{c}{Number 
    of dogs} \\
    \cmidrule(r){2-5}
    \multicolumn{1}{c}{} & \multicolumn{2}{c}{Dogs of type 1} & \multicolumn{2}{c}{
    Very very bad Dogs of type 2\tnote{1}} \\
    \midrule
    \multicolumn{1}{c}{} & \multicolumn{1}{c}{male} & \multicolumn{1}{c}{female}
    & \multicolumn{1}{c}{male} & \multicolumn{1}{c}{female}\\
    $\omega_a= 100$  & $1$ & $2$ & $4$ & $12$ \\[2mm]
    \cmidrule(r){1-1}
    Sex ratio \\[-1.5mm]
    between second \\[-1.5mm]
    and first value & \multicolumn{2}{c}{$2$}  & \multicolumn{2}{c}{$3$} \\[2.3mm]
    \midrule
    \multicolumn{1}{c}{} & \multicolumn{1}{c}{male} & \multicolumn{1}{c}{female}
    & \multicolumn{1}{c}{male} & \multicolumn{1}{c}{female}\\
    $\omega_b= 200$ & $1$ & $60$ & $100$ & $1000$ \\[2mm]
    \cmidrule(r){1-1}
    Sex ratio \\[-1.5mm]
    between second \\[-1.5mm]
    and first value & \multicolumn{2}{c}{$60$}  & \multicolumn{2}{c}{$10$} \\[2.3mm]
    
    \bottomrule
    \end{tabular}
    \begin{tablenotes}
    \item[1]\textit{considering all stray dogs.}
    \item[2]\textit{considering two possible cases}
    \end{tablenotes}
    
    \end{threeparttable}
    \end{center}
    \end{table}
    \end{document} 
    

Any help will be most appreciated...

2
  • I think that this minimal example isn't so minimal. People are trying to help you but you seem to be adding more and more things you need help with. Please formulate the problem more precisely and you may get help. if there are irrelevant parts, then remove them. you need to do some work too, if you want other people to work for you. Nov 1 '10 at 11:39
  • @yossi : Thanks. I have made some changes and explained that I have 2 problems. I believe that the problem is minimal as it's a complex table (at least for me) and I can't make it simpler. Nov 1 '10 at 16:35
3

To explain the alignment behavior: by this line

\begin{tabular}{@{}lcccc@{}}

you format the last column by c@{}, removing the small space which would follow. But here

... & \multicolumn{1}{c}{female}\\

you would get this default space. That's the reason why the numbers and the word 'female' aren't aligned correctly. Change to

... & \multicolumn{1}{c@{}}{female}\\

and you will notice, that 'female', 12 and 1000 will become horizontally centered.

4
  • kottwitz : thanks but it doesn't work. Try replacing "Dogs of Type 2" with "Very Very Bad Dogs of type 2" and u will see that the 3 and 10 are not centered between male and female (in the line sex ratio between second and first value). Oct 31 '10 at 19:13
  • @yCalleecharan: Regarding that other problem: I would create another column for the values 3 and 10 instead of aligning them to the merged column which consists of columns of different width.
    – Stefan Kottwitz
    Oct 31 '10 at 20:41
  • kottwitz : Thanks. I see what u mean but it doesn't seem to give the correct result (not with my code and nor with Thorsten's code). Nov 1 '10 at 11:28
  • 1 vote up for your suggestion. Nov 1 '10 at 16:36
2

If you want a centered last column, you should choose the »c« column type instead of »r« (edited while I was typing my answer?). In your table you can also add other improvements. And you don't need so much \multicolumns. See code below for inspiration.

\documentclass[10pt]{article}
\usepackage[T1]{fontenc}
\usepackage[latin1]{inputenx}
\usepackage[font=small,labelfont=bf,tableposition=top]{caption}
\usepackage{booktabs}
\usepackage{threeparttable}
\usepackage{array,multirow}

\begin{document}
  \begin{table}[!ht]
    \caption{Testing for table}\label{tab:freqcompa}
    \centering
    \begin{threeparttable}
      \begin{tabular}{@{}>{\raggedright}m{2.8cm}cccc@{}}\toprule
        \multicolumn{1}{c}{\multirow{2}{*}[-0.5ex]{Case\tnote{2}}} & \multicolumn{4}{c}{Number of dogs} \\ \cmidrule(r){2-5}
        \multicolumn{1}{c}{} & \multicolumn{2}{c}{Dogs of type 1} & \multicolumn{2}{c}{Dogs of type 2\tnote{1}} \\ \midrule
        & male & female & male & female\\
        $\omega_a=100$  & 1 & 2 & 4 & 12 \\ \addlinespace\cmidrule(r){1-1}
        Sex ratio between second and first value & \multicolumn{2}{c}{2}  & \multicolumn{2}{c}{3} \\ \addlinespace\midrule
        & male & female & male & female\\
        $\omega_b=200$ & 1 & 60 & 100 & 1000 \\\addlinespace\cmidrule(r){1-1}
        Sex ratio between second and first value & \multicolumn{2}{c}{60} & \multicolumn{2}{c}{10} \\ \bottomrule
      \end{tabular}
      \begin{tablenotes}
        \item[1]\textit{considering all stray dogs.}
        \item[2]\textit{considering two possible cases}
      \end{tablenotes}
    \end{threeparttable}
  \end{table}
\end{document}
2
  • donig :Thanks. If we change "Dogs of type 2" to "Dogs of very bad type 2", then u can see that the numbers 3 and 10 in the sex ratio rows are not centered anymore between male and female. This is another main problem that I was trying to fix. I tried to create many columns as u realised but this didn't help also. Oct 31 '10 at 15:38
  • 1 vote up for a cleaner code. Nov 1 '10 at 16:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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