2

I am trying to rotate column title of a table. This is my TeX code.

\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}

However, I am getting error as

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.

Do you have any suggestions? Thank you in advance.

8
  • \rotatebox is defined in the graphicx (actually graphics) package that you have not loaded. add \usepackage{graphicx} Jan 10, 2017 at 10:59
  • I added \usepackage{graphicx} Jan 10, 2017 at 11:06
  • but now having three other errors (badbox warning disappeared) Jan 10, 2017 at 11:06
  • line30 ! Missing number, treated as zero.<to be read again> \advance \end{tabularx} Jan 10, 2017 at 11:07
  • line30 ! Illegal unit of measure(pt inserted). <to be read again> \advance \end{tabularx} Jan 10, 2017 at 11:08

2 Answers 2

4

A solution with the \rothead command, from makecell. I took the opportunity to add some vertical padding to your cells and removed some unnecessary code (such as the center environment, wich adds some unwanted vertical spacing around the table).

\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} 

enter image description here

2
  • Your MWE cannot work. Instead \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 \\ had to be \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 \\ .
    – Zarko
    Jan 10, 2017 at 14:31
  • @Zarko: Latexindent played a trick on me, it seems to have problems with tables sometimes. Thank for pointing it! It's fixed now.
    – Bernard
    Jan 10, 2017 at 14:52
2

It's possible to rotate the column titles using the \adjustbox macro from the adjustbox package. The syntax is \adjustbox{〈key=value, . . .〉}{〈content〉}. Also, for a better look, I have used the \thead and \makecell macros from the makecell package. Here is an MWE:

\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}

The output is as below:

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .