3

How can I center the two columns inside the multicolumn?

enter image description here

MWE

\documentclass{article}

\usepackage{booktabs}
\usepackage{multirow}
\usepackage{siunitx}
\sisetup{input-ignore={,},
         input-decimal-markers={.},
         group-separator={,},
         group-minimum-digits=4
}

\begin{document}

{\renewcommand{\arraystretch}{1.2}% for the vertical padding
\begin{tabular}{cc
|
S[table-format=1.0]
S[table-format=3.0]
|
S[table-format=4.0]
|
}
\cline{3-5}
& &   \multicolumn{2}{c|}{has breast cancer} & \\
& &    {yes} & {no} & {total} \\
\hline
\multicolumn{1}{|c}{\multirow{2}{*}{screened positive}} &
\multicolumn{1}{c|}{yes} & 7 & 70 & 77     \\
\multicolumn{1}{|c}{}        &                
\multicolumn{1}{c|}{no}& 1 & 922 & 923   \\
\hline
\multicolumn{1}{|c}{}
& total & 8 & 992 & 1,000 \\
\hline
\end{tabular}
}

\end{document}


\documentclass{article}

\usepackage{booktabs}
\usepackage{multirow}
\usepackage{siunitx}
\sisetup{input-ignore={,},
         input-decimal-markers={.},
         group-separator={,},
         group-minimum-digits=4
}

Edit (in response to @Bernard 's comment):

I added the vertical line and edited one of the column specifications and data so that the columns should be the same width. The result changes the thrust of my question though not the goal. I need to ask why the columns have such different widths. The lack of centering seems to be a side effect.

enter image description here

Modified MWE:

\begin{document}

{\renewcommand{\arraystretch}{1.2}% for the vertical padding
\begin{tabular}{cc
|
%S[table-format=1.0]
S[table-format=3.0]
|
S[table-format=3.0]
| % extra vertical line
S[table-format=4.0]
|
}
\cline{3-5}
& &   \multicolumn{2}{c|}{has breast cancer} & \\
& &    {yes} & {no} & {total} \\
\hline
\multicolumn{1}{|c}{\multirow{2}{*}{screened positive}} &
\multicolumn{1}{c|}{yes} & 7 & 70 & 77     \\
\multicolumn{1}{|c}{}        &                
\multicolumn{1}{c|}{no}& 1 & 922 & 923   \\
\hline
\multicolumn{1}{|c}{}
%& total & 8 & 992 & 1,000 \\
& total & 800 & 992 & 1,000 \\
\hline
\end{tabular}
}

\end{document}
  • If you add a verticel rule between the two columns under the multicolumn, you'll see they are centred. It s the lack of vertical rule, that make them look uncentred, due to the difference of their widths. – Bernard May 31 '15 at 20:47
5

By using \makebox one can force the entries {yes} and {no} to each fill up half the available space. Note, the outer {} is needed around \makebox for some reason.

\documentclass{standalone}
\usepackage{booktabs}
\usepackage{multirow}
\usepackage{siunitx}
\sisetup{input-ignore={,},
         input-decimal-markers={.},
         group-separator={,},
         group-minimum-digits=4
}

\newlength{\tempdima}

\begin{document}

\settowidth{\tempdima}{has breast cancer}% compute width needed
\addtolength{\tempdima}{-2\tabcolsep}% minus default column sep

{\renewcommand{\arraystretch}{1.2}% for the vertical padding
\begin{tabular}{cc
|
S[table-format=1.0]
S[table-format=3.0]
|
S[table-format=4.0]
|
}
\cline{3-5}
& &   \multicolumn{2}{c|}{has breast cancer} & \\
& &   {\makebox[0.5\tempdima]{yes}} & {\makebox[0.5\tempdima]{no}} & {total} \\
\hline
\multicolumn{1}{|c}{\multirow{2}{*}{screened positive}} &
\multicolumn{1}{c|}{yes} & 7 & 70 & 77     \\
\multicolumn{1}{|c}{}        &                
\multicolumn{1}{c|}{no}& 1 & 922 & 923   \\
\hline
\multicolumn{1}{|c}{}
& total & 8 & 992 & 1,000 \\
\hline
\end{tabular}
}

\end{document}

makebox in taqbular

  • This works even when the text "has breast cancer" is replaced by something much shorter than "yes ... no". So I should be able to automate it with a macro for other similar tables. Thank you. – Ethan Bolker Jun 1 '15 at 0:29
  • @Mic0 - Yup. I tested it by comparing it with using @{}. Will edit solution. Thank you for catching that. – John Kormylo Jun 1 '15 at 13:05
  • The "some reason" for having to place curly braces around the two \makebox instructions comes from the fact that the default column type for both columns is S. Without the curly braces, siunitx will try to parse the material, which cannot be the intent. With the curly braces, the material will simply be centered. (Of course, given that the boxes take up the full width of the respective columns, centering the columns has no further effect.) – Mico Jun 1 '15 at 17:23
0

How about this?

\documentclass{article}

\usepackage{booktabs}
\usepackage{multirow}
\usepackage{siunitx}
\sisetup{input-ignore={,},
         input-decimal-markers={.},
         group-separator={,},
         group-minimum-digits=4
}

\begin{document}

{\renewcommand{\arraystretch}{1.2}% for the vertical padding
\begin{tabular}{cc|c|c|c|}

\cline{3-5}
& &   \multicolumn{2}{c|}{has breast cancer} &  \\
& &  {yes} & {no} & {total} \\
\hline
\multicolumn{1}{|c}{\multirow{2}{*}{screened positive}} &
\multicolumn{1}{c|}{yes} & 7 & 70 & 77     \\
\multicolumn{1}{|c}{}        &                
\multicolumn{1}{c|}{no}& 1 & 922 & 923   \\
\hline
\multicolumn{1}{|c}{}
& total & 8 & 992 & 1,000 \\
\hline
\end{tabular}
}

\end{document}

enter image description here

  • This doesn't do what I need. Without the siunitx S option it centers the numbers rather than right justifying them in a centered column. The two columns beneath the multicolumn aren't centered there. And there's an extra vertical rule. I added the image that compiled from your code. – Ethan Bolker May 31 '15 at 20:18

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.