3

How to merge table cells in Latex

I have this table

enter image description here

\begin{table*}[htbp]
    \caption{Test Table}
    \begin{center}
        \begin{tabular}{|c|c|c|}
            \hline
            \textbf{\thead{Type}} & \textbf{\thead{Category}} & \textbf{{Name}}\\
            \hline
            Apple&IT&Nancy\\
            \hline
            Apple&Business&Mike\\
            \hline
            Apple&IT&Ali\\
            \hline
            Microsoft&Marketing&Linda\\
            \hline          
            Microsoft&IT&Sam\\
            \hline
            Google&Marketing&Eduard\\
            \hline
            Google&Business&John\\
            \hline
            
        \end{tabular}
        \label{graph_1}
    \end{center}
\end{table*}

I want to join similar values in the first column

So it would look like this.

enter image description here

2
  • --table firstly looks better without verticals-- secondly booktabs package will give errors with the vertical rules hence I have removed these
    – js bibra
    Dec 10, 2020 at 3:08
  • also I missed out the bold font in the caption -- now added after editing with the additional \usepackage[labelfont=bf]{caption}
    – js bibra
    Dec 10, 2020 at 3:18

4 Answers 4

2

with \multirow and \multicolumn

\documentclass{article}
\usepackage{multirow}

\begin{document}
\begin{table*}[htbp]
    \caption{Test Table}
    \label{tab:my-table}
    \begin{center}
    \begin{tabular}{|c|c|c|}
        \hline
                              \textbf{Type }& \textbf{Category}  & \textbf{Name}   \\ \hline
        \multirow{3}{*}{Apple}     & IT        & Nancy  \\ \cline{2-3} 
                                   & Business  & Mike   \\ \cline{2-3} 
                                   & IT        & Ali    \\ \hline
        \multirow{2}{*}{Microsoft} & Marketing & Linda  \\ \cline{2-3} 
                                   & IT        & Sam    \\ \hline
        \multirow{2}{*}{Google}    & Marketing & Eduard \\ \cline{2-3} 
                                   & Business  & John   \\ \hline
    \end{tabular}
    \end{center}
\end{table*}
\end{document}

enter image description here

3

With {NiceTabular} of nicematrix, you merge cells with the command \Block and, then you can draw all the rules with the key hvlines (the rules are not drawn in the blocks).

\documentclass{article}
\usepackage{nicematrix}

\begin{document}
\begin{table*}[htbp]
\caption{Test Table}
\label{tab:my-table}
\centering
\begin{NiceTabular}{ccc}[hvlines]
\textbf{Type }& \textbf{Category}  & \textbf{Name}   \\ 
    \Block{3-1}{Apple}     & IT        & Nancy  \\ 
                           & Business  & Mike   \\ 
                           & IT        & Ali    \\ 
    \Block{2-1}{Microsoft} & Marketing & Linda  \\ 
                           & IT        & Sam    \\ 
    \Block{2-1}{Google}    & Marketing & Eduard \\ 
                           & Business  & John   \\ 
\end{NiceTabular}
\end{table*}
\end{document}

Output of the above code

2

enter image description here

    \documentclass{article}
    \usepackage{makecell, multirow, booktabs}
    \usepackage[labelfont=bf]{caption}
    \begin{document}
    
    
\begin{table*}[htbp]
    \caption{Test Table}
    \begin{center}
        \begin{tabular}{ccc}
            \toprule
            \textbf{\thead{Type}} & \textbf{\thead{Category}} & \textbf{{Name}}\\
            \hline
            \multirow{3.7}{*}{Apple}&IT&Nancy\\
            \cmidrule{2-3}
            &Business&Mike\\
            \cmidrule{2-3}
            &IT&Ali\\
            \midrule
            \multirow{2.5}{*}{Microsoft}&Marketing&Linda\\
            \cmidrule{2-3}          
            &IT&Sam\\
            \midrule
            \multirow{2.5}{*}{Google}&Marketing&Eduard\\
            \cmidrule{2-3}
            &Business&John\\
            \bottomrule
            
        \end{tabular}
        \label{graph_1}
    \end{center}
\end{table*}
    \end{document}
2

To long to be a comment ...

A wee but important modification of @js bibra answer. Differences are:

  • Environment center is replaced by \centering command
  • In column headers are removed \textbf{...} and used just \thead instruction. For it is defined fonts in preamble with \renewcommand\theadfont{\small\bfseries}
  • For horizontal rules are used rules defined in the booktabs package. Rules are used only between group of types:
\documentclass{article}
\usepackage{booktabs,makecell, multirow, booktabs}
\renewcommand\theadfont{\small\bfseries}
\usepackage[skip=1ex, 
            font=small, labelfont=bf]{caption}

\begin{document}
    \begin{table}[htbp]
\caption{Test Table}
\label{tab:my-table}
    \centering
\begin{tabular}{lll}
        \toprule
             \thead{Type}   & \thead{Category}  & \thead{Name}  \\ \midrule
\multirow{3}{*}{Apple}      & IT                & Nancy         \\  
                            & Business          & Mike          \\  
                            & IT                & Ali           \\ \midrule
\multirow{2}{*}{Microsoft}  & Marketing         & Linda         \\ 
                            & IT                & Sam           \\ \midrule
\multirow{2}{*}{Google}     & Marketing         & Eduard        \\ 
                            & Business          & John          \\ 
            \bottomrule
\end{tabular}
    \end{table}
\end{document}

enter image description here

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.