3

I am trying to create a table having the following outlook.

Any ideas why in the 1st column in the 2nd and 3rd merged rows are not aligned vertically in the middle like the 3rd row???

The same question goes for 3rd and 5th column in the 4th row, the text again is not aligned in the middle???

I am not sure if it matters that I am compiling with XeLaTex (and not e.g. with pdfLaTex).

Many thanks 🙏 !!!

enter image description here

\documentclass{article}
 \usepackage{multirow}
 \usepackage{graphicx} 


\begin{document}
\begin{table}[]
\centering
\renewcommand{\arraystretch}{1.5}
\resizebox{\textwidth}{!}{%
\begin{tabular}{|c|c|c|c|c|}
\hline
\textbf{\begin{tabular}[c]{@{}c@{}}Two\\ Lines\end{tabular}} &
  \textbf{Line} &
  \textbf{\begin{tabular}[c]{@{}c@{}}Two\\ Lines\end{tabular}} &
  \textbf{\begin{tabular}[c]{@{}c@{}}Two\\ Lines\end{tabular}} &
  \textbf{\begin{tabular}[c]{@{}c@{}}Two\\ Lines\end{tabular}} \\ \hline
\multirow{2}{*}{\begin{tabular}[c]{@{}c@{}}Three\\ -\\ Lines\end{tabular}} &
  P1 &
  \begin{tabular}[c]{@{}c@{}}Text that I would wish\\ to span in two Lines\end{tabular} &
  \begin{tabular}[c]{@{}c@{}}Text that\\ I would\\ wish to span\\ in four Lines\end{tabular} &
  \begin{tabular}[c]{@{}c@{}}Text that\\ I would \\ wish to span\\ in four Lines\end{tabular} \\ \cline{2-5} 
 &
  P2 &
  \begin{tabular}[c]{@{}c@{}}Text that I would wish\\ to span in two Lines\end{tabular} &
  \begin{tabular}[c]{@{}c@{}}Text that\\ I would\\ wish to span\\ in four Lines\end{tabular} &
  \begin{tabular}[c]{@{}c@{}}Text that\\ I would\\ wish to span\\ in four Lines\end{tabular} \\ \hline
\multirow{4}{*}{\begin{tabular}[c]{@{}c@{}}Three\\ -\\ Lines\end{tabular}} &
  A1 &
  \multirow{4}{*}{\begin{tabular}[c]{@{}c@{}}Text common\\ to all rows\\ Vertically Centered\\ in the Middle\end{tabular}} &
  \begin{tabular}[c]{@{}c@{}}Text that I would wish\\ to span in two Lines\end{tabular} &
  \multirow{4}{*}{\begin{tabular}[c]{@{}c@{}}Text common\\ to all rows\\ Vertically Centered\\ in the Middle\end{tabular}} \\ \cline{2-2} \cline{4-4}
 &
  A2 &
   &
  \begin{tabular}[c]{@{}c@{}}Text that I would wish\\ to span in two Lines\end{tabular} &
   \\ \cline{2-2} \cline{4-4}
 &
  A3 &
   &
  \begin{tabular}[c]{@{}c@{}}Text that I would wish\\ to span in two Lines\end{tabular} &
   \\ \cline{2-2} \cline{4-4}
 &
  A4 &
   &
  \begin{tabular}[c]{@{}c@{}}Text that I would wish\\ to span in two Lines\end{tabular} &
   \\ \hline
\multirow{6}{*}{\begin{tabular}[c]{@{}c@{}}Three\\ -\\ Lines\end{tabular}} &
  A5 &
  \multirow{6}{*}{\begin{tabular}[c]{@{}c@{}}Text common\\ to all rows\\ Vertically Centered\\ in the Middle\end{tabular}} &
  One Line Text &
  \multirow{6}{*}{\begin{tabular}[c]{@{}c@{}}Text common\\ to all rows\\ Vertically Centered\\ in the Middle\end{tabular}} \\ \cline{2-2} \cline{4-4}
 &
  A6 &
   &
  One Line Text &
   \\ \cline{2-2} \cline{4-4}
 &
  A7 &
   &
  One Line Text &
   \\ \cline{2-2} \cline{4-4}
 &
  A8 &
   &
  One Line Text &
   \\ \cline{2-2} \cline{4-4}
 &
  A9 &
   &
  One Line Text &
   \\ \cline{2-2} \cline{4-4}
 &
  A10 &
   &
  One Line Text &
   \\ \hline
\end{tabular}%
}
\caption{TEST}
\label{tab:my-table}
\end{table}

