在下面的示例表中,我希望第一行的列标题分别跨越三个单元格。但它们只包含在一个单元格中。如果有人能帮助解决这个问题,我将不胜感激。
\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
变化:
- 的定义
\mcx
。它只有一个参数(用于单元格内容)和三个单元格的宽度。宽度计算如下\dimexpr ...
- 在第一行(带有
\mcx
单元格)中删除多余的“与”符号(中间\mcx
只能有一个) - 离题:我将单词“states”移到第二行(但如果不喜欢这样,你可以简单地返回到第一行)
\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}
编辑:
在定义多列单元格\multicolumn
时tabularx
,考虑列类型X
时tabularx
仅考虑单元格内容的最大宽度,而不考虑单元格的宽度。换句话说
\multicolumn{2}{>{\hsize=2\hsize}X}{...}
不考虑tabcolsep
这两列中的所有空格,因此multicolumn
单元格较窄,只有四个空格tabcolsep
。因此,在“\newcommand\mcx{...}”的定义中(通过更简单的排版表)在上面定义姆韦作为
\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}}