[Tex/LaTex] Newline in \multirow environment

line-breakingmultirowtables

One element of a multirow column is long. How can I split it so that it spans over multiple line. The following code

\documentclass{article}
\usepackage{multirow,array}
\usepackage{booktabs}
\begin{document}

\begin{tabular}{rcc}
  \toprule
  Name & Subject & Grade \\
  \midrule
  \multirow{3}*{John}           & Maths   & A \\
                                & Science & A \\
                                & Arts    & A \\[.5\normalbaselineskip]
  \multirow{3}*{Very Long Name} & Maths   & A \\
                                & Science & A \\
                                & Arts    & A \\
  \bottomrule
\end{tabular}

\end{document}

generates:

enter image description here

Is there a way to split the "Very Long Name" into multiple row?

Best Answer

What you might like to do is to use a minipage, within the \multirow argument to force the name to a certain horizontal width, breaking it across multiple lines if it exceeds a certain width. (This requires names with enough spaces, or at least which are allowed to be hyphenated.)

As egreg suggests in the comments, if you do not want the "Name" column to always take up the maximum possible width if the names are all sufficiently short, better still would be to use a varwidth environment (using the varwidth package), which also allows the text to take up less than the maximum amount of horizontal space.

Sample code:

\documentclass{article}
\usepackage{multirow,array,varwidth}
\usepackage{booktabs}
\begin{document}

\newcommand\NameEntry[1]{%
  \multirow{3}*{%
    \begin{varwidth}{5em}% --- or minipage, if you prefer a fixed width
    \flushright #1%
    \end{varwidth}}}

\begin{tabular}{rcc}
  \toprule
  Name & Subject & Grade \\
  \midrule
  \NameEntry{John}           & Maths   & A \\
                             & Science & A \\
                             & Arts    & A \\[.5\normalbaselineskip]
  \NameEntry{Very Long Name} & Maths   & A \\
                             & Science & A \\
                             & Arts    & A \\
  \bottomrule
\end{tabular}

\end{document}

(You should edit the width of the varwidth/minipage environment to suit your needs.)

Output:

Table created from the sample code