31

I use \rotatebox to create a table with vertical headers, like so:

\documentclass{article}

\usepackage{rotating}

\begin{document} 

\begin{table}
\begin{tabular}{|l|l|l|}
\hline
\rotatebox{90}{Header 1} &
\rotatebox{90}{Header 2} &
\rotatebox{90}{Header 3}\\
\hline
Content & Content & Content\\
\hline
\end{tabular}
\end{table}

\end{document}

Now, I have a header that is awkwardly long (and cannot be abbreviated in a meaningful way). I would like to break the header into two lines. However, I can't seem to get this to work with \linebreak or \\, so apparently some trickery is needed.

Help?

David Carlisle
  • 757,742
DevSolar
  • 7,857
  • Rotating the column headers isn't a great solution. Is spreading them out over two or more lines an acceptable solution? –  Jan 08 '12 at 21:12
  • 1
    @MarcvanDongen: I'm working on a book translation, with the original in a rather cramped layout (which I still want to conserve). The actual table has 27 columns. Rotated headers is about the only way to get this done, and the way the original book did it, too. Unfortunately, the German headers are quite a bit longer than the English ones, and with 77 rows I'm running out of vertical space. Hence, it's a line break in the vertical header, or spreading this sea of numbers over multiple pages, which wouldn't help the document any... – DevSolar Jan 08 '12 at 21:17
  • Would you mind giving an example of one such table? –  Jan 08 '12 at 21:21
  • Have you thought of rotating the table? That would give you some extra space. –  Jan 08 '12 at 21:25
  • @DevSolar Thanks. Got it. Can you also provide the text in the first column (the one that's entitled Profession)? –  Jan 08 '12 at 22:06
  • @MarcvanDongen: ??? What do you need that for? The first lines are Monk - Ranger - Bard - Fighter - Thief - Rogue. The rest should be legible. – DevSolar Jan 09 '12 at 05:41
  • @DevSolar Nothing is legible. As to your triple question mark, I was trying to see if I could come up an alternative table. –  Jan 09 '12 at 08:31
  • @MarcvanDongen: As much as I welcome your offer, keeping as close as possible to the original layout is one of the project goals. I.e., I am not looking for an alternative table. The parbox suggestion solved my problem nicely. – DevSolar Jan 09 '12 at 09:40

4 Answers4

35

How about putting the "problematic" header into a \parbox and manually adding \\ (which works inside the box) where necessary?

\documentclass{article}

\usepackage{rotating}

\begin{document} 

\begin{table}
\begin{tabular}{|l|l|l|}
\hline
\rotatebox{90}{Header 1} &
\rotatebox{90}{Header 2} &
\rotatebox{90}{\parbox{4cm}{An awkwardly long \\header that cannot be \\abbreviated in a \\meaningful way}}\\
\hline
Content & Content & Content\\
\hline
\end{tabular}
\end{table}

\end{document}

enter image description here

David Carlisle
  • 757,742
lockstep
  • 250,273
10

You can wrap the long header in a \parbox:

enter image description here

\documentclass{article}

\usepackage{rotating}

\begin{document} 

\begin{table}
\begin{tabular}{|l|l|l|}
\hline
\rotatebox{90}{Header 1} &
\rotatebox{90}{Header 2} &
\rotatebox{90}{\parbox{1.5cm}{Long Header Over Several Lines}}\\
\hline
Content & Content & Content\\
\hline
\end{tabular}
\end{table}

\end{document}
Moriambar
  • 11,466
Peter Grill
  • 223,288
  • This has worked for me too, but there is a problem: the content of the \parbox comes awfully close to the upper line of the table. Like, it touches it, which doesn't look good. How can one add some extra space at the top of the cell (between text and line)? – Andyc Nov 24 '20 at 12:14
  • @Andyc: I think that problem may be helpful to others as well, so suggest you ask a new question with a MWE including \documentclass and the appropriate packages that reproduces the problem. If this answer is relevant you can link to it and can even use this as a starting point in creating the MWE. – Peter Grill Nov 24 '20 at 19:03
6

You can use \adjustbox{minipage=<width>,rotate=<angle>}{some text\\with line break} from the adjustbox package to create rotated box with the given width which can have manual and automatic line breaks.

Martin Scharrer
  • 262,582
0

If you are using tabularx instead of tabular and your column type is X then the above solutions might not work as expected (see his question: Issue with rotation and line breaking in tabularx).

The solution is to use \raggedright:

\begin{table}
  \begin{tabularx}{\textwidth}{|X|X|X|X|X|X|}
    \multicolumn{1}{l|}{%
        \rotatebox{90}{\parbox{2cm}{\raggedright Some Longer Text}}} &
  \end{tabularx}
\end{table}
lanoxx
  • 1,093