# Rotated column titles in tabular

I'd like to create a chart where the column titles are rotated, as per this toy example:

I used the rotating package to generate the above. It has certain shortcomings: the rotate command doesn't leave space for the titles, so I have to manually space things to prevent overlaps. The turn command, by contrast, creates too much space, where each column is the width of the entire rotated column title.

There is also no easy way to have long titles be split into multiple lines.

Is there a package for creating tables of this sort, or at least one that would relieve some of the manual placing and spacing that goes into creating such a table?

This question is more to make sure I don't miss any easy way of doing what I want than seeking help on manually doing it, but the code for above is included anyways:

\begin{tabular}{r|ccc}
&
\begin{rotate}{60} Property 1 \end{rotate} &
\begin{rotate}{60} Property 2 \end{rotate} &
\begin{rotate}{60} Property 3 \end{rotate} \\ \hline
System 1        &       &       &  X    \\
System 2        & X     & X     &  X    \\
System 3        & X &   &  X    \\ \hline
\end{tabular}

-
I actually did look at that question first but the answer only works with exactly sideways text (they argue it is otherwise confusing; I disagree and would like rotated text as above). –  PulpSpy Oct 25 '11 at 16:16
Why do you need the rotation? First, introduce one more column heading at the top of the table for the last three columns with the word Properties in it. Next use 1, 2, and 3 for the last three column headings. Your table will be much simpler if you do this. –  Marc van Dongen Feb 16 '13 at 7:43
@MarcvanDongen Thanks but the column headers in the example were just placeholder names; the real table I wanted to create (last page of this paper ) has less repetitious column headers. –  PulpSpy Feb 18 '13 at 3:05

You can use the adjustbox package to rotate the content and limit it official width. I would also recommend to use a separate \newcolumntype for it as well as a \rot macro which inserts the required \multicolumn command. You won't avoid that one. However, such a macro must be fully expandable and start directly with \multicolumn. If you want multi line headings add the minipage=<width> option to \adjustbox.

\documentclass{article}

\usepackage{array}

\newcolumntype{R}[2]{%
l%
<{\egroup}%
}
\newcommand*\rot{\multicolumn{1}{R{45}{1em}}}% no optional argument here, please!

\begin{document}

\begin{tabular}{r|ccc}
&
\rot{Property 1} &
\rot{Property 2} &
\rot{Property 3}
\\ \hline
System 1        &       &       &  X    \\
System 2        & X     & X     &  X    \\
System 3        & X &   &  X    \\ \hline
\end{tabular}

\renewcommand*\rot{\multicolumn{1}{R{60}{1em}}}% no optional argument here, please!

\begin{tabular}{r|ccc}
&
\rot{Property 1} &
\rot{Property 2} &
\rot{Property 3}
\\ \hline
System 1        &       &       &  X    \\
System 2        & X     & X     &  X    \\
System 3        & X &   &  X    \\ \hline
\end{tabular}

