17

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?

  • Rotating the column headers isn't a great solution. Is spreading them out over two or more lines an acceptable solution? – user10274 Jan 8 '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 8 '12 at 21:17
  • Would you mind giving an example of one such table? – user10274 Jan 8 '12 at 21:21
  • Have you thought of rotating the table? That would give you some extra space. – user10274 Jan 8 '12 at 21:25
  • Duplicate: Wrapping table column headings in -turn- environment? (in terms of the answer). – Werner Jan 8 '12 at 21:27
20

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

6

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

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.

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}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.