17

How do I code a table to look like this? I'm not the best at LaTeX and can't seem to wrap my head around \multirow. I checked other examples prior to posting but they used \multicolumn and it just confused me even more.

Table

\begin{center}
\begin{tabular}{ |c|c|c|c|c|} 
 \hline
 TextA & TextB & TextC & TextD & TextE\\
 \hline
 A & TextF & TextG & TextH & TextI\\
 B & TextJ & TextK & TextL & TextM\\
 C & \multirow{2}*{TextF} & TextG & TextH & TextI\\
 \cline{3-3}
 & TextJ & TextK \\
\hline
\end{tabular}
\end{center}

I was trying something like this.. I'm starting to have problems as I'm trying to multirow within a multirow? I'm not sure of the process here. I would like TextG to be split into 2 and TextH and TextI to be the cells which are split in 3 but not sure how to go about it. Will then TextJ and TextK be the contents of the split TextG cell?

5
  • 2
    what you try so far? from images i see, that multirow cells you need only in the bottom row: \multirow{3}*{....} in the first two columns, and \multirow{2}*{....} in the third column. for use multirow you need to load multirow package in document preamble. for more, you should provide small but complete document with this tale (without multi row cells).
    – Zarko
    Commented Mar 11, 2018 at 18:22
  • @Zarko thanks for the response. I'm mainly confused by what the brackets mean after multirow, and where to insert the text that you want in those rows. How do I split them into threes? Is that not the name of the multirow for the previous one?
    – E.Don
    Commented Mar 11, 2018 at 18:45
  • dots in \multirow{3}*{....} are instead real text (which is unknown to us). please provide small document which produce showed table. and don't bother with multirow this we can easily add to table. help us to help you. writing document from scratch and don't knowing more about your table doesn't lead to desired result.
    – Zarko
    Commented Mar 11, 2018 at 18:53
  • @Zarko I've edited the post to add a somewhat attempt at this.
    – E.Don
    Commented Mar 11, 2018 at 19:20
  • Unrelated, but you may use some visual latex table generator (like tablesgenerator.com/#).
    – hola
    Commented Dec 16, 2021 at 19:03

3 Answers 3

17

enter image description here

i extend your table code (and also correct it) that the table mimic the table which you show in question. note: cells in rows, where multirow cell is placed, had to be presented but should be empty (see mwe below):

mwe:

\documentclass{article}
\usepackage{multirow}

\begin{document}
\begin{center}
\renewcommand\arraystretch{1.3}
\begin{tabular}{|c|c|c|c|c|}
 \hline
 Text A     & Text B    & Text C    & Text D    & Text E    \\
 \hline
 A          & Text F    & Text G    & Text H    & Text I    \\
 \hline
 B          & Text J    & Text K    & Text L    & Text M    \\
 \hline
 \multirow{3}*{Text N}
            & \multirow{3}*{Text O}
                        & Text P    & Text Q    & Text R    \\
 \cline{3-5}
            &           & \multirow{2}*{Text S}
                                    & Text T    & Text U    \\
 \cline{4-5}
            &           &           & Text V    & Text Z    \\
\hline
\end{tabular}
    \end{center}
\end{document}
3
  • Thank you for your help! What does the \renewcommand\arraystretch{1.3} do? Also can you explain why you used multirow on text N and why its width is 3?
    – E.Don
    Commented Mar 11, 2018 at 21:33
  • 2
    (i) basic knowledge about table settings you can learn on site en.wikibooks.org/wiki/LaTeX/Tables.; (ii) \renewcommand\arraystretch{1.3} stretch distance between table rows. default is 1, so 1.3 increase distance about 30% (good to know that in multi line cells doesn't work well in all circumstances); (ii) in \multirow{3}{*{...} number 3 tel, hove many lines of text in other columns cell will span. cell with Text N span 3 lines of text (which in this case is equal to 3 table rows. the same valid for cell which contain Text =, bit cell with Text S span only 2 lines/rows.
    – Zarko
    Commented Mar 11, 2018 at 21:49
  • 1
    the last sentence in my above comment has some typing/spelling errors. it should be: the same applies to a cell containing a Text O. cell with Text S span only 2 lines/rows.
    – Zarko
    Commented Mar 11, 2018 at 22:09
4

You can do such table with {NiceTabular} of nicematrix.

In {NiceTabular}, you merge cells both horizontally and vertically with the command \Block. For the rows, you give the number of logical rows (and not the number of physical lines as with \multicolumn).

With the key hvlines, all the rules are drawn, excepted in the blocks (constructed by \Block).

\documentclass{article}
\usepackage{nicematrix}

\begin{document}
\begin{center}
\renewcommand\arraystretch{1.3}
\begin{NiceTabular}{ccccc}[hvlines]
 Text A     & Text B    & Text C    & Text D    & Text E    \\
 A          & Text F    & Text G    & Text H    & Text I    \\
 B          & Text J    & Text K    & Text L    & Text M    \\
 \Block{3-1}{Text N}
            & \Block{3-1}{Text O}
                        & Text P    & Text Q    & Text R    \\
            &           & \Block{2-1}{Text S}
                                    & Text T    & Text U    \\
            &           &           & Text V    & Text Z    \\
\end{NiceTabular}
\end{center}
\end{document}

You need several compilations (because nicematrix uses PGF/Tikz nodes under the hood).

Output of the above code

1
1

Just a quick comment that you should always provide the central brackets in multirow. ie, from the example above:

\multirow{3}*{Text N}

Should actually be:

\multirow{3}{*}{Text N}

This is because the lack of {*} brackets will break latexdiff, see for instance: latexdiff produces invalid output from changes to \multirow

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .