我正在尝试创建一个表格,但标题的大小与表格的大小不匹配。有人能帮我吗?
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{booktabs}
\usepackage{tabularx}
\usepackage{placeins}
\newcolumntype{Y}{>{\centering\arraybackslash}X}
\usepackage{graphicx,booktabs,mhchem,makecell,array}
\usepackage{comment}
\usepackage{amsmath}
\usepackage{multirow}
\begin{document}
\begin{table}[H]
\centering
\begin{tabularx}{\textwidth}{@{}YYYYYYY@{}}
\toprule
\multirow{2}{*}{\textbf{\makecell{Mobility\\group}}}& \multirow{2}{*}{\textbf{\makecell{Mobility\\group long}}} & \multirow{2}{*}{\textbf{\makecell{CHL1\\ no sens.}}}& \multicolumn{3}{c}{\textbf{\makecell{Cars}}}&
%\multirow{2}{*}{\textbf{\makecell{shares\\added}}} &
\multirow{2}{*}{\textbf{\makecell{CHL1\\with sens.}}} \\
\cmidrule{4-6}
& & & \textbf{0} & \textbf{1} & \textbf{2} & \\
\midrule
1 & R1HH1IC1 & 1.94 & 1.64 & 0.27 & 0.03 3 & 0.59 \\
2 & R1HH1IC2 & 2.94 & 1.56 & 1.03 & 0.34 & 3.10 \\
3 & R1HH1IC3 & 0.59 & 0.06 & 0.17 & 0.37 & 1.64 \\
4 & R1HH2IC1 & 2.86 & 2.41 & 0.40 & 0.04 & 0.88 \\
5 & R1HH2IC2 & 4.33 & 2.31 & 1.52 & 0.50 & 4.57 \\
6 & R1HH2IC3 & 0.87 & 0.08 & 0.25 & 0.54 & 2.42 \\
7 & R1HH3IC1 & 7.79 & 6.58 & 1.09 & 0.11 & 2.39 \\
8 & R1HH3IC2 & 11.80 & 6.29 & 4.15 & 1.36 & 12.47 \\
9 & R1HH3IC3 & 2.38 & 0.23 & 0.67 & 1.48 & 6.59 \\
\bottomrule
\end{tabularx}
\caption{Caption}
\label{tab:nn}
\end{table}
\end{document}
答案1
将三列“cars”从 切换Y
到c
可以节省大量空间。而且,由于我们继续Y
对其余四列使用 列类型,因此不要使用带有明确换行符的\multirow
和\makecell
指令来让生活变得毫无必要地乏味;只需让 LaTeX 借助适当定义的\mytab
宏为您处理这项工作;请参阅下文了解其定义方式。
\documentclass{article}
\usepackage{tabularx,booktabs,amsmath}
\newcolumntype{Y}{>{\centering\arraybackslash}X}
\newcolumntype{P}{>{\centering\arraybackslash}p{\hsize}}
\newcommand\mytab[1]{\smash[b]{%
\begin{tabular}[t]{@{} P @{}} #1 \end{tabular}}}
\begin{document}
\begin{table}[ht]
\begin{tabularx}{\textwidth}{@{} YYYcccY @{}}
\toprule
\bfseries \mytab{Mobility group} &
\bfseries \mytab{Mobility group long} &
\bfseries \mytab{CHL1 no~sens.} &
\multicolumn{3}{c}{\bfseries Cars} &
\bfseries \mytab{CHL1 with sens.} \\
\cmidrule(lr){4-6}
& & & \textbf{0} & \textbf{1} & \textbf{2} \\
\midrule
1 & R1HH1IC1 & 1.94 & 1.64 & 0.27 & 0.03 & 0.59 \\
2 & R1HH1IC2 & 2.94 & 1.56 & 1.03 & 0.34 & 3.10 \\
3 & R1HH1IC3 & 0.59 & 0.06 & 0.17 & 0.37 & 1.64 \\
4 & R1HH2IC1 & 2.86 & 2.41 & 0.40 & 0.04 & 0.88 \\
5 & R1HH2IC2 & 4.33 & 2.31 & 1.52 & 0.50 & 4.57 \\
6 & R1HH2IC3 & 0.87 & 0.08 & 0.25 & 0.54 & 2.42 \\
7 & R1HH3IC1 & 7.79 & 6.58 & 1.09 & 0.11 & 2.39 \\
8 & R1HH3IC2 & 11.80 & 6.29 & 4.15 & 1.36 &12.47 \\
9 & R1HH3IC3 & 2.38 & 0.23 & 0.67 & 1.48 & 6.59 \\
\bottomrule
\end{tabularx}
\caption{Caption} \label{tab:nn}
\end{table}
\end{document}