![tabularx 和 multirow:奇怪的行为](https://linux22.com/image/353611/tabularx%20%E5%92%8C%20multirow%EF%BC%9A%E5%A5%87%E6%80%AA%E7%9A%84%E8%A1%8C%E4%B8%BA.png)
平均能量损失
\documentclass[12pt]{article}
\usepackage[
a4paper,
left = 5mm,
right = 5mm,
landscape,
%showframe,
]
{geometry}
\usepackage{tabularx}
\usepackage{multirow}
% Thicker table lines for screenshot
\setlength{\arrayrulewidth}{1.5pt}
\setlength{\parindent}{0pt}
\usepackage[table]{xcolor}
\definecolor{myTableColor}{gray}{0.80}
% Nice sf font for screenshot
\usepackage[sfdefault]{cabin}
% https://tex.stackexchange.com/questions/163061
% https://tex.stackexchange.com/questions/89166
\newcolumntype{Y}{>{\centering\arraybackslash}X}
\begin{document}
\section*{Red Text Causes Strange Column}
\begin{tabularx}{\textwidth}{lc|l|Y|Y|Y|Y}
& & & \multicolumn{3}{c}{\multirow{2}{*}{\parbox{130mm}{\centering Text}}} & \\
% New Row
& & & \multicolumn{3}{l}{} & \\
% New Row
Text & Text & & & & & \\ \hline
% New Row
\rowcolor{myTableColor}
Text & Text & & & & \textcolor{red}{Problem Column} & \\ \hline
% New Row
Text Text Text Text \textcolor{red}{Text} & Text & Text Text Text Text Text \textcolor{red}{Text} & & & & \\ \hline
% New Row
Text & Text & & & & & \\ \hline
\end{tabularx}
\section*{Red Text Removed}
\begin{tabularx}{\textwidth}{lc|l|Y|Y|Y|Y}
& & & \multicolumn{3}{c}{\multirow{2}{*}{\parbox{130mm}{\centering Text}}} & \\
% New Row
& & & \multicolumn{3}{l}{} & \\
% New Row
Text & Text & & & & & \\ \hline
% New Row
\rowcolor{myTableColor}
Text & Text & & & & \textcolor{blue}{No Problem Column} & \\ \hline
% New Row
Text Text Text Text & Text & Text Text Text Text Text & & & & \\ \hline
% New Row
Text & Text & & & & & \\ \hline
\end{tabularx}
\end{document}
MWE 输出
问题
- 我有一张表,其中我使用
tabularx
和multirow
。 - 当我在 MWE 中添加红色文本时,其中一列发生了偏移
X
。 - 我不明白发生了什么。
更新
在评论中聊天之后,我认为这可能是更好的问题:
我可以使用自动
multirow
换行符multicolumn
而不必明确指定宽度(此处为 120 毫米)吗?
“真实”表
这是“真实”的表格。
答案1
您希望使用 Y 列作为跨度条目
\documentclass[12pt]{article}
\usepackage[
a4paper,
left = 5mm,
right = 5mm,
landscape,
%showframe,
]
{geometry}
\usepackage{tabularx}
\usepackage{multirow}
% Thicker table lines for screenshot
\setlength{\arrayrulewidth}{1.5pt}
\setlength{\parindent}{0pt}
\usepackage[table]{xcolor}
\definecolor{myTableColor}{gray}{0.80}
% Nice sf font for screenshot
\usepackage[sfdefault]{cabin}
% https://tex.stackexchange.com/questions/163061
% https://tex.stackexchange.com/questions/89166
\newcolumntype{Y}{>{\centering\arraybackslash}X}
\begin{document}
\section*{Red Text Causes Strange Column}
\begin{tabularx}{\textwidth}{lc|l|Y|Y|Y|Y}
& & &
\multicolumn{3}{>{\hsize=\dimexpr3\hsize+4\tabcolsep+2\arrayrulewidth\relax}Y}
{\multirow{2}{=}{\centering Text
text text text text text text text text text text
text text text text text text text text text text
text text text text text text text text text text
}} & \\
% New Row
& & & \multicolumn{3}{l}{} & \\
% New Row
Text & Text & & & & & \\ \hline
% New Row
\rowcolor{myTableColor}
Text & Text & & & & \textcolor{red}{Problem Column} & \\ \hline
% New Row
Text Text Text Text \textcolor{red}{Text} & Text & Text Text Text Text Text \textcolor{red}{Text} & & & & \\ \hline
% New Row
Text & Text & & & & & \\ \hline
\end{tabularx}
\section*{Red Text Removed}
\begin{tabularx}{\textwidth}{lc|l|Y|Y|Y|Y}
& & & \multicolumn{3}{c}{\multirow{2}{*}{\parbox{130mm}{\centering Text}}} & \\
% New Row
& & & \multicolumn{3}{l}{} & \\
% New Row
Text & Text & & & & & \\ \hline
% New Row
\rowcolor{myTableColor}
Text & Text & & & & \textcolor{blue}{No Problem Column} & \\ \hline
% New Row
Text Text Text Text & Text & Text Text Text Text Text & & & & \\ \hline
% New Row
Text & Text & & & & & \\ \hline
\end{tabularx}
\end{document}