Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question

1 Answer 1

up vote 3 down vote accepted

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

share|improve this answer
    
Wouldn't this be a case where the varwidth environment (package with the same name) is better than minipage? –  egreg Jul 18 '12 at 12:44
    
@egreg: Looks like it. Revised to mention it. –  Niel de Beaudrap Jul 18 '12 at 13:15

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.