Wrap text in LaTeX table and merge rows?

Viewed 1204

I managed to either wrap text in latex using

\newcolumntype{L}{>{\centering\arraybackslash}m{3cm}}

or merge rows using

\multirow{3}{*}{Problem}

But I cannot use these two together. I would like a table like this, where "Problem x" and "Subproblem x.y" is centered both horizontally and vertically.

+-----------+----------------+---------------------------+
|           | SUBPROBLEM 1.1 | This line is long and too |
|           |                | long to fit in one line.  |
| PROBLEM 1 |----------------+---------------------------+
|           | SUBPROBLEM 1.2 | This could also be long   |
|           |                | and it is.                |
+-----------+----------------+---------------------------+
|           | SUBPROBLEM 2.1 | This could also be long.  |
| PROBLEM 2 |----------------+---------------------------+
|           | SUBPROBLEM 2.2 | This one is not long.     |
+-----------+----------------+---------------------------+

Minimal working problem.

\documentclass{article}
\usepackage{array, multirow}
\newcolumntype{L}{>{\centering\arraybackslash}m{3cm}}
\begin{document}
    \begin{tabular}{|c|c|L|}
        \hline
        \multirow{3}{*}{P1}  
        & P1.1 & This is indeed a problem. \\\hline
        & P1.2 & Also this.\\\hline
        & P1.3 & This migth also be one.\\\hline
    \end{tabular}
\end{document}

enter image description here The "P1" should be centered and in all three columns.

1 Answers
  • please have a look at the booktabs package. Your table will look much more professional if you remove all these nasty vertical lines

  • to avoid the lines being drawn through your merged cell, you can use \cline

  • to fine tune the vertical placement of P1, you can pass an optional argument to multirow


\documentclass{article}
\usepackage{array, multirow}
\newcolumntype{L}{>{\centering\arraybackslash}m{3cm}}
\begin{document}
    \begin{tabular}{|c|c|L|}
        \hline
        \multirow{3}{*}[-5.8pt]{P1}  
        & P1.1 & This is indeed a problem. \\%\hline
        \cline{2-3}
        & P1.2 & Also this.\\%\hline
         \cline{2-3}
        & P1.3 & This migth also be one.\\\hline
    \end{tabular}
\end{document}

enter image description here

Related