![如何将具有不同文本长度的乳胶表的单元格内容居中?](https://linux22.com/image/359882/%E5%A6%82%E4%BD%95%E5%B0%86%E5%85%B7%E6%9C%89%E4%B8%8D%E5%90%8C%E6%96%87%E6%9C%AC%E9%95%BF%E5%BA%A6%E7%9A%84%E4%B9%B3%E8%83%B6%E8%A1%A8%E7%9A%84%E5%8D%95%E5%85%83%E6%A0%BC%E5%86%85%E5%AE%B9%E5%B1%85%E4%B8%AD%EF%BC%9F.png)
我正在使用数组包将文本垂直居中(见下面的代码)。问题是第一列和第二列中的文本与第一列不对齐。我该如何解决这个问题?
\usepackage{array}
\newcolumntype{P}[1]{>{\centering\arraybackslash}p{#1}}
\newcolumntype{M}[1]{>{\centering\arraybackslash}m{#1}}
\begin{table*}
\caption{caption goes here}
\label{tab:1} % Give a unique label
\begin{center}
\begin{tabular}{|M{1.5cm}|M{2cm}|M{7cm}|M{1.5cm}|M{2cm}|}
\hline\noalign{\smallskip}
\multicolumn{3}{c}{text} & \multicolumn{2}{c}{text text text}\\
\noalign{\smallskip}\hline\noalign{\smallskip}
text & text text& text text text text & text text & text text text\\
\noalign{\smallskip}\hline\noalign{\smallskip}
text & text & {text text text text text text text text text text text text. text text text text agricultural areas around text text text text} & text & text& text\\
\rule{0pt}{5ex}
text & text & {text text text text text text text text text text text text. text text text text agricultural areas around text text text text} & text & & text& text & text\\
\rule{0pt}{5ex}
text & text & {text text text text text text text text text text text text. text text text text agricultural areas around text text text text} & text & text\\
\rule{0pt}{5ex}
text & text & {text text text text text text text text text text text text. text text text text agricultural areas around text text text text} & text & text& text\\
\noalign{\smallskip}\hline
\end{tabular}
\end{center}
\end{table*}
答案1
罪魁祸首是那些\rule
毫无用处的命令。我还删除了那些\noalign{\smallskip}
位,它们唯一的作用就是断开规则。
\documentclass{article}
\usepackage{array}
\newcolumntype{M}[1]{>{\centering\arraybackslash}m{#1}}
\begin{document}
\begin{table*}
\caption{caption goes here}
\label{tab:1} % Give a unique label
\centering
\begin{tabular}{|M{1.5cm}|M{2cm}|M{7cm}|M{1.5cm}|M{2cm}|}
\hline
\multicolumn{3}{c}{text} & \multicolumn{2}{c}{text text text}\\
\hline
text & text text & text text text text & text text & text text text\\
\hline
text & text & text text text text text text text text text text text text. text text text text agricultural areas around text text text text & text & text \\
text & text & text text text text text text text text text text text text. text text text text agricultural areas around text text text text & text & text \\
text & text & text text text text text text text text text text text text. text text text text agricultural areas around text text text text & text & text\\
text & text & text text text text text text text text text text text text. text text text text agricultural areas around text text text text & text & text\\
\hline
\end{tabular}
\end{table*}
\end{document}
你可能想看到不同的实现:
\documentclass{article}
\usepackage{array,booktabs}
\newcolumntype{M}[1]{>{\centering\arraybackslash}m{#1}}
\begin{document}
\begin{table*}
\caption{caption goes here}
\label{tab:1} % Give a unique label
\centering
\begin{tabular}{M{1.5cm}M{2cm}M{7cm}M{1.5cm}M{2cm}}
\toprule
\multicolumn{3}{c}{text} & \multicolumn{2}{c}{text text text}\\
\cmidrule(lr){1-3} \cmidrule(lr){4-5}
text & text text & text text text text & text text & text text text\\
\midrule
text & text & text text text text text text text text text text text text. text text text text agricultural areas around text text text text & text & text \\
\addlinespace
text & text & text text text text text text text text text text text text. text text text text agricultural areas around text text text text & text & text \\
\addlinespace
text & text & text text text text text text text text text text text text. text text text text agricultural areas around text text text text & text & text\\
\addlinespace
text & text & text text text text text text text text text text text text. text text text text agricultural areas around text text text text & text & text\\
\bottomrule
\end{tabular}
\end{table*}
\end{document}