103

I want to create a table like this using Latex

      ---------------------------------
      |               sets            |
      ---------------------------------
      | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
---------------------------------------
| I1  | * |   | * | * |   | * |   | * |
---------------------------------------

Here is what I wrote

\begin{table}
\caption{Multiprogram sets}
\label{multiprogram}
 \begin{tabular}{| c | c | c | c | c | c | c | c | c |}
  \cline{3-9}
  \multicolumn{8}{}{}    &   Sets  \\
  \hline
  \multicolumn{1}{}{}   & 1 & 2 &  3 & 4 & 5 &  6 & 7  & 8 \\
  \hline
   astar         &  & * &  & * &  &  & * &   \\
  \hline
 \end{tabular}
\end{table}

But it doesn't work! The output look like

          -----------------------------
                                 sets |
---------------------------------------
        1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
---------------------------------------
| I1  | * |   | * | * |   | * |   | * |
---------------------------------------

Any way to fix that?

1
  • 11
    It should be \multicolumn{1}{c|}{} & \multicolumn{8}{c|}{Sets} \\
    – egreg
    Sep 6, 2013 at 11:13

4 Answers 4

130

Code:

\documentclass{article}
\begin{document}
    \begin{table}
        \centering
        \caption{Multiprogram sets}
        \label{multiprogram}
        \begin{tabular}{c|c|c|c|c|c|c|c|c|}
            \cline{2-9}
             & \multicolumn{8}{|c|}{Sets}\\
            \cline{2-9}
             & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8\\
            \hline
            \multicolumn{1}{|c|}{astar} & & * &  & * &  &  & * &\\
            \hline
        \end{tabular}
    \end{table}
\end{document}

And the result:

enter image description here

0
11

I propose using https://www.tablesgenerator.com/ it is really saving you a lot of time and it is intuitive to use! Just go ahead and try it out!

enter image description here

2
  • Far from complete. Import from LaTeX code doesn't work at all.
    – Urhixidur
    Feb 18, 2022 at 21:56
  • This is reasonably good for quick 'n' dirty, but produces nasty code for larger documents/class development.
    – Colin
    May 29, 2023 at 7:16
3

With {NiceTabular} of nicematrix, you only have to use the keys hvlines and corners=NW (NW stands for north west) and all the expected rules will be drawn.

\documentclass{article}
\usepackage{nicematrix}
\begin{document}
\begin{table}
\centering
\caption{Multiprogram sets}
\label{multiprogram}
\begin{NiceTabular}{*{9}{c}}[hvlines,corners=NW]
      & \Block{1-8}{Sets}\\
      & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8\\
astar & & * &  & * &  &  & * &  \\
\end{NiceTabular}
\end{table}
\end{document}

Ouput of the above code

0
1

An alternative solution with tabularray package:

\documentclass{article}
\usepackage{caption}
\usepackage{tabularray}
\begin{document}
    
\begin{table}
  \centering
  \caption{Multiprogram sets}
  \label{multiprogram}
  \begin{tblr}{
    hline{1,2} = {2-Z}{solid},
    hline{3,4} = {solid},
    vline{1} = {Z}{solid},
    vline{2-Z} = {solid},  % Z stands for the last
    cells = {c},
    cell{1}{2} = {c=8}{c}, % multicolumn
  }
          & Sets &   &   &   &   &   &   &   \\
          & 1    & 2 & 3 & 4 & 5 & 6 & 7 & 8 \\
    astar &      & * &   & * &   &   & * &   \\
  \end{tblr}
\end{table}

\end{document}

enter image description here

You must log in to answer this question.

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