\end{document}
  • 3
    Please always provide a MWE for people to be able to compile it – Sergey Belyaev Feb 21 at 12:59
  • Did you try booktabs? – Sergey Belyaev Feb 21 at 13:17
  • totally correct @SergeyBelyaev. I just approved an edit and I already made some improvement. – V. Nikolaidis Feb 21 at 15:13
  • 1
    I tried your example, and it does what you want if you set [b] option right after \multirow. I know this doesn't explain why tables behave in the way they do. – Sergey Belyaev Feb 21 at 15:21
  • 2
    Could you please be a bit more precise regarding the contents of your actual table? Do you really wish to manually split the text into multiple rows? – leandriis Feb 21 at 15:53
5
  • Don't use \resizebox, with it you loose control on the font size. Fonts can become to small that can be read.
  • The table generated by your MWE is (without use of \resizebox) wider than is text width. To remedy this you can select one of the following possibilities:
    • make \textwidth wider, for example with use of the \geometry package
    • reduce font and \tabcol size
    • define width of table in advance and use columns' type which enable automatically break text in more lines (if necessary). For example tabularxy table environment is promising.
  • with use of \thead instruction from the makecell package, is possible to write shorter and more clean code for columns' headers
  • defining column widths is possible to use new feature of the multirow package:
    • with option = multirow cell overtake defined width of a column.
    • with \renewcommand\multirowsetup{\centering} are texts in multirow cells horizontal centered

Complete MWE:

\documentclass{article}
\usepackage{makecell, multirow, tabularx}
\renewcommand\theadfont{\bfseries}
\renewcommand\theadgape{}

%---------------- show page layout. don't use in a real document!
\usepackage{showframe}
\renewcommand\ShowFrameLinethickness{0.15pt}
\renewcommand*\ShowFrameColor{\color{red}}
%---------------------------------------------------------------%

\begin{document}
    \begin{table}[ht]   % <---
\small
\setlength\tabcolsep{3pt}
\setcellgapes{2pt}
\makegapedcells
\renewcommand\multirowsetup{\centering} % <-----
\begin{tabularx}{\linewidth}{ |p{3em}|c| *{3}{>{\centering\arraybackslash}X|} }
    \hline
\thead{Two\\ Lines} 
    &   \thead{Line} 
        &   \thead{Two\\ Lines} 
            &   \thead{Two\\ Lines} 
                &   \thead{Two\\ Lines} \\ 
    \hline
\multirow{7}{=}{Three\\ -\\ Lines} 
    &   P1 
        &   Text that I would wish to span in two Lines
            &   Text that I would wish to span in four Lines
                &   Text that I would wish to span in four Lines
                                        \\ 
    \cline{2-5}
    &   P2 
        &   Text that I would wish to span in two Lines
            &   Text that I would wish to span in four Lines
                &   Text that I would wish to span in four Lines
                                        \\ 
    \hline
\multirow{13}{=}{Three\\ -\\ Lines} 
    &   A1 
        &   \multirow{13}{=}{Text common to all rows Vertically Centered in the Middle}
            &   Text that I would wish to span in two Lines
                &   \multirow{13}{=}{Text common to all rows Vertically Centered in the Middle}                 \\ 
    \cline{2-2} \cline{4-4}
    &   A2 
        &   &   Text that I would wish to span in two Lines
                &                       \\ 
    \cline{2-2} \cline{4-4}
    &   A3 
        &   &   Text that I would wish to span in two Lines
                &                       \\ 
    \cline{2-2} \cline{4-4}
    &   A4 
        &   &   Text that I would wish to span in two Lines
                &                       \\ 
    \hline
