2

I have two problems with a table as highlighted in red in the figure below.

  1. First, on the left, the multirow cell containing the text "Number" is not aligned at the center of the cell. I believe this happens because I have used a tabular inside the cell containing the "Very long category name distributed on two rows"; I did this because I need this text to be on two rows and I do not know another way to do that.
  2. The cell containing "Category" is obtained using multirow but as you can see on the right, there is no vertical bar. I have no idea why this happens.

Anyone knows how I can fix this? Here is my code with a MWE:

\documentclass[a4paper]{article}
\usepackage{multirow}

\begin{document}
\begin{table}[t]
\begin{center}

\begin{tabular}{|c|c|c|c|c|} 
 \hline
 \multirow{2}{*}{Number} & \multicolumn{4}{c}{Category} \\ \cline{2-5}  
 & \begin{tabular}{c} Very long category name \\ distributed on two rows \end{tabular}  & cat.\ A & cat.\ B & \begin{tabular}{c} Another very long name \\ on two rows \end{tabular}\\
 \hline
 abc & def & ghi & jkl & mno\\
 \hline
\end{tabular}

\end{center}
\end{table}
\end{document}  

enter image description here

3
  • 2
    Unrelated: the package multicol is for having text in multiple columns, it has nothing to do with tables. The macro \multicolumn is defined in the LaTeX kernel. – campa Oct 16 '20 at 11:52
  • Thanks @campa for your suggestion, I removed multicol from my code! – cholo14 Oct 16 '20 at 11:57
  • 2
    Since multicolumn overrides stuff you'll have to add the vertical line manually thus use {c|} not {c} – daleif Oct 16 '20 at 11:58
2

You can also draw that table with {NiceTabular} of nicematrix with two advantages:

  • Instead of \multirow, you have a command \Block; with that command (which may be used to merge cells vertically but also horizontally), you have to specify the number of rows (and the number of columns) whereas, with \multirow, you have to specify the number of physical lines (which requires sometimes tuning as in your case).

  • In {NiceTabular}, you have a key hvlines which draws all the rules, excepted in the blocks.

\documentclass[a4paper]{article}
\usepackage{nicematrix}

\begin{document}
\begin{table}[t]
\begin{center}
\begin{NiceTabular}{ccccc}[hvlines]
    \Block{2-1}{Number} & \multicolumn{4}{c}{Category} \\ 
    & \begin{tabular}{c} Very long category name \\ distributed on two rows \end{tabular}  
    & cat.\ A & cat.\ B & \begin{tabular}{c} Another very long name \\ on two rows \end{tabular}\\
    abc & def & ghi & jkl & mno\\
\end{NiceTabular}
\end{center}
\end{table}
\end{document} 

However, you need several compilations since nicematrix uses PGF/Tikz nodes.

Output of the above code

0

enter image description here

\documentclass[a4paper]{article}
\usepackage{multirow}

\begin{document}
    \begin{table}[t]
        \begin{center}
            
            \begin{tabular}{|c|c|c|c|c|} 
                \hline
                \multirow{3.5}{*}{Number} & \multicolumn{4}{c|}{Category} \\ \cline{2-5}  
                & \begin{tabular}{c} Very long category name \\ distributed on two rows \end{tabular}  & cat.\ A & cat.\ B & \begin{tabular}{c} Another very long name \\ on two rows \end{tabular}\\
                \hline
                abc & def & ghi & jkl & mno\\
                \hline
            \end{tabular}
            
        \end{center}
    \end{table}
\end{document} 
3
  • Could you explain why the 3.5 value in the multirow? It does not seem to be exactly centered if I zoom in – cholo14 Oct 17 '20 at 10:08
  • \multirow{3}{*}{Number} should vertically center the word "Name" and thus result in the desired output. – leandriis Oct 17 '20 at 10:19
  • it was purely to show the multirow command can take decimals for fine tuning -- you can experiment with the appropriate number -- i use 3.5 since with 3 it was aligning with the horizontal rule under Category – js bibra Oct 17 '20 at 11:41

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.