如何在 LaTeX 中旋转带有标题的表格?

如何在 LaTeX 中旋转带有标题的表格?

对于下表,标题不随表格旋转。有人能在这方面帮助我吗?

\usepackage{array, booktabs, tabularx} 
\usepackage{setspace} 
\usepackage{multirow} 
\usepackage{rotating}
\begin{document}
\begin{table}
  \label{table:NSFAccuDivBG}
  \caption{Comparative Performance of Subforests from Bagging}
  \newcolumntype{R}{>{\raggedright \arraybackslash} X}
  \newcolumntype{S}{>{\centering \arraybackslash} X}
  \newcolumntype{T}{>{\raggedleft \arraybackslash} X}
  \begin{sideways}
  \begin{tabularx}{\linewidth} {>{\setlength\hsize{.12\hsize}}R >{\setlength\hsize{.11\hsize}}T >{\setlength\hsize{.11\hsize}}T >{\setlength\hsize{.11\hsize}}T >{\setlength\hsize{.11\hsize}}T >{\setlength\hsize{.11\hsize}}T >{\setlength\hsize{.11\hsize}}T >{\setlength\hsize{.11\hsize}}T >{\setlength\hsize{.11\hsize}}T}  % centered columns (4 columns)
  \toprule
  \multicolumn{4}{r}{EA} & \multicolumn{4}{r}{ES} \\
  \cmidrule(l){2-5} \cmidrule(l){6-9}
  DS & $ Sub_{A} $ & $ Sub_{D} $ & $ Sub_{A \cap D} $ & $ Sub_{All} $ & $ Sub_{A} $ & $ Sub_{D} $ & $ Sub_{A \cap D} $ & $ Sub_{All} $ \tabularnewline
  \midrule
  BS & 79.24 & 84.33 & 77.75 & 80.50 & 51.90 & 49.20 & 9.10 & 100.00 \tabularnewline
  BC & 77.83 & 77.30 & 75.81 & 77.92 & 55.80 & 46.30 & 12.30 & 100.00 \tabularnewline
  CE & 90.96 & 83.29 & 87.44 & 91.19 & 84.90 & 29.70 & 14.70 & 100.00 \tabularnewline
  CHES & 94.84 & 83.97 & 82.16 & 95.22 & 72.40 & 42.90 & 15.30 & 100.00 \tabularnewline
  CA & 86.68 & 81.16 & 83.76 & 86.07 & 76.80 & 40.90 & 18.50 & 100.00 \tabularnewline
  DER & 87.82 & 62.03 & 73.37 & 86.96 & 64.40 & 37.20 & 4.50 & 100.00 \tabularnewline
  EC & 84.62 & 85.43 & 85.43 & 84.97 & 60.00 & 38.20 & 13.60 & 100.00 \tabularnewline
  GI & 74.12 & 69.83 & 68.95 & 74.12 & 59.90 & 40.80 & 15.10 & 100.00 \tabularnewline
  HR & 72.62 & 73.39 & 63.39 & 69.54 & 49.30 & 49.30 & 13.10 & 100.00 \tabularnewline
  HEP & 86.25 & 85.00 & 81.25 & 86.25 & 71.70 & 43.40 & 17.80 & 100.00 \tabularnewline
  IS & 97.19 & 97.23 & 95.80 & 97.14 & 70.60 & 27.60 & 15.90 & 100.00 \tabularnewline
  ION & 94.02 & 93.45 & 93.17 & 93.73 & 52.50 & 47.50 & 12.90 & 100.00 \tabularnewline
  IRS & 95.33 & 93.33 & 84.00 & 96.00 & 54.20 & 26.90 & 9.50 & 100.00 \tabularnewline
  LM & 74.72 & 75.00 & 51.11 & 76.11 & 50.20 & 49.50 & 2.40 & 100.00 \tabularnewline
  LD & 68.40 & 68.36 & 66.63 & 71.48 & 62.30 & 42.70 & 20.20 & 100.00 \tabularnewline
  PID & 76.22 & 75.83 & 75.38 & 75.95 & 60.20 & 41.90 & 22.50 & 100.00 \tabularnewline
  SON & 80.86 & 81.07 & 27.00 & 83.07 & 52.10 & 48.20 & 1.30 & 100.00 \tabularnewline
  SH & 82.96 & 82.97 & 80.37 & 82.96 & 51.40 & 47.00 & 10.70 & 100.00 \tabularnewline
  SV & 74.27 & 74.02 & 73.05 & 74.14 & 59.70 & 42.90 & 13.30 & 100.00 \tabularnewline
  TN & 95.04 & 95.99 & 93.90 & 94.56 & 60.50 & 38.70 & 8.30 & 100.00 \tabularnewline
  \midrule
  \textbf{Avg} & 83.70 & 81.15 & 75.99 & \textbf{83.89} \cellcolor[gray] {.8} & 61.04 & 41.54 & 12.55 & 100.00 \tabularnewline
  \bottomrule
  \end{tabularx}
  \end{sideways}
 \end{table}

答案1

嗯,您的代码中有几个错误。您不需要table,而应该使用sidewaystable\label必须遵循\caption,否则您对表格的引用将不是工作!

请检查以下 MWE(主要变化以 标记% <=========):

\documentclass{scrartcl}

\usepackage{booktabs}
\usepackage[table,xcdraw]{xcolor}
\usepackage{multirow}
\usepackage{rotating,tabularx}

\begin{document}

%\begin{table}

  \begin{sidewaystable}% <===============================================
  \caption{Comparative Performance of Subforests from Bagging}\label{table:NSFAccuDivBG}% <===============================================
  \newcolumntype{R}{>{\raggedright \arraybackslash} X}
  \newcolumntype{S}{>{\centering \arraybackslash} X}
  \newcolumntype{T}{>{\raggedleft \arraybackslash} X}
  \begin{tabularx}{\linewidth} {>{\setlength\hsize{.12\hsize}}R >{\setlength\hsize{.11\hsize}}T >{\setlength\hsize{.11\hsize}}T >{\setlength\hsize{.11\hsize}}T >{\setlength\hsize{.11\hsize}}T >{\setlength\hsize{.11\hsize}}T >{\setlength\hsize{.11\hsize}}T >{\setlength\hsize{.11\hsize}}T >{\setlength\hsize{.11\hsize}}T}  % centered columns (4 columns)
  \toprule
  \multicolumn{4}{r}{EA} & \multicolumn{4}{r}{ES} \\
  \cmidrule(l){2-5} \cmidrule(l){6-9}
  DS & $ Sub_{A} $ & $ Sub_{D} $ & $ Sub_{A \cap D} $ & $ Sub_{All} $ & $ Sub_{A} $ & $ Sub_{D} $ & $ Sub_{A \cap D} $ & $ Sub_{All} $ \tabularnewline
  \midrule
  BS & 79.24 & 84.33 & 77.75 & 80.50 & 51.90 & 49.20 & 9.10 & 100.00 \tabularnewline
  BC & 77.83 & 77.30 & 75.81 & 77.92 & 55.80 & 46.30 & 12.30 & 100.00 \tabularnewline
  CE & 90.96 & 83.29 & 87.44 & 91.19 & 84.90 & 29.70 & 14.70 & 100.00 \tabularnewline
  CHES & 94.84 & 83.97 & 82.16 & 95.22 & 72.40 & 42.90 & 15.30 & 100.00 \tabularnewline
  CA & 86.68 & 81.16 & 83.76 & 86.07 & 76.80 & 40.90 & 18.50 & 100.00 \tabularnewline
  DER & 87.82 & 62.03 & 73.37 & 86.96 & 64.40 & 37.20 & 4.50 & 100.00 \tabularnewline
  EC & 84.62 & 85.43 & 85.43 & 84.97 & 60.00 & 38.20 & 13.60 & 100.00 \tabularnewline
  GI & 74.12 & 69.83 & 68.95 & 74.12 & 59.90 & 40.80 & 15.10 & 100.00 \tabularnewline
  HR & 72.62 & 73.39 & 63.39 & 69.54 & 49.30 & 49.30 & 13.10 & 100.00 \tabularnewline
  HEP & 86.25 & 85.00 & 81.25 & 86.25 & 71.70 & 43.40 & 17.80 & 100.00 \tabularnewline
  IS & 97.19 & 97.23 & 95.80 & 97.14 & 70.60 & 27.60 & 15.90 & 100.00 \tabularnewline
  ION & 94.02 & 93.45 & 93.17 & 93.73 & 52.50 & 47.50 & 12.90 & 100.00 \tabularnewline
  IRS & 95.33 & 93.33 & 84.00 & 96.00 & 54.20 & 26.90 & 9.50 & 100.00 \tabularnewline
  LM & 74.72 & 75.00 & 51.11 & 76.11 & 50.20 & 49.50 & 2.40 & 100.00 \tabularnewline
  LD & 68.40 & 68.36 & 66.63 & 71.48 & 62.30 & 42.70 & 20.20 & 100.00 \tabularnewline
  PID & 76.22 & 75.83 & 75.38 & 75.95 & 60.20 & 41.90 & 22.50 & 100.00 \tabularnewline
  SON & 80.86 & 81.07 & 27.00 & 83.07 & 52.10 & 48.20 & 1.30 & 100.00 \tabularnewline
  SH & 82.96 & 82.97 & 80.37 & 82.96 & 51.40 & 47.00 & 10.70 & 100.00 \tabularnewline
  SV & 74.27 & 74.02 & 73.05 & 74.14 & 59.70 & 42.90 & 13.30 & 100.00 \tabularnewline
  TN & 95.04 & 95.99 & 93.90 & 94.56 & 60.50 & 38.70 & 8.30 & 100.00 \tabularnewline
  \midrule
  \textbf{Avg} & 83.70 & 81.15 & 75.99 & \textbf{83.89} \cellcolor[gray] {.8} & 61.04 & 41.54 & 12.55 & 100.00 \tabularnewline
  \bottomrule
  \end{tabularx}
  \end{sidewaystable}

\end{document}

结果:

在此处输入图片描述

答案2

sidewaystable除了使用专用环境(例如由包提供)外rotating,您还应考虑(a)更正标题“EA”和“ES”的位置以及(b)大幅简化表结构。据我所知,T对于所有八个数据列,使用列类型(无需调整宽度)就足够了。

在此处输入图片描述

\documentclass{scrartcl}

\usepackage{booktabs}
\usepackage[table,xcdraw]{xcolor}
\usepackage{rotating,tabularx,siunitx}
\newcolumntype{T}{>{\raggedleft\arraybackslash} X}
\newcommand\Sub{{\textit{Sub}}}
\begin{document}

  \begin{sidewaystable}
  \caption{Comparative Performance of Subforests from Bagging}
  \label{table:NSFAccuDivBG} 
  \begin{tabularx}{\linewidth} { l *{8}{T} }  
  \toprule
  DS & \multicolumn{4}{c}{EA} & \multicolumn{4}{c}{ES} \\
  \cmidrule(lr){2-5} \cmidrule(l){6-9}
     & $\Sub_{A}$ & $\Sub_{D}$ & $\Sub_{A \cap D}$ & $\Sub_{\textit{All}}$ 
     & $\Sub_{A}$ & $\Sub_{D}$ & $\Sub_{A \cap D}$ & $\Sub_{\textit{All}}$ \\
  \midrule
  BS & 79.24 & 84.33 & 77.75 & 80.50 & 51.90 & 49.20 & 9.10 & 100.00 \\
  BC & 77.83 & 77.30 & 75.81 & 77.92 & 55.80 & 46.30 & 12.30 & 100.00 \\
  CE & 90.96 & 83.29 & 87.44 & 91.19 & 84.90 & 29.70 & 14.70 & 100.00 \\
  CHES & 94.84 & 83.97 & 82.16 & 95.22 & 72.40 & 42.90 & 15.30 & 100.00 \\
  CA & 86.68 & 81.16 & 83.76 & 86.07 & 76.80 & 40.90 & 18.50 & 100.00 \\
  DER & 87.82 & 62.03 & 73.37 & 86.96 & 64.40 & 37.20 & 4.50 & 100.00 \\
  EC & 84.62 & 85.43 & 85.43 & 84.97 & 60.00 & 38.20 & 13.60 & 100.00 \\
  GI & 74.12 & 69.83 & 68.95 & 74.12 & 59.90 & 40.80 & 15.10 & 100.00 \\
  HR & 72.62 & 73.39 & 63.39 & 69.54 & 49.30 & 49.30 & 13.10 & 100.00 \\
  HEP & 86.25 & 85.00 & 81.25 & 86.25 & 71.70 & 43.40 & 17.80 & 100.00 \\
  IS & 97.19 & 97.23 & 95.80 & 97.14 & 70.60 & 27.60 & 15.90 & 100.00 \\
  ION & 94.02 & 93.45 & 93.17 & 93.73 & 52.50 & 47.50 & 12.90 & 100.00 \\
  IRS & 95.33 & 93.33 & 84.00 & 96.00 & 54.20 & 26.90 & 9.50 & 100.00 \\
  LM & 74.72 & 75.00 & 51.11 & 76.11 & 50.20 & 49.50 & 2.40 & 100.00 \\
  LD & 68.40 & 68.36 & 66.63 & 71.48 & 62.30 & 42.70 & 20.20 & 100.00 \\
  PID & 76.22 & 75.83 & 75.38 & 75.95 & 60.20 & 41.90 & 22.50 & 100.00 \\
  SON & 80.86 & 81.07 & 27.00 & 83.07 & 52.10 & 48.20 & 1.30 & 100.00 \\
  SH & 82.96 & 82.97 & 80.37 & 82.96 & 51.40 & 47.00 & 10.70 & 100.00 \\
  SV & 74.27 & 74.02 & 73.05 & 74.14 & 59.70 & 42.90 & 13.30 & 100.00 \\
  TN & 95.04 & 95.99 & 93.90 & 94.56 & 60.50 & 38.70 & 8.30 & 100.00 \\
  \midrule
  \textbf{Avg} & 83.70 & 81.15 & 75.99 & \cellcolor[gray]{.8} \textbf{83.89} 
               & 61.04 & 41.54 & 12.55 & 100.00 \\
  \bottomrule
  \end{tabularx}
  \end{sidewaystable}

\end{document}

答案3

解决方案是将表格和标题放在里面minipage,如下所示。

\documentclass{article}
\usepackage{tabularx}
\usepackage{booktabs}
\usepackage{colortbl}
\usepackage{rotating}
\begin{document}
\begin{table}
  \newcolumntype{R}{>{\raggedright \arraybackslash} X}
  \newcolumntype{S}{>{\centering \arraybackslash} X}
  \newcolumntype{T}{>{\raggedleft \arraybackslash} X}
  \begin{sideways}
  \begin{minipage}{\textheight} % here
  \caption{Comparative Performance of Subforests from Bagging}
  \begin{tabularx}{\linewidth} {
    >{\setlength\hsize{.12\hsize}}R
    >{\setlength\hsize{.11\hsize}}T
    % ... rest of column definitions ...
    >{\setlength\hsize{.11\hsize}}T
  }  % centered columns (4 columns)
  \toprule
  \multicolumn{4}{r}{EA} & \multicolumn{4}{r}{ES} \\
  \cmidrule(l){2-5} \cmidrule(l){6-9}
  DS & $ Sub_{A} $ & ... & $ Sub_{All} $ \tabularnewline
  \midrule
  BS & 79.24 & ... & 100.00 \tabularnewline
  BC & 77.83 & ... & 100.00 
  % ... rest of table ...
  SV & 74.27 & ... & 100.00 \tabularnewline
  TN & 95.04 & ... & 100.00 \tabularnewline
  \midrule
  \textbf{Avg} & 83.70 & ... & 100.00 \tabularnewline
  \bottomrule
  \end{tabularx}
  \end{minipage} % and here
  \end{sideways}
 \end{table}
\end{document}

也可以看看:Wikibooks LaTeX,旋转

相关内容