\multirow{8}{=}{Three\\ -\\ Lines} 
    &   A5 
        &   \multirow{8}{=}{Text common\\ to all rows\\ Vertically Centered\\ in the Middle} 
            &   One Line Text 
                &   \multirow{8}{=}{Text common\\ to all rows\\ Vertically Centered\\ in the Middle}             \\ 
    \cline{2-2} \cline{4-4}
    &   A6 
        &   &   One Line Text   &       \\ 
    \cline{2-2} \cline{4-4}
    &   A7
        &   &   One Line Text   &       \\
    \cline{2-2} \cline{4-4}
    &   A8
        &   &   One Line Text   &       \\
    \cline{2-2} \cline{4-4}
    &   A9
        &   &   One Line Text   &       \\
    \cline{2-2} \cline{4-4}
    &   A10
        &   &   One Line Text   &       \\
   \hline
\end{tabularx}%
%}
\caption{TEST}
\label{tab:my-table}
    \end{table}
\end{document}

enter image description here

(red lines indicate text borders)

|improve this answer|||||
  • Hi and many thanks for your solution. I will definitely keep your solution in mind since it is much simpler and structured code. However, if only the text was vertically aligned in the middle and horizontally aligned in the center. – V. Nikolaidis Feb 21 at 20:34
  • @V.Nikolaidis, ups, I didn't upload the last vewrsion of my answer. In code is mising code line \renewcommand\multirowsetup{\centering}. Corrected now. – Zarko Feb 21 at 22:19
  • @V.Nikolaidis, any news? Does answer now fulfill your expectation? Now is time that you decided, which of obtained answer is the best for you needs and accept them (by clicking on check mark at top left side of answer). SO far as I see you not accept any of answers received on your questions :-( – Zarko Feb 22 at 13:39
  • I thought by answering to all and by upvoting answers that I liked was they way to go. I am still not entirely familiar with the whole system ;-). I liked and tried all answers, however as I wrote again in an earlier message, I used a combination of @Sveinung code along with my tries to finish what I had started. However, I upvoted your code since it produces the same result and the code is much clearer and simpler. Thanks. – V. Nikolaidis Feb 23 at 21:37
3

You have misunderstood the use of multirow. You have to span the number of "normal" rows because multirow does not know the height of the spanning box, see page 6 in the manual and the example given there:

  1. The middle column is the \multirow. You would expect it to be vertically centered, but it isn’t. This is because \multirow doesn’t know the height of the box. The only estimate \multirow can make about the height is the number of rows the normal height of a row. It tries to center the text in that space, but that space is too low in this example. Therefore the text is at the top of the box. If you want it to be centered, you have to supply a hvmovei argument to shift it down.

Just increase the number of spanned rows, as indicated below, or fine tune utilising the optional argument [vmode=]:

enter image description here

\documentclass{article}
\usepackage{geometry}

 \usepackage{multirow}
 \usepackage{graphicx} 


\begin{document}
\begin{table}[]
\centering
\renewcommand{\arraystretch}{1.5}

\begin{tabular}{|c|c|c|c|c|}
\hline
\textbf{\begin{tabular}[c]{@{}c@{}}Two\\ Lines\end{tabular}} &
  \textbf{Line} &
  \textbf{\begin{tabular}[c]{@{}c@{}}Two\\ Lines\end{tabular}} &
  \textbf{\begin{tabular}[c]{@{}c@{}}Two\\ Lines\end{tabular}} &
  \textbf{\begin{tabular}[c]{@{}c@{}}Two\\ Lines\end{tabular}} \\ \hline
