8

I'd like a table that looks like this in Beamer:

nice table

But the best I can do is this:

ugly table

\documentclass{beamer}
\usepackage{booktabs}
\begin{document}
\frame{
\begin{tabular}{|c|c||c|}\cmidrule{2-3}\noalign{\vskip-\belowrulesep}
\multicolumn{1}{c|}{} & $M_2$ & $a$\\\hline
$M_1$ & $M_3$ &\\\noalign{\vskip-\aboverulesep}\cmidrule{1-2}
    \morecmidrules\cmidrule{1-2}\noalign{\vskip-\belowrulesep}
$b$ & \multicolumn{1}{c}{} & \multicolumn{1}{c|}{$x$}\\\hline
\end{tabular}
}
\end{document}

Is the table I want possible?

2
  • 2
    Don't use booktabs with vertical lines Commented Apr 21, 2023 at 14:32
  • @samcarter_is_at_topanswers.xyz, I asked a related question here.
    – user338955
    Commented Apr 22, 2023 at 2:23

4 Answers 4

8

A solution with the tabularray package:

\documentclass{beamer}
\usepackage{tabularray}
\begin{document}
\frame{
    \begin{tblr}{
        colspec = { *{3}{c }},
        hline{1} = {1}{2-3}{},
        hline{2,4} = {1}{1-3}{},
        hline{3} = {1}{1-2}{rightpos = 0},
        hline{3} = {2}{1-2}{},
        vline{1} = {1}{2-3}{},
        vline{2} = {1}{1-3}{},
        vline{3} = {1}{1-2}{},
        vline{3} = {2}{1-2}{belowpos = 1},
        vline{4} = {1}{1-3}{belowpos = 1},
    }
     & $M_2$ & $a$ \\ 
    $M_1$ & $M_3$ & \\
    $b$ &  & $x$ \\
    \end{tblr}
}
\end{document}

enter image description here


If you want to get a continuous gap also between the vertical lines on the right side, it might be better to use the nicematrix package, which combines the strengths of TikZ with the possibilities of tabulars (at least tabularray won't give you a nice result in this case):

\documentclass{beamer}
\usepackage{nicematrix, tikz}
\begin{document}
\frame{
    \begin{NiceTabular}{ *{3}{c} }
     & $M_2$ & $a$ \\ 
    $M_1$ & $M_3$ & \\ 
    $b$ &  & $x$ \\
    \CodeAfter
        \begin{tikzpicture}
            \draw 
                (1-|2) -- 
                ([xshift=-1pt]1-|3) -- 
                ([xshift=-1pt, yshift=1pt]3-|3) -- 
                ([yshift=1pt]3-|1) -- 
                (2-|1) -- (2-|2) -- cycle;
            \draw 
                ([xshift=1pt]1-|3) -- 
                (1-|4) -- (4-|4) -- (4-|1) --
                ([yshift=-1pt]3-|1) -- 
                ([xshift=1pt, yshift=-1pt]3-|3) -- 
                cycle;
            \draw (2-|2) -- ([xshift=-1pt]2-|3)
                ([xshift=1pt]2-|3) -- (2-|4);
            \draw (2-|2) -- ([yshift=1pt]3-|2) 
                ([yshift=-1pt]3-|2) -- (4-|2);
        \end{tikzpicture}
    \end{NiceTabular}
}
\end{document}

enter image description here

3
  • Thank you for this. This is what I need. I did not know about the tabularray package.
    – user338955
    Commented Apr 21, 2023 at 14:52
  • Your nicematrix answer is what I really want, I didn't ask for it in my post because I didn't think it was possible.
    – user338955
    Commented Apr 21, 2023 at 15:17
  • 1
    @user338955 You could also try to use a NiceArray since this uses math-mode, but you would need to change the syntax for the border drawings, I think. Commented Apr 21, 2023 at 15:23
6

Tikz for you :-)

\documentclass{beamer}
\usepackage{tikz}
\usetikzlibrary{positioning}
\begin{document}
\begin{frame}
  \begin{tikzpicture}[
    , thick
    , n/.style = {
      , minimum width = 10mm
      , minimum height = 6mm
      , draw
    }
    , outer sep = 0
    , node distance = 0 and 0
  ]
    \node (M2) [n] {$M_2$};
    \node (M3) [below = of M2, n] {$M_3$};
    \node (M1) [left = of M3, n] {$M_1$};
    \node (a) [right = 1mm of M2, n] {$a$}; % add , minimum width = 7mm if you want narrower node
    \node (b) [below = 1mm of M1, n] {$b$};
    \node (x) [below = 7mm of a.south, minimum height = 6mm] {$x$};
    \draw (M2.north east) -- (a.north west);
    \draw (M2.south east) -- (a.south west);
    \draw (a.south west) |- (b.north east);
    \draw (a.south east) |- (b.south east);
  \end{tikzpicture}%
\end{frame}
\end{document}

Result

3
  • Thanks. This is very good, but I think the other answer using the tabularray package is what I am more interested in.
    – user338955
    Commented Apr 21, 2023 at 14:53
  • 1
    @user338955 I previously knew the power of tabularray but haven't used them frequently. Your question is also a great opportunity for me to catch up with the tblr trend :-) Commented Apr 21, 2023 at 14:54
  • 1
    Your solution is also nice. I was wondering whether an approach using the nicematrix package would also work ... in the end, you would also be able to draw with tikz between the cells Commented Apr 21, 2023 at 14:56
5

With hhline package;

\documentclass{beamer}
\usepackage{hhline}

\begin{document}
\begin{frame}
\begin{center}
\renewcommand\arraystretch{1.2}
    \begin{tabular}{|c|c||c|}
    \hhline{~|-|-|}
\multicolumn{1}{c|}{}
        & $M_2$ & $a$   \\
    \hhline{-|--|}
$M_1$   & $M_3$ &       \\
    \hhline{:=:=:b|~|}
$b$     & \multicolumn{1}{c}{}
                &  $x$  \\
    \hline
    \end{tabular}
\end{center}
\end{frame}
\end{document}

enter image description here

4
  • Wow! This is the simplest one so far.
    – user338955
    Commented Apr 21, 2023 at 15:26
  • 1
    @user338955, many times some old, many times already seem to be forgotten packages, make writing of table simply :-)
    – Zarko
    Commented Apr 21, 2023 at 15:37
  • @DavidCarlisle, in comparison to a some new packages like tabularray. I should write "...some good old, ...",
    – Zarko
    Commented Apr 21, 2023 at 22:24
  • I just asked a follow-up question based on this answer: tex.stackexchange.com/q/683580/200958
    – user338955
    Commented Apr 22, 2023 at 2:22
3

A little patch to a simple tabular with the help of array to play with the intercolumn spaces (@{}):

mwe

\documentclass{article}
\usepackage{array}
\begin{document}\sffamily\extrarowheight2pt
\begin{tabular}{|c|c|@{}p{2pt}@{}|l|}
\cline{2-4}
\multicolumn{1}{c|}{}&M2&&a\\\hline
M1&M3&&\\\cline{1-2}
\multicolumn{3}{c}{\vspace*{-12pt}}&%
\multicolumn{1}{@{\kern-.4pt\rule[8pt]{.4pt}{2.6pt}}c|}{}\\\cline{1-3}
b & \multicolumn{2}{c}{} &  x \\
\hline
\end{tabular}
\end{document}

You must log in to answer this question.

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