我同时使用了 multirow 和 tabularx,只要表格大小正确,所有行和列都绘制得很好,它就运行良好。然而,文本断行功能无法正常工作。图片说明了一切。
我究竟做错了什么?
提前感谢任何提示或暗示。
(注意:我确实看过有关 tabularx 和 multirow 组合的其他问题,但没有找到针对我的问题的答案)
\documentclass[]{scrbook}
\usepackage[a4paper]{geometry}
\usepackage{graphicx}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{ragged2e}
\usepackage{tabularx, multirow, booktabs, makecell}
\usepackage[flushleft]{threeparttable}
\begin{document}
\begin{table}[]
\centering
\begin{tabularx}{\textwidth}{|l|l|l|X|}
\hline
&Name&\multicolumn{2}{l|}{Details}\\ \hline
\multirow{6}{*}{\rotatebox[origin=c]{90}{Attributes}} &
info{[}'name'{]}&\multicolumn{2}{l|}{"NAME"(string)}\\ \cline{2-4}
& 1 & \multicolumn{2}{X|}{"feature\_extraction" (string)} \\ \cline{2-4}
& 2 & \multicolumn{2}{X|}{0 (integer)}
\\ \cline{2-4}
& 3 & \multicolumn{2}{X|}{long long long long long long long long text that should break and it does break but it breaks at the wrong position. Why does it break in this position. What can I do to make it break at the right position?}
\\ \cline{2-4}
& 4 & \multicolumn{2}{l|}{0 (integer)}
\\ \cline{2-4}
& 5 & \multicolumn{2}{l|}{0}
\\ \hline
\multirow{6}{*}{\rotatebox[]{90}{Function}} & \multirow{3}{*}{transform} & Input & X (\textit{numpy.array}) \\ \cline{3-4}
& & Output & Xt(\textit{numpy.array}) \\ \cline{3-4}
& & Explanation & long long long long long long long long text that should break and does break wonderfully. Everything should work out as fine as it does here. \\ \hline
\end{tabularx}
\caption{Details of the feature extraction class}
\label{tab:feature_extraction}
\end{table}
\end{document}
答案1
您需要将列类型重新定义为这样的类型:
\begin{tabularx}{\textwidth}{|l|l|
>{\hsize=0.2\hsize}X|
>{\hsize=0.8\hsize}X|}
(必须提前知道第三列的宽度)并将 \multicolumn
单元格宽度重新定义为:
\multicolumn{2}{>{\hsize=\dimexpr\hsize+2\tabcolsep+\arrayrulewidth\relax}X|}
现在考虑第三和第四列的宽度(它们的宽度之和为0.2\hsize + 0.8\hsize=\hsize
)以及合并单元格之间的和\tabcolsep
。\arrayrulewidth
完整的 mwe:
\documentclass[]{scrbook}
\usepackage[a4paper]{geometry}
\usepackage{graphicx}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{ragged2e}
\usepackage{tabularx, multirow, booktabs, makecell}
\usepackage[flushleft]{threeparttable}
\begin{document}
\begin{table}[]
\centering
\begin{tabularx}{\textwidth}{|l|l|
>{\hsize=0.2\hsize}X|
>{\hsize=0.8\hsize}X|}
\hline
&Name&\multicolumn{2}{l|}{Details}\\ \hline
\multirow{6}{*}{\rotatebox[origin=c]{90}{Attributes}} &
info{[}'name'{]}&\multicolumn{2}{l|}{"NAME"(string)}\\ \cline{2-4}
& 1 & \multicolumn{2}{X|}{"feature\_extraction" (string)} \\ \cline{2-4}
& 2 & \multicolumn{2}{X|}{0 (integer)}
\\ \cline{2-4}
& 3 & \multicolumn{2}{>{\hsize=\dimexpr\hsize+2\tabcolsep+\arrayrulewidth\relax}X|}
{long long long long long long long long text that should break and it does break but it breaks at the wrong position. Why does it break in this position. What can I do to make it break at the right position?}
\\ \cline{2-4}
& 4 & \multicolumn{2}{l|}{0 (integer)}
\\ \cline{2-4}
& 5 & \multicolumn{2}{l|}{0}
\\ \hline
\multirow{6}{*}{\rotatebox[]{90}{Function}} & \multirow{3}{*}{transform} & Input & X (\textit{numpy.array}) \\ \cline{3-4}
& & Output & Xt(\textit{numpy.array}) \\ \cline{3-4}
& & Explanation & long long long long long long long long text that should break and does break wonderfully. Everything should work out as fine as it does here. \\ \hline
\end{tabularx}
\caption{Details of the feature extraction class}
\label{tab:feature_extraction}
\end{table}
\end{document}