Latex: how to break the line in multirow inside the tabular

90,941

Solution 1

You could try to minipage it:

\begin{center}
\begin{tabular}{|l|l|l|l|l|l|}
    \hline
    \multirow{2}{*}{\begin{minipage}{0.5in}Long text to break\end{minipage}}
    & Thing  & \multicolumn{2}{|c|}{Thing 2} & \multicolumn{2}{|c|}{Thing 3} \\
    \cline{2-6}
    & sth 1 & sth 1 & sth 2 & sth 1  & sth 2 \\ 
    \hline
    \hline
\end{tabular}
\end{center}

However, in your particular case, my suggestion would simply be to loosen the restrictions of the other columns, because there is too much space wasted there. With each p{}, that forces the other columns to be a certain width, so there is not enough room for the first column.

The following code looked presentable to me when I compiled it:

\begin{center}
\begin{tabular}{|l|l|l|l|l|l|}
    \hline
    \multirow{2}{*}{Long text to break}
    & Thing  & \multicolumn{2}{|c|}{Thing 2} & \multicolumn{2}{|c|}{Thing 3} \\
    \cline{2-6}
    & sth 1 & sth 1 & sth 2 & sth 1  & sth 2 \\
    \hline
    \hline
\end{tabular}
\end{center}

Solution 2

p column and \parbox also works:

\usepackage{multirow}

\begin{document}
\begin{center}
\begin{tabular}{|p{1.5cm}|l|l|l|l|l|}
    \hline
    \multirow{2}{*}{\parbox{1.5cm}{Long text to break}}
    & Thing  & \multicolumn{2}{|c|}{Thing 2} & \multicolumn{2}{|c|}{Thing 3} \\
    \cline{2-6}
    & sth 1 & sth 1 & sth 2 & sth 1  & sth 2 \\ 
    \hline
    \hline
\end{tabular}
\end{center}
\end{document}

parbox in latex document

Solution 3

for me the shortest and most practical answer:

use \linewidth as the length for the {width} parameter.

\usepackage{multirow}
\begin{document}

\begin{center}
\begin{tabular}{|p{1cm}|p{2.5cm}|p{2cm}|p{2cm}|p{2cm}|p{2cm}|}
\hline
\multirow{2}{\linewidth}{Long text to break} % HERE IS A PROBLEM
    & Thing  & \multicolumn{2}{|c|}{Thing 2} & \multicolumn{2}{|c|}{Thing 3}    
\\ \cline{2-6}
    & sth 1 & sth 1 & sth 2 & sth 1  & sth 2 \\ \hline
\hline
\end{tabular}
\end{center}

\end{document}

That's it!

The only possible problem is that in the improbable case that the text in the other cells is really short it may look like that: Broken text in the right width but sadly going out of the table

However if typically your table has more text on the other cells than just "sth1" it will look great: enter image description here

Solution 4

For me it worked to use the build-in command of "multirow" - the {*} is "{width}"

Solution 5

Also, using parbox and \\:

\documentclass{article}
\usepackage{multirow}

\begin{document}

\begin{center}
    \begin{tabular}{|p{1cm}|p{2.5cm}|p{2cm}|p{2cm}|p{2cm}|p{2cm}|}
        \hline
        \multirow{2}{*}{\parbox{1cm}{Long\\ text\\ to\\ break}} % NOT A PROBLEM?
        & Thing  & \multicolumn{2}{|c|}{Thing 2} & \multicolumn{2}{|c|}{Thing 3}    
        \\ \cline{2-6}
        & sth 1 & sth 1 & sth 2 & sth 1  & sth 2 \\ \hline
        \hline
    \end{tabular}
\end{center}

\end{document}

Be anyway careful not to exceed the margins of your cells.

Share:
90,941
kokosing
Author by

kokosing

Software engineer and Co-founder of Starburst, the enterprise Presto company (https://www.starburstdata.com/). Contributor and committer to PrestoSql (https://prestosql.io/, https://github.com/prestosql/presto)

Updated on April 20, 2020

Comments

  • kokosing
    kokosing about 4 years

    I cannot find out how to break the line inside the multirow in tabular. I need to make some table where I have one cell which is two row high, and I have long text in it, but it does not break the line and text is overlapping another cell on the left side.

    Any suggestions?

    Sample of code:

    \begin{center}
        \begin{tabular}{|p{1cm}|p{2.5cm}|p{2cm}|p{2cm}|p{2cm}|p{2cm}|}
        \hline
        \multirow{2}{*}{Long text to break} % HERE IS A PROBLEM
            & Thing  & \multicolumn{2}{|c|}{Thing 2} & \multicolumn{2}{|c|}{Thing 3}    
        \\ \cline{2-6}
            & sth 1 & sth 1 & sth 2 & sth 1  & sth 2 \\ \hline
    \hline
    \end{tabular}
    \end{center}