\multirow{5}{*}{\begin{tabular}[c]{@{}c@{}}Three\\ -\\ Lines\end{tabular}} &
  P1 &
  \begin{tabular}[c]{@{}c@{}}Text that I would wish\\ to span in two Lines\end{tabular} &
  \begin{tabular}[c]{@{}c@{}}Text that\\ I would\\ wish to span\\ in four Lines\end{tabular} &
  \begin{tabular}[c]{@{}c@{}}Text that\\ I would \\ wish to span\\ in four Lines\end{tabular} \\ \cline{2-5} 
 &
  P2 &
  \begin{tabular}[c]{@{}c@{}}Text that I would wish\\ to span in two Lines\end{tabular} &
  \begin{tabular}[c]{@{}c@{}}Text that\\ I would\\ wish to span\\ in four Lines\end{tabular} &
  \begin{tabular}[c]{@{}c@{}}Text that\\ I would\\ wish to span\\ in four Lines\end{tabular} \\ \hline
\multirow{7}{*}{\begin{tabular}[c]{@{}c@{}}Three\\ -\\ Lines\end{tabular}} &
  A1 &
  \multirow{7}{*}{\begin{tabular}[c]{@{}c@{}}Text common\\ to all rows\\ Vertically Centered\\ in the Middle\end{tabular}} &
  \begin{tabular}[c]{@{}c@{}}Text that I would wish\\ to span in two Lines\end{tabular} &
  \multirow{7}{*}{\begin{tabular}[c]{@{}c@{}}Text common\\ to all rows\\ Vertically Centered\\ in the Middle\end{tabular}} \\ \cline{2-2} \cline{4-4}
 &
  A2 &
   &
  \begin{tabular}[c]{@{}c@{}}Text that I would wish\\ to span in two Lines\end{tabular} &
   \\ \cline{2-2} \cline{4-4}
 &
  A3 &
   &
  \begin{tabular}[c]{@{}c@{}}Text that I would wish\\ to span in two Lines\end{tabular} &
   \\ \cline{2-2} \cline{4-4}
 &
  A4 &
   &
  \begin{tabular}[c]{@{}c@{}}Text that I would wish\\ to span in two Lines\end{tabular} &
   \\ \hline
\multirow{6}{*}{\begin{tabular}[c]{@{}c@{}}Three\\ -\\ Lines\end{tabular}} &
  A5 &
  \multirow{6}{*}{\begin{tabular}[c]{@{}c@{}}Text common\\ to all rows\\ Vertically Centered\\ in the Middle\end{tabular}} &
  One Line Text &
  \multirow{6}{*}{\begin{tabular}[c]{@{}c@{}}Text common\\ to all rows\\ Vertically Centered\\ in the Middle\end{tabular}} \\ \cline{2-2} \cline{4-4}
 &
  A6 &
   &
  One Line Text &
   \\ \cline{2-2} \cline{4-4}
 &
  A7 &
   &
  One Line Text &
   \\ \cline{2-2} \cline{4-4}
 &
  A8 &
   &
  One Line Text &
   \\ \cline{2-2} \cline{4-4}
 &
  A9 &
   &
  One Line Text &
   \\ \cline{2-2} \cline{4-4}
 &
  A10 &
   &
  One Line Text &
   \\ \hline
\end{tabular}%

\caption{TEST}
\label{tab:my-table}
\end{table}

\end{document}
|improve this answer|||||
  • Hi Sveinung. Thank you for your time spent in my problem. I did combine your solution with some of my own tries. So in the 2,3 &4th \multirow{7}{*} I used the [b] option ( \multirow[b]{4}{*} ) proposed by @Sergey and it somehow worked ;-) – V. Nikolaidis Feb 21 at 20:26
  • @V.Nikolaidis The b-option works by coincident, this will depend on the number of of spanned lines. Try to increase or decrease the number of lines, and see the effect. According to the manual, you should use [vmode=] to adjust the vertical alignment. – Sveinung Feb 21 at 21:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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