1

In the sample table below, I would like the column headings in the first row to span three cells each. But they wrap in a single cell. Would appreciate if somebody could help fix this.

\documentclass[11pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{tabulary,tcolorbox,xcolor,tabularx,siunitx,booktabs}
\sisetup{add-decimal-zero = true,add-integer-zero = true,round-integer-to-decimal,round-mode = places,round-precision=1,}
\newcommand{\mcx}[3]{\multicolumn{3}{>{\centering\arraybackslash}X}{#1}}
\newcommand{\mcone}[1]{\multicolumn{1}{>{\centering\arraybackslash}X}{#1}}
\date{\today}
\title{Testing multicolumn commands}
\begin{document}

\maketitle
\begin{table}[htbp]
\caption{\label{tab:orga246370}
Caption of my table}
\centering
\begin{tabularx}{\textwidth}{l*6S[table-format=2.1,round-precision=1]}
\toprule
States & \mcx{Proportion of households with two cars} &  &  & \mcx{Proportion of households with three cars} &  & \\
 & \mcone{1991} & \mcone{2002} & \mcone{2012} & \mcone{1991} & \mcone{2002} & \mcone{2012}\\
\midrule
Punjab & 12.7 & 9.7 & 11 & 19.2 & 18.7 & 26.1\\
Bihar & 6.9 & 12.3 & 17.1 & 5.5 & 12 & 22.5\\
India & 9.3 & 8 & 10.3 & 8.7 & 6.7 & 11.1\\
\bottomrule
\end{tabularx}
\end{table}
\end{document}

1 Answer 1

3

enter image description here

changes:

  • definition of \mcx. it has only one parameter (for content of cells) and width of three cells. width is calculated with \dimexpr ...
  • in the first row (with \mcx cells) are removed superfluous ampersands (between \mcx had to be only one)
  • off-topic: i move word "states" into second row (but f not liked this, you can simple return to the first)

\documentclass[11pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{booktabs, tabularx, tabulary}
\newcolumntype{C}{>{\centering\arraybackslash}X}
\usepackage{tcolorbox} 
\usepackage{xcolor}
\usepackage[add-decimal-zero = true,
            add-integer-zero = true,
            round-integer-to-decimal,
            round-mode = places,
            round-precision=1]{siunitx}
\newcommand{\mcx}[1]{\multicolumn{3}{>{\hsize=\dimexpr3\hsize+4\tabcolsep\relax}C}{#1}}
\newcommand{\mcone}[1]{\multicolumn{1}{C}{#1}}
\date{\today}
\title{Testing multicolumn commands}

\begin{document}
\maketitle

\begin{table}[htbp]
\caption{\label{tab:orga246370}
Caption of my table}
\centering
\begin{tabularx}{\textwidth}{l*{6}{S[table-format=2.1]}}
\toprule
        & \mcx{Proportion of households with two cars}
            & \mcx{Proportion of households with three cars}    \\
    \cmidrule(lr){2-4}\cmidrule(lr){5-7}
States  & \mcone{1991} & \mcone{2002} & \mcone{2012}
        & \mcone{1991} & \mcone{2002} & \mcone{2012}            \\
    \midrule
Punjab  & 12.7  & 9.7   & 11    & 19.2  & 18.7  & 26.1          \\
Bihar   & 6.9   & 12.3  & 17.1  & 5.5   & 12    & 22.5          \\
India   & 9.3   & 8     & 10.3  & 8.7   & 6.7   & 11.1          \\
    \bottomrule
\end{tabularx}
\end{table}
\end{document}

edit:

in defining \multicolumn in tabularx which consider column type X from tabularx the width multicolumn cells consider only maximal width of cells content and not the width of cells. with other words

\multicolumn{2}{>{\hsize=2\hsize}X}{...}

not consider all tabcolsep spaces in these two columns, consequently multicolumn cell is narrower for four spaces of tabcolsep. therefore in definition of for "\newcommand\mcx{...}` (by which is simpler typesetting table) is defined in above mwe as

\newcolumntype{C}{>{\centering\arraybackslash}X}% definition of `C`
\newcommand{\mcx}[1]{\multicolumn{3}{>{\hsize=\dimexpr3\hsize %width of 3 cells without \tabcolsep
                                     + 6\tabcolsep\relax}% added width of all `tabcolep spaces in merged cells
                                      C}{#1}}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .