使用 tabularx 旋转列标题

使用 tabularx 旋转列标题

我正在尝试旋转表格的列标题。这是我的 TeX 代码。

\documentclass[11pt]{article} 

\usepackage{booktabs}

\usepackage{tabularx}

\newcommand{\rot}[2]{\rule{1em}{0pt}%

\makebox[0cm][c]{\rotatebox{15cm#1}{\ 15cm#2}}}

\begin{document}

\begin{table}[htbp]

\begin{center}

\footnotesize

\newcolumntype{Y}{>{\raggedleft\arraybackslash}X}

\begin{tabularx} {\textwidth} {@{} l Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y@{}} \\

\toprule

&\rot{60}{\textbf{Not college graduate}}&\rot{60}{\textbf{College graduate}}&\rot{60}{\textbf{Total}}&\rot{60}{\textbf{Not college graduate}}&\rot{60}
{\textbf{College graduate}}&\rot{60}{\textbf{Total}}&\rot{60}{\textbf{Not  college graduate}}&\rot{60}{\textbf{College graduate}}&\rot{60}{\textbf{Total}} \\

\midrule

\textbf{Location}&&&&&&&&& \\
Does not live in the south&5,041&1,540&6,581&76.6\%&23.4\%&100.0\%&58.4\%&58.9\%&58.5\% \\
Lives in the South&3,589&1,076&4,666&76.9\%&23.1\%&100.0\%&41.6\%&41.1\%&41.5\% \\
\textbf{Total}&8,631&2,616&11,247&76.7\%&23.3\%&100.0\%&100.0\%&100.0\%&100.0\% \\

\midrule

\textbf{Race}&&&&&&&&& \\
white&6,175&2,069&8,244&74.9\%&25.1\%&100.0\%&71.5\%&79.1\%&73.3\% \\
Black&2,370&494&2,864&82.7\%&17.3\%&100.0\%&27.5\%&18.9\%&25.5\% \\
Other&86&52&138&62.2\%&37.8\%&100.0\%&1.0\%&2.0\%&1.2\% \\
\textbf{Total}&8,631&2,616&11,247&76.7\%&23.3\%&100.0\%&100.0\%&100.0\%&100.0\% \\

\midrule

\textbf{N}&1,714&532&2,246&&&&&& \\

\bottomrule

\addlinespace[.75ex]

\end{tabularx}

\normalsize

\end{center}

\end{table}

\end{document}

但是我得到了错误

line29 ! Undefined control sequence. <argement> \rotatebox{15cm60}{\ 15cm\textbf {Not college graduate}} \end{tabularx}

line29 ! Undefined control sequence. <argement> \rotatebox{15cm60}{\ 15cm\textbf {College graduate}} \end{tabularx}

line29 ! Undefined control sequence. <argement> \rotatebox{15cm60}{\ 15cm\textbf {Total}} \end{tabularx}

and a lot of 
Badbox warning in same line(line29) as

Overfull \hbox (XXXXXXpt too wide0 in paragraph at lines 29--29.

你有什么建议吗?提前谢谢。

答案1

使用命令的解决方案\rothead,来自makecell。我借此机会向您的单元格添加了一些垂直填充,并删除了一些不必要的代码(例如环境center,它在表格周围添加了一些不必要的垂直间距)。

\documentclass[11pt]{article}
 \usepackage{booktabs}
\usepackage{rotating, graphicx}%
 \usepackage{tabularx, makecell}


\begin{document}

\begin{table}[!htbp]
  \setcellgapes{2pt}
  \makegapedcells
  \renewcommand{\cellrotangle}{60}
  \renewcommand\theadfont{\bfseries}%
  \renewcommand\theadset{\linespread{0.6}}
  \settowidth\rotheadsize{\theadfont graduate (\%)}
  \newcolumntype{Y}{>{\raggedleft\arraybackslash}X}
  \setlength\tabcolsep{2.8pt}
  \footnotesize\centering
  \begin{tabularx} {\textwidth} {@{} l *{9}{Y}@{}} \\
    \toprule
    \addlinespace[-6ex]
                                & \rothead{Not college graduate} & \rothead{College graduate} & \rothead{Total} & \rothead{Not college graduate (\%)} & \rothead{College graduate (\%)} & \rothead{Total (\%)} & \rothead{Not college graduate (\%)} & \rothead{College graduate (\%)} & \rothead{Total (\%)} \\[-1.5ex]%
    \midrule
    \textbf{Location} \\
    \makecell[lt]{Does not live\\ in the south}&5,041&1,540&6,581&76.6&23.4&100.0&58.4&58.9&58.5 \\
    \makecell[lt]{Lives in\\the South}&3,589&1,076&4,666&76.9&23.1&100.0&41.6&41.1&41.5 \\
    \textbf{Total} & 8,631 & 2,616 & 11,247 & 76.7 & 23.3 & 100.0 & 100.0 & 100.0 & 100.0 \\
    \midrule
    \textbf{Race} \\
    White & 6,175 & 2,069 & 8,244 & 74.9 & 25.1 & 100.0 & 71.5 & 79.1 & 73.3 \\
    Black & 2,370 & 494 & 2,864 & 82.7 & 17.3 & 100.0 & 27.5 & 18.9 & 25.5 \\
    Other & 86 & 52 & 138 & 62.2 & 37.8 & 100.0 & 1.0 & 2.0 & 1.2 \\
    \textbf{Total} & 8,631 & 2,616 & 11,247 & 76.7 & 23.3 & 100.0 & 100.0 & 100.0 & 100.0 \\
    \midrule
    \textbf{N} & 1,714 & 532 & 2,246 & & & & & & \\
    \bottomrule
  \end{tabularx}
\end{table}

\end{document} 

在此处输入图片描述

答案2

可以使用包\adjustbox中的宏来旋转列标题adjustbox。语法是\adjustbox{〈key=value, . . .〉}{〈content〉}。此外,为了获得更好的外观,我使用了包中的\thead和宏。这是一个 MWE:\makecellmakecell

\documentclass{book}

\usepackage{adjustbox}
\usepackage{tabularx}
\usepackage{booktabs}
\usepackage{makecell}

\begin{document}
    \begin{table}[htbp]
    \begin{center}
    \footnotesize
    \newcolumntype{Y}{>{\raggedleft\arraybackslash}X}
    
    \begin{tabularx} {\textwidth} {@{} l Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y@{}} \\
    
    \toprule
    
    &\adjustbox{angle=60}{\textbf{\thead{Not college\\graduate}}}&\adjustbox{angle=60}{\textbf{\thead{College\\graduate}}}&\adjustbox{angle=60}{\textbf{Total}}&\adjustbox{angle=60}{\textbf{\thead{Not college\\graduate}}}&\adjustbox{angle=60}
    {\textbf{\thead{College\\graduate}}}&\adjustbox{angle=60}{\textbf{Total}}&\adjustbox{angle=60}{\textbf{\thead{Not  college\\graduate}}}&\adjustbox{angle=60}{\textbf{\thead{College\\graduate}}}&\adjustbox{angle=60}{\textbf{Total}} \\
    
    \midrule
    
    \textbf{Location}&&&&&&&&& \\
    \makecell[l]{Does not live\\ in the south} &5,041&1,540&6,581&76.6\%&23.4\%&100.0\%&58.4\%&58.9\%&58.5\% \\
    \makecell[l]{Lives in\\ the South}&3,589&1,076&4,666&76.9\%&23.1\%&100.0\%&41.6\%&41.1\%&41.5\% \\
    \textbf{Total}&8,631&2,616&11,247&76.7\%&23.3\%&100.0\%&100.0\%&100.0\%&100.0\% \\
    
    \midrule
    
    \textbf{Race}&&&&&&&&& \\
    white&6,175&2,069&8,244&74.9\%&25.1\%&100.0\%&71.5\%&79.1\%&73.3\% \\
    Black&2,370&494&2,864&82.7\%&17.3\%&100.0\%&27.5\%&18.9\%&25.5\% \\
    Other&86&52&138&62.2\%&37.8\%&100.0\%&1.0\%&2.0\%&1.2\% \\
    \textbf{Total}&8,631&2,616&11,247&76.7\%&23.3\%&100.0\%&100.0\%&100.0\%&100.0\% \\
    
    \midrule
    
    \textbf{N}&1,714&532&2,246&&&&&& \\
    \bottomrule
    \addlinespace[.75ex]
    \end{tabularx}
    \normalsize
    \end{center}
    \end{table}
\end{document}

输出如下:

在此处输入图片描述

相关内容