3

I'm working with a long table in and I'm having some issues with the it. I can't wrap the text in the last column, which uses multicolumn. Therefore, the text goes out of margins. Here's my code:

\usepackage{multirow}
\usepackage{makecell}

\begin{tabularx}{\textwidth}{XXXXXX}%[H]
        \hline
    Header that can be wrapped & Header 2 & Header 3 & Header 4 & Header 5 & Header 6\\ 
      \hline
    Data1 & 0.111 & 0.112 & 0.113 & 0.114 & \multirow{3}{*}{This text is too long and can not be wrapped}\\
    Data2 & 0.221 & 0.222 & 0.223 & 0.224 & \\
    Data3 & 0.331 & 0.332 & 0.333 & 0.334 & \\
    \hline
    \end{tabularx}

First Try

I've tried to force the line in the last column to break, but then it invades the cell below. This is what I've tried:

 \usepackage{multirow}
 \usepackage{makecell}    
\begin{tabularx}{\textwidth}{XXXXXX}%[H]
            \hline
        Header that can be wrapped & Header 2 & Header 3 & Header 4 & Header 5 & Header 6\\ 
          \hline
        Data1 & 0.111 & 0.112 & 0.113 & 0.114 & \multirow{3}{*}{This text is too long and can not be wrapped}\\
        Data2 & 0.221 & 0.222 & 0.223 & 0.224 & \\
        Data3 & 0.331 & 0.332 & 0.333 & 0.334 & \\
        \hline
        Data4 & 0.111 & 0.112 & 0.113 & 0.114 & \multirow{2}{*}{Cell bellow}\\
        Data5 & 0.221 & 0.222 & 0.223 & 0.224 & \\
        \hline
        \end{tabularx}

Second try

Any idea of how can I fix this situation?

  • welcome to tex.se! (i) please always provide complete but small document beginning with \documentclass{...} and ending with end{document}; (ii) your second code fragment not produce the showed table ; (iii) if multirow cell has more lines than cell spanned by it, than its text will protrude in rows below (or above). in such a cases you need to make spanned cells higher. – Zarko May 26 '18 at 05:34

1 Answers1

3

When the rows on the left are less heigh then the text on the right, put them in a sub tabular within a \multicolumn.

When the rows on the left are more heigh then the text on the right, you can simply use = instead of * in the \multirow command, from the package manual:

The width parameter can be specified as = to use the defined width of the column in which the \multirow appears.

I also suggest to you some improvements: use booktabs for the rules and siunitx for the digit alignment.

The code \renewcommand\tabularxcolumn[1]{m{#1}} for vertical centering text in X column, is from this answer by Zarko.

\documentclass{book}
\usepackage{array}
\newcolumntype{P}[1]{>{\raggedright\arraybackslash}p{#1}}
\newcolumntype{U}{S[table-format=1.3,table-column-width=4em]}
\usepackage{booktabs}
\usepackage{tabularx}
\renewcommand\tabularxcolumn[1]{m{#1}}% for vertical centering text in X column code from https://tex.stackexchange.com/a/343329/101651
\usepackage{multirow}
\usepackage{siunitx}

\begin{document}    
\begin{table}
\begin{tabularx}{\textwidth}{P{1.5cm} *4{U} X}
    \toprule
    Header that can be wrapped & {Header 2} & {Header 3} & {Header 4} & {Header 5} & Header 6\\ 
    \midrule
    \multicolumn{5}{@{}c@{}}{\begin{tabular}{P{1.5cm}*4{U}}
            Data1 & 0.111 & 0.112 & 0.113 & 0.114 \\
            Data2 & 0.221 & 0.222 & 0.223 & 0.224 \\
            Data3 & 0.331 & 0.332 & 0.333 & 0.334 \\
        \end{tabular}} &
    This text is too long and can not be wrapped long long long long text\\
    \midrule
    Data4 & 0.111 & 0.112 & 0.113 & 0.114 & \multirow{2}{=}{Cell below}\\
    Data5 & 0.221 & 0.222 & 0.223 & 0.224 & \\
    \bottomrule
\end{tabularx}
\end{table}
\end{document}

enter image description here

CarLaTeX
  • 62,716
  • good idea to nesting table (+1)! – Zarko May 26 '18 at 06:02
  • Hey! Thank you for your help. I tried to run the code just as it is but it didn't compile. Some errors came up (one "Missing number, treated as zero" and "Illegal unit of measure (pt inserted)"). Do you have any idea of how can I fix them? Cheers – Manuel González May 27 '18 at 18:13
  • Hey @CarLaTeX. First I put it in my document and didn't work. Then I run it alone, exactly as you posted it and got the errors I mentioned before. I'm not very good at Latex, so my coding is rather simple. That's why I'm having a hard time dealing with these issues. Thanks for your help, I really appreciate it – Manuel González May 27 '18 at 18:28
  • @ManuelGonzález Try deleting the auxiliary files and running again. If it still doesn't work, maybe it's a problem with the cut & paste, check if the strange chars like * are like they appear in my example. – CarLaTeX May 27 '18 at 18:42