2

I am facing problem to combine multicolumn and multirow together in a table. Anyone help what should be the latex code for below table?

tex

Update

I have tried some code,

\begin{tabular}{|c|c|cc|}
    \hline
\multicolumn{2}{|c|}{}  &   \multicolumn{2}{c|}{C}  \\
    \cline{3-4}
\multicolumn{2}{|c|}{R}&   true    &   false       \\
    \hline
\multirow{2}{*}{R}      &   true    &   1   &   1   \\
\hline
            &   false   &   2       &   3           \\
    \hline
\end{tabular}

Output: enter image description here

It's splitting the row. And I don't know how to make column margin at the top.

4
  • \multirow and multicolumn cells ... show what you try so far that we can simly show, how to use them. See tex.stackexchange.com/questions/375581/… (your question can be duplicate to this).
    – Zarko
    Nov 17, 2019 at 18:42
  • @Zarko this is not same question. I am facing problem, when I add \hline it crosses the row margin and splits the row. Nov 17, 2019 at 18:45
  • 1
    One \hline (use \cline instead of tit) should not be a problem. Show us, what you try so far. Otherwise your question is "do-this-instead-of-me" and will be closed us to broad or as duplicate (what for sure it is)
    – Zarko
    Nov 17, 2019 at 18:49
  • @Zarko I have updated the post. Please help. Nov 17, 2019 at 18:56

3 Answers 3

6

Probably one of the following suggestion helps:

enter image description here

\documentclass{article}
\usepackage{multirow}
\begin{document}

\begin{tabular}{|c|c|c|}
\hline
\multirow{2}{*}{R} & \multicolumn{2}{c|}{C} \\ \cline{2-3}
                   & C1 & C2               \\ \hline
r1                 & a  & b                \\ \hline
r2                 & a  & b                \\ \hline
\end{tabular}



\begin{tabular}{|c|c|c|c|}
    \hline
\multicolumn{2}{|c|}{\multirow{2}{*}{R}}  &   \multicolumn{2}{c|}{C}  \\
    \cline{3-4}
\multicolumn{2}{|c|}{}                    &   true    &   false       \\
    \hline
\multirow{2}{*}{R}      &   true          &   1       &   1           \\
\cline{2-4}
                        &   false         &   2       &   3           \\
    \hline
\end{tabular}
\end{document}
1

adding a table from your mwe.

   \documentclass{article}
   \usepackage{multicol, multirow}

   \begin{document}
   \begin{table}
   \centering
   \caption{captionless table}
   \label{tab:example}
   \renewcommand\multirowsetup{\centering}
   \begin{tabular}{|c|c|c|}
   \hline
   & \multicolumn{2}{c|}{C}\\
   \cline{2-3}
   \multirow{-2}{*}{R} & c1 & c2\\
   \hline
   r1 & a & b\\
   \hline
   r2 & c & d\\
   \hline
   \end{tabular}

   \end{table}

\begin{tabular}{|cc|c|c|}
   \hline
\multicolumn{2}{|c}{\multirow{2}{*}{R}}  &  \multicolumn{2}{|c|}{C}  \\
   \cline{3-4}
 &   true    &   false       \\
   \hline
\multicolumn{1}{|c|}{\multirow{2}{*}{R}}   &   true    &   1   &   1   \\
\cline{2-4}
\multicolumn{1}{|c|}{}          &   false   &   2       &   3           \\
   \hline
\end{tabular}

 \end{document}
3
  • Thanks a lot. It helps. Nov 17, 2019 at 18:57
  • 1
    For this MWE you not need multicol package!
    – Zarko
    Nov 17, 2019 at 18:57
  • You might want to replace \cline{3-4} & true & false \\ with \cline{3-4} & & true & false \\ . Otherwise, the cells are shifted to the left.
    – leandriis
    Nov 17, 2019 at 19:29
1

In {NiceTabular} of nicematrix, you have a built-in command \Block to merge cells both horizontally and vertically. The key hvlines draws all the rules excepted in the blocks.

\documentclass{article}
\usepackage{nicematrix}
\begin{document}

\begin{NiceTabular}{cccc}[hvlines]
\Block{2-2}{R} &       & \Block{1-2}{C} \\
               &       & true & false \\
\Block{2-1}{R} & true  & 1    & 1 \\
               & false & 2    & 3 
\end{NiceTabular}

\end{document}

Output of the above code

You must log in to answer this question.

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