长表格标题不换行

长表格标题不换行

表格标题很长,同时我们需要每个标题代表两年:这导致每个标题下有两列。在这种情况下,标题文本不会换行。请指导!

以下是代码:

\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

像这样?

在此处输入图片描述

您的列标题没有换行,因为使用multicolumn{2}{c}{...}不允许将包含的文本分成更多行。为此,应使用列类型或p{...}X或其派生类型),您需要相应地确定其宽度。例如,如以下建议的解决方案中所做的那样。

使用包装siunitx并使用

  • 为列标题定义了新的列类型Y,其宽度为两列,
  • 定义新的命令\newcommand\mcx[1]{\multicolumn{1}{C}{#1}}来决定列宽,并且
  • Ssiunitx包中定义的用于将数字的小数点对齐的列类型:
\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}

如果您希望将列标题的第一行垂直居中,那么您只需要替换

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

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

您将看到下表:

在此处输入图片描述

相关内容