\renewcommand*\rot[2]{\multicolumn{1}{R{#1}{#2}}}% no optional argument here, please!

\begin{tabular}{r|ccc}
&
\rot{90}{1em}{Property 1} &
\rot{60}{1em}{Property 2} &
\rot{45}{2em}{Property 3}
\\ \hline
System 1        &       &       &  X    \\
System 2        & X     & X     &  X    \\
System 3        & X &   &  X    \\ \hline
\end{tabular}

\end{document}


-
Thanks, this worked perfectly for me. I did "cheat" and just use: \newcommand{\rot}[1]{\multicolumn{1}{c}{\adjustbox{angle=60,lap=\width-1em}{#1}}‌​} What is the advantage of using \newcolumntype? –  PulpSpy Nov 1 '11 at 19:20
@PulpSpy: You don't need \newcolumntype, but it simplifies it if you need it more often. –  Martin Scharrer Nov 1 '11 at 20:26

Wrapping the rotated contents in a box of fixed width you can easily manipulate the intended width of each column header. All of this, including the rotation, can be wrapped in a macro so that your code reads a little better. Moreover, some of the rotation and width specifications can be given default values so you only have to worry about them in special instances.

In the minimal example below, the command \rot[<angle>][<width>]{<stuff>} rotates <stuff> at angle <angle> (defaults to 45 degrees) and fixes it to have a width of <width> (default is 1em). The first two arguments are optional, while the last is mandatory.

\documentclass{article}
\usepackage{graphicx}% http://ctan.org/pkg/graphicx
\usepackage{booktabs}% http://ctan.org/pkg/booktabs
\usepackage{xparse}% http://ctan.org/pkg/xparse
% Rotation: \rot[<angle>][<width>]{<stuff>}
\NewDocumentCommand{\rot}{O{45} O{1em} m}{\makebox[#2][l]{\rotatebox{#1}{#3}}}%
\begin{document}

% ===== DEFAULT ROTATION PARAMETERS
\begin{tabular}{lccc}
& \rot{Property 1}
& \rot{Property 2}
& \rot{Property 3} \\
\midrule
System 1 &   &   & X \\
System 2 & X & X & X \\
System 3 & X &   & X \\
\bottomrule
\end{tabular}

\bigskip

% ===== MODIFIED ANGLE ROTATION
\begin{tabular}{lccc}
& \rot[60]{Property 1}
& \rot[60]{Property 2}
& \rot[60]{Property 3} \\
\midrule
System 1 &   &   & X \\
System 2 & X & X & X \\
System 3 & X &   & X \\
\bottomrule
\end{tabular}

\bigskip

% ===== MODIFIED ANGLE & WIDTH ROTATION
\begin{tabular}{lccc}
& \rot[25][3em]{Property 1}
& \rot[25][3em]{Property 2}
& \rot[25][3em]{Property 3} \\
\midrule
System 1 &   &   & X \\
System 2 & X & X & X \\
System 3 & X &   & X \\
\bottomrule
\end{tabular}

\end{document}​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​


To me, this allows for less manual interaction, since some of the parameters are set by default.

-

I would define a new column type using a rotated minipage; in the following example, the first mandatory argument of this new column type controls the angle of rotation, and the second argument specifies the minipage's width:

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

\newcolumntype{P}[2]{%
>{\begin{turn}{#1}\begin{minipage}{#2}\small\raggedright\hspace{0pt}}l%
<{\end{minipage}\end{turn}}%
}

\begin{document}

\begin{table}
\centering
\caption{Some properties}
\label{tab:test1}
\begin{tabular}{@{}rccc@{}}
\toprule
& \multicolumn{1}{P{90}{1.6cm}}{Property 1} &
\multicolumn{1}{P{90}{1.6cm}}{Property 2} &
\multicolumn{1}{P{90}{1.6cm}@{}}{Property 3 more text} \\
\midrule
System 1 &     &     & X \\
System 2 & X & X & X \\
System 3 & X &    & X \\
\bottomrule
\end{tabular}
\end{table}

\begin{table}
\centering
\caption{Some properties}
\label{tab:test2}
\begin{tabular}{@{}rccc@{}}
\toprule
& \multicolumn{1}{P{60}{1.6cm}}{Property 1} &
\multicolumn{1}{P{60}{1.6cm}}{Property 2} &
\multicolumn{1}{P{60}{1.6cm}@{}}{Property 3 more text} \\
\midrule
System 1 &     &     & X \\
System 2 & X & X & X \\
System 3 & X &    & X \\
\bottomrule
\end{tabular}
\end{table}

\end{document}​


-

The simplest way to do it (at least for me):

Add in the preamble of the document (wherever you feel like):

\def\rot{\rotatebox}


Then, inside the table, if you want to rotate some cell just wrap its content like:

\rot{angle}{content}


Where angle goes in degrees, and it be a number of course must.

-
Welcome to TeX.sx! Feel free to visit the TeX.SX starter guide to get the most out of the site. –  Peter Jansson May 28 '13 at 12:57