如何在表格单元格中换行?

如何在表格单元格中换行?

此代码:

%%%%%%%%%%%%%%%%%%Table: Valence x valence crosstab 
\begin{table*}[t]
    \centering
        \begin{tabular}{c c c c c c c}
            \toprule
            \midrule
                & & \multicolumn{5}{c}{Predicted valence }\\ 
                \cmidrule{3-7}
                && Negative  & Little negative & Neutral & Little positive & Positive  \\ 
                \cmidrule{3-7}
                \multicolumn{1}{c}{\multirow{5}{*}{\begin{sideways}Actual valence\end{sideways}}}   &
                \multicolumn{1}{l}{Negative}& 59.0 & 18.0 & 17.3 & 5.8 &  \\
                \multicolumn{1}{c}{}    &
                \multicolumn{1}{l}{Little negative}& 0.9 & 66.6 & 16.7 & 10.7 & 5.1  \\
                \multicolumn{1}{c}{}    &
                \multicolumn{1}{l}{Neutral} &1.4 & 16.6 & 70.8 & 8.0 & 3.1  \\
                \multicolumn{1}{c}{}    &   
                \multicolumn{1}{l}{Little positive} &0.3 & 11.2 & 20.0 & 68.0 & .5  \\
                \multicolumn{1}{c}{}    &
                \multicolumn{1}{l}{Positive} & 4.9 & 7.8 & 17.5 & 69.9  \\
            \midrule
            \bottomrule
        \end{tabular}
        \centering
        \caption{Classification results for the valence dimension.}
        \label{tab:sam_count}
\end{table*}
%%%%%%%%%%%%%%%%%   

产生这个:

截图

我希望垂直文本“实际价”中间有一个换行符。

我该如何实现这个目标?

答案1

一种选择是使用\parbox

\documentclass{article}
\usepackage{booktabs}
\usepackage{multirow}
\usepackage{rotating}

\begin{document}

\begin{table*}[t]
    \centering
        \begin{tabular}{c c c c c c c}
            \toprule
            \midrule
                & & \multicolumn{5}{c}{Predicted valence }\\ \cmidrule{3-7}
                && Negative  & Little negative & Neutral & Little positive & Positive  \\ \cmidrule{3-7}
                \multicolumn{1}{c}{\multirow{5}{*}{\begin{sideways}\parbox{2cm}{\centering Actual \\ valence}\end{sideways}}}   &
                \multicolumn{1}{l}{Negative}& 59.0 & 18.0 & 17.3 & 5.8 &  \\
                \multicolumn{1}{c}{}    &
                \multicolumn{1}{l}{Little negative}& 0.9 & 66.6 & 16.7 & 10.7 & 5.1  \\
                \multicolumn{1}{c}{}    &
                \multicolumn{1}{l}{Neutral} &1.4 & 16.6 & 70.8 & 8.0 & 3.1  \\
                \multicolumn{1}{c}{}    &   
                \multicolumn{1}{l}{Little positive} &0.3 & 11.2 & 20.0 & 68.0 & .5  \\
                \multicolumn{1}{c}{}    &
                \multicolumn{1}{l}{Positive} & 4.9 & 7.8 & 17.5 & 69.9  \\
            \midrule
            \bottomrule
        \end{tabular}
        \centering
        \caption{Classification results for the valence dimension.}
        \label{tab:sam_count}
\end{table*}

\end{document}

在此处输入图片描述

答案2

正如@GonzaloMedina 所建议的,使用的另一个选择parbox是放弃旋转\multirow并使用修剪的 cmidrules 来获得更“传统”的表头。

\documentclass[a4paper]{article}

\usepackage{rotating,booktabs,multirow}

\begin{document}
\begin{table*}[t]
\centering
\begin{tabular}{c c c c c c}
    \toprule
    \midrule
    \multirow{2}[4]{*}{Actual Valence} & \multicolumn{5}{c}{Predicted valence }\\ 
    \cmidrule(rl){2-6}
    & Negative  & Little negative & Neutral & Little positive & Positive  \\ 
    \cmidrule(r){1-1}\cmidrule(l){2-6}
    \multicolumn{1}{l}{Negative}& 59.0 & 18.0 & 17.3 & 5.8 &  \\
    \multicolumn{1}{l}{Little negative}& 0.9 & 66.6 & 16.7 & 10.7 & 5.1  \\
    \multicolumn{1}{l}{Neutral} &1.4 & 16.6 & 70.8 & 8.0 & 3.1  \\
    \multicolumn{1}{l}{Little positive} &0.3 & 11.2 & 20.0 & 68.0 & .5  \\
    \multicolumn{1}{l}{Positive} & 4.9 & 7.8 & 17.5 & 69.9  \\
    \midrule
    \bottomrule
\end{tabular}
        %\centering
\caption{Without rotation, with trimmed cmidrules}
\label{tab:sam_count2}
\end{table*}
\end{document}

在此处输入图片描述

答案3

强迫读者将脖子伸长 90 度才能读到某些标题行几乎总是一个低劣的想法。在目前的表格中,我看不出有什么理由给读者带来这种不便,尤其是第一列的标题(“实际价数”)几乎与该列中最长的条目(“小负”)一样宽。

我宁愿多花点精力把所有数字的小数点对齐,因为这样倾向于促进对表格内容的理解。在下面修改后的 MWE 中,这是使用siunitx包及其S列类型实现的。

在此处输入图片描述

\documentclass{article}
\usepackage{booktabs,siunitx}
\sisetup{table-format=2.1}
\begin{document}

\begin{table}[t]
\setlength{\tabcolsep}{5pt}
\centering
\begin{tabular}{@{} l SSSSS @{}} % @{} serves to suppress white space at ends of table
\toprule
Actual valence   &  \multicolumn{5}{c @{}}{Predicted valence }\\ 
\cmidrule(l){2-6}
    & {Negative} & {Little negative} & {Neutral} & {Little positive} & {Positive}  \\
\midrule
Negative        & 59.0 & 18.0 & 17.3 &  5.8 &  \\
Little negative &  0.9 & 66.6 & 16.7 & 10.7 & 5.1  \\
Neutral         &  1.4 & 16.6 & 70.8 &  8.0 & 3.1  \\
Little positive &  0.3 & 11.2 & 20.0 & 68.0 &  .5  \\
Positive        &  4.9 &  7.8 & 17.5 & 69.9 & \\
\bottomrule
\end{tabular}
\caption{Classification results for the valence dimension.}
\label{tab:sam_count}
\end{table}
\end{document}

相关内容