# How to make TeX fit rotated text into table cell?

This is what I have in HTML:

<style type="text/css">
table, th, td
{
border: 1px solid black;
border-collapse: collapse;
margin: 2px;
}
th {        background-color: #dddddd;    }
.right { text-align: right; }
.center { text-align: center; }
.left { text-align: left; }
</style>

<table>
<tr>
<th colspan="7">Mérővonalak</th>
</tr>
<tr>
<th class="center" rowspan="2">Sorszám</th>
<th class="center" colspan="2">Pozíció 1</th>
<th class="center" colspan="2">Pozíció 2</th>
<th class="right"  rowspan="2">Dőlés°</th>
<th class="right"  rowspan="2">Metszéspontok<br>száma</th>
</tr>
<tr>
<td class="center">1</td>
<td class="center"><small>17.90 mm</small></td>
<td class="center"><small>27.19 mm</small></td>
<td class="center"><small>-192.10 mm</small></td>
<td class="center"><small>69.53 mm</small></td>
<td class="right">-90.0°</td>
<td class="right">29</td>
</tr>
</table>

I have tried to do the same in XeLaTeX. I had to rotate the last column because there was not enough horizontal space for the table.

\begin{tabular}{|c|r|r|r|r|r|r|}
\hline
\multicolumn{7}{|c|}{Mérővonalak} \\
\hline
\multirow{2}{*}{Sorszám}
&   \multicolumn{2}{|c|}{Pozíció 1}
&   \multicolumn{2}{|c|}{Pozíció 2}
&   \multirow{2}{*}{Dőlés}
&   \multirow{2}{*}{\rotatebox{90}{~\parbox{1.5cm}{Metszéspontok Száma}~}} \\
\cline{2-5}
& Vízszintes
& Függőleges
& Vízszintes
& Függőleges
& \\
\hline
\end{tabular}

The vertical text goes out of the table. It even overwrites other text under the table. Shouldn't the tabular environment automatically calculate the vertical size of the row that is needed to fit the content? What am I doing wrong?

Desired:

Actual:

By using Herbert's rules, it is almost perfect:

I can make this work by changing the \rule height parameter to 3cm. But this is not a solution, because cell data comes from a database. It can be a short word, or a very long word. I need a flexible solution.

If you don't know the width/height of the object that will be used, you should store it in a box so LaTeX can calculate it. Here's a slight variation from Herbert's answer that stores the contents of the unknown cell in \box0. You can make your own box if your document is much larger, but it suffices in this MWE:

\documentclass{article}
\usepackage{fontspec}% http://ctan.org/pkg/fontspec
\usepackage{graphicx,multirow}% http://ctan.org/pkg/{graphicx,multirow}

\begin{document}

\setbox0\hbox{\tabular{@{}l}Metszéspontok\\ Száma\endtabular}
\begin{tabular}{|c|r|r|r|r|r|r|}\hline
\multicolumn{7}{|c|}{Mérővonalak} \\\hline
\multirow{2}{*}{Sorszám}
&   \multicolumn{2}{|c|}{Pozíció 1}
&   \multicolumn{2}{|c|}{Pozíció 2}
&   \multirow{2}{*}{Dőlés}
&   \rule{0pt}{\dimexpr\wd0-\normalbaselineskip}\\\cline{2-5} % save space for the rotatebox
& Vízszintes
& Függőleges
& Vízszintes
& Függőleges
&
& \rotatebox{90}{\rlap{\usebox0}}\\\hline
\end{tabular}

\end{document}​

With each row being displaced \normalbaselineskip apart, \dimexpr\wd0-\normalbaselineskip inserts the appropriate rule height for proper display.

• All right, this is fabolous. Although none of \dimexpr \normalbaselineskip or \wd can be found in the "not so short introduction to latex". So I guess I just accidentally ran into something special. I'll have to read more documentation. Sep 28, 2012 at 19:32

I wouldn't use a multirow cell here. rotate the box of height 0 and use \rule for the vertical space (delete fontspec, if you use pdflatex):

\documentclass{article}
\usepackage{fontspec}
\usepackage{graphicx,multirow}

\begin{document}

\begin{tabular}{|c|r|r|r|r|r|r|}\hline
\multicolumn{7}{|c|}{Mérővonalak} \\\hline
\multirow{2}{*}{Sorszám}
&   \multicolumn{2}{|c|}{Pozíció 1}
&   \multicolumn{2}{|c|}{Pozíció 2}
&   \multirow{2}{*}{Dőlés}
&   \rule{0pt}{2cm}\\\cline{2-5} % save space for the rotatebox
& Vízszintes
& Függőleges
& Vízszintes
& Függőleges
&
& \rotatebox{90}{\rlap{\tabular{@{}l}Metszéspontok\\ Száma\endtabular}}\\\hline
\end{tabular}

\end{document}

If you want allow hyphenation of Metszéspontok then use:

\begin{tabular}{|c|r|r|r|r|r|r|}\hline
\multicolumn{7}{|c|}{Mérővonalak} \\\hline
\multirow{2}{*}{Sorszám}
&   \multicolumn{2}{|c|}{Pozíció 1}
&   \multicolumn{2}{|c|}{Pozíció 2}
&   \multirow{2}{*}{Dőlés}
&   \rule{0pt}{1.75cm}\\\cline{2-5} % save space for the rotatebox
& Vízszintes
& Függőleges
& Vízszintes
& Függőleges
&
& \rotatebox{90}{\rlap{\parbox{2cm}{\hspace{0pt}Metszéspontok  Száma}}}\\\hline
\end{tabular}

and without knowing the width/height of the text:

\begin{tabular}{|c|r|r|r|r|r|r|}\hline
\multicolumn{7}{|c|}{Mérővonalak} \\\hline
\multirow{2}{*}{Sorszám}
&   \multicolumn{2}{|c|}{Pozíció 1}
&   \multicolumn{2}{|c|}{Pozíció 2}
&   \multirow{2}{*}{Dőlés}
&   \rotatebox{90}{\tabular{@{}l}Metszéspontok\\  Száma\endtabular}
\\\cline{2-5}
& Vízszintes
& Függőleges
& Vízszintes
& Függőleges
&
&  \\\hline
\end{tabular}

By the way: tabulars without vertical rules are better to read ...

• On my computer, the rotated text still overlaps with the top row. All right, I understand that I can increase the space manually. Unfortunately, I'm working on a program that generates TeX files from a database. There are certain limits as to how many lines can be in a cell, but I cannot tell in advance if it will contain just a 0.5cm short word or a 3cm long word. Sep 28, 2012 at 18:52
• see my last example
– user2478
Sep 28, 2012 at 19:21
• There is a small problem, the rotated text not occupying space on the second row, just the first row. See the red area link Otherwise it works, so answer accepted. Sep 28, 2012 at 19:27