0

The table headings are long, and at the same time, we need to have each heading for two years: that results into two columns under each heading. In this case, heading text is not wrapping. Guidance please!

Here is the code:

\documentclass[12pt]{article}
\usepackage[margin=1in]{geometry}
\usepackage[none]{hyphenat}
\usepackage[flushleft]{threeparttable}
\usepackage{tabularx, ragged2e}
\usepackage{booktabs}
\usepackage{multicol}

\begin{document}
\newcolumntype{C}{>{\Centering\arraybackslash\hspace{0pt}}X}
\begin{table}
\centering
\begin{threeparttable}
\caption{Composition of merchandised exports} \label{t:comp}
\setlength\tabcolsep{2pt} % default value: 6pt
\small
\begin{tabularx}{\textwidth}{p{.15\textwidth}*{10}{C}} \toprule
 &
  \multicolumn{2}{c}{Primary commodities} &
  \multicolumn{2}{c}{Labor/resource intensive} &
  \multicolumn{2}{c}{Low skill/technology intensive} &
  \multicolumn{2}{c}{Medium skill/technology intensive} &
  \multicolumn{2}{c}{High skill/technology intensive} \\
\cmidrule(l){2-3} \cmidrule(l){4-5} \cmidrule(l){6-7}\cmidrule(l){8-9} \cmidrule(l){10-11}
 & 2001 & 2020 & 2001 & 2020 & 2001 & 2020 & 2001 & 2020 & 2001 & 2020 \\ \midrule
Afghanistan & 82.5 & 94.4 & 6.5  & 1.6  & 3.8  & 0.3  & 2.6  & 1.0  & 4.0  & 0.8  \\
Bangladesh  & 7.3  & 4.6  & 89.7 & 92.6 & 0.3  & 0.9  & 1.0  & 0.8  & 1.6  & 1.0  \\
Bhutan      & 48.5 & 39.8 & 21.1 & 6.5  & 10.7 & 45.2 & 3.1  & 0.9  & 16.2 & 7.5  \\
India       & 35.6 & 34.8 & 30.5 & 14.4 & 6.8  & 10.0 & 7.7  & 15.1 & 16.4 & 25.6 \\
Maldives    & 32.9 & 92.9 & 62.6 & 0.4  & 1.4  & 2.6  & 1.8  & 1.1  & 1.0  & 2.6  \\
Nepal       & 26.0 & 30.3 & 55.4 & 47.5 & 7.5  & 11.1 & 3.0  & 1.8  & 8.2  & 9.2  \\
Pakistan    & 14.9 & 25.0 & 75.7 & 63.5 & 1.5  & 1.5  & 4.1  & 2.9  & 3.6  & 6.9  \\
Sri Lanka   & 26.4 & 34.6 & 60.4 & 48.5 & 1.1  & 2.0  & 5.9  & 10.3 & 5.0  & 4.7 \\ \bottomrule
\end{tabularx}
\begin{tablenotes}[flushleft]
\setlength\labelsep{0pt}
\footnotesize 
\item Source: United Nations Conference on Trade and Development \\
\end{tablenotes}
\end{threeparttable}
\end{table}
\end{document}

1 Answer 1

1

Like this?

enter image description here

Your column headers not wrapping because used multicolumn{2}{c}{...} doesn't enable breaking of contained text into more lines. For this It should use column type as p{...} or X (or its derivatives), whre you need determine its width accordingly. For example as is done in proposed solution below.

With use of the siunitx package and use

  • defined new column type Y for columns headers which has width of two columns,
  • defined new command \newcommand\mcx[1]{\multicolumn{1}{C}{#1}} which actually determine columns widths, and
  • S column type defined in the siunitx package for aligning numbers at their decimal points:
\documentclass[12pt]{article}
\usepackage[margin=1in]{geometry}

\usepackage{ragged2e}
\usepackage{booktabs, tabularx}
\newcolumntype{C}{>{\Centering\arraybackslash}X}
\newcolumntype{Y}{>{\Centering\arraybackslash
                    \hsize=2\linewidth}X}
\newcommand\mcx[1]{\multicolumn{1}{C}{#1}}
\usepackage[flushleft]{threeparttable}

\usepackage{siunitx}

\begin{document}
    \begin{table}
    \small
    \setlength\tabcolsep{2pt} % default value: 6pt
    \renewcommand\tabularxcolumn[1]{b{#1}}
\begin{threeparttable}
    \caption{Composition of merchandised exports} \label{t:comp}
\begin{tabularx}{\textwidth}{@{} l *{10}{S[table-format=2.1]} @{}} \toprule
 &  \multicolumn{2}{Y}{Primary commodities} &
    \multicolumn{2}{Y}{Labor/resource intensive} &
    \multicolumn{2}{Y}{Low skill/ technology intensive} &
    \multicolumn{2}{Y}{Medium skill/ technology intensive} &
    \multicolumn{2}{Y}{High skill/ technology intensive} \\
    \cmidrule(l){2-3} \cmidrule(l){4-5} \cmidrule(l){6-7}\cmidrule(l){8-9} \cmidrule(l){10-11}
 & \mcx{2001} & \mcx{2020} & \mcx{2001} & \mcx{2020} & \mcx{2001} 
 & \mcx{2020} & \mcx{2001} & \mcx{2020} & \mcx{2001} & \mcx{2020} \\ \midrule
Afghanistan & 82.5 & 94.4 & 6.5  & 1.6  & 3.8  & 0.3  & 2.6  & 1.0  & 4.0  & 0.8  \\
Bangladesh  & 7.3  & 4.6  & 89.7 & 92.6 & 0.3  & 0.9  & 1.0  & 0.8  & 1.6  & 1.0  \\
Bhutan      & 48.5 & 39.8 & 21.1 & 6.5  & 10.7 & 45.2 & 3.1  & 0.9  & 16.2 & 7.5  \\
India       & 35.6 & 34.8 & 30.5 & 14.4 & 6.8  & 10.0 & 7.7  & 15.1 & 16.4 & 25.6 \\
Maldives    & 32.9 & 92.9 & 62.6 & 0.4  & 1.4  & 2.6  & 1.8  & 1.1  & 1.0  & 2.6  \\
Nepal       & 26.0 & 30.3 & 55.4 & 47.5 & 7.5  & 11.1 & 3.0  & 1.8  & 8.2  & 9.2  \\
Pakistan    & 14.9 & 25.0 & 75.7 & 63.5 & 1.5  & 1.5  & 4.1  & 2.9  & 3.6  & 6.9  \\
Sri Lanka   & 26.4 & 34.6 & 60.4 & 48.5 & 1.1  & 2.0  & 5.9  & 10.3 & 5.0  & 4.7 \\ \bottomrule
\end{tabularx}
\begin{tablenotes}[flushleft]
\setlength\labelsep{0pt}
\footnotesize
\item Source: United Nations Conference on Trade and Development \\
\end{tablenotes}
\end{threeparttable}
    \end{table}
\end{document}

In the case that you like to have the first row in column headers vertical centered, than you only need replace

   \renewcommand\tabularxcolumn[1]{b{#1}}

with

\renewcommand\tabularxcolumn[1]{m{#1}}

and you will het the following table:

enter image description here

You must log in to answer this question.

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