[Tex/LaTex] How to make TeX fit rotated text into table cell

multirowrotatingtables

This is what I have in HTML:

<style type="text/css">
table, th, td
{
border: 1px solid black;
border-collapse: collapse;
padding: 2px;
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:

Desired

Actual:

Actual

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

Herbert's rules

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.

Best Answer

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:

enter image description here

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

Related Question