3

How can I create a table with empty corner and continous vertical lines like this?

enter image description here

Here is what I have so far:

enter image description here

\documentclass[journal]{IEEEtran}
\usepackage{mathtools}
\usepackage{booktabs}

\begin{document}
  \begin{table}[!t]
  \centering
  \caption{Adjacency Matrix}
  \label{tab:Adjacency Matrix}
  \begin{tabular}{|c|c|c|c|c|c|}
  \toprule
$V_1$ & $V_2$ & $V_3$ & $V_4$ & $V_5$ \\
\midrule
$V_1$ \\
\midrule
$V_2$ \\
\midrule
$V_3$ \\
\midrule
$V_4$ \\
\midrule
$V_5$ \\
\bottomrule
\end{tabular}
\end{table}
\end{document}

Also please suggest if you have a nice idea to do it.

5
  • tex.stackexchange.com/a/640176/197451
    – js bibra
    Aug 18, 2022 at 14:55
  • Regarding the gaps between horizontal rules when using booktabs, have a look at this question
    – Celdor
    Aug 18, 2022 at 15:12
  • @jsbibra that is nice. Can I use booktab style for the nicetablular? I tried but couldn't make it work with \toprule.
    – emnha
    Aug 18, 2022 at 15:14
  • @Celdor I see, l'll check \cmidrule.
    – emnha
    Aug 18, 2022 at 15:15
  • @emnha Well, I thought you would like to know what happens with the gaps. I just post a solution with tabularray, which also accepts booktabs.
    – Celdor
    Aug 18, 2022 at 15:50

3 Answers 3

4

I would suggest to use tabularray for this kind of tables or NiceMatrix but I am not familiar with the latter, so I give a solution based on tabularray

enter image description here

\documentclass[journal]{IEEEtran}
\usepackage{mathtools}
\usepackage{tabularray}
    \UseTblrLibrary{booktabs}


\begin{document}
  \begin{table}[!t]
    \centering
    \caption{Adjacency Matrix}
    \label{tab:Adjacency Matrix}
    \begin{tblr}{
            colspec={*6{c}},
            vline{1} = {2-Z}{0.8pt},
            vline{2,Z} = {0.8pt},
            vline{3-Y} = {0.3pt},
            hline{1} = {2-Z}{0.8pt},
            hline{2,Z} = {0.8pt},
            hline{3-Y} = {0.3pt},
            row{1} = {mode=math},
            column{1} = {mode=math},
        }
        & V_1 & V_2 & V_3 & V_4 & V_5 \\
        V_1 & 1 & 1 & 0 & 0 & 1 \\
        V_2 & 1 & 0 & 1 & 0 & 1 \\
        V_3 & 0 & 1 & 0 & 1 & 0 \\
        V_4 & 0 & 0 & 1 & 0 & 1 \\
        V_5 & 1 & 1 & 0 & 1 & 0 \\
    \end{tblr}
  \end{table}
\end{document}
10
  • It looks very nice but somehow I got a lot of errors in Overleaf. Also how would you fix the width size for every columns and rows? ibb.co/g7kTy0T
    – emnha
    Aug 18, 2022 at 16:11
  • I am afraid Overleaf is still not up-to-date and tabularray is still not available. However, you can download the zip-file, extracts the tabularray.sty and upload it in your project on Overleaf. Then it will work.
    – Celdor
    Aug 18, 2022 at 16:16
  • You're right. It works now. How would you fix the width size of columns and rows?
    – emnha
    Aug 18, 2022 at 16:18
  • It's a typo in my code. Change 5 to 6 in colspec={*5{c}}, ... and it will work
    – Celdor
    Aug 18, 2022 at 16:23
  • It still works but for now the width of columns and rows fits the content. I want to make them fixed instead.
    – emnha
    Aug 18, 2022 at 16:49
3

It's easy with the environment {NiceTabular} of nicematrix.

\documentclass{article}
\usepackage{nicematrix}

\begin{document}

\renewcommand{\arraystretch}{1.2}
\begin{NiceTabular}{>{\bfseries}cccccc}[hvlines,corners=NW] % NW = north west 
\RowStyle{\bfseries}
  & 1 & 2 & 3 & 4 & 5 \\
1 &   &   & 1 & 1 &   \\
2 &   &   &   & 1 & 1 \\
3 & 1 &   &   &   & 1 \\
4 & 1 & 1 &   &   &   \\
5 &   & 1 & 1 &   &   \\
\end{NiceTabular}

\end{document}

You need several compilations (because nicematrix uses PGF/Tikz nodes under the hood).

Output of the above code


Answer to the comment. Usually, one uses the rules of \booktabs (\toprule, midrule, \bottomrule, etc.) only with a design without any vertical rule...

However, if you want to add rules with the thickness of a \toprule of booktabs (equal to \heavyrulewidth), it's possible with nicematrix by defining a custom style of line with the key custom-line:


\documentclass{article}
\usepackage{nicematrix,booktabs,tikz}

\begin{document}

\NiceMatrixOptions
  {
    custom-line = 
     {
       command = boldhline ,
       ccommand = cboldhline ,
       tikz = { line width = \heavyrulewidth } , 
       total-width = \heavyrulewidth 
     }
  }

\renewcommand{\arraystretch}{1.2}
\begin{NiceTabular}{>{\bfseries}cccccc}[hvlines,corners=NW] % NW = north west 
\cboldhline{2-last}
\RowStyle{\bfseries}
  & 1 & 2 & 3 & 4 & 5 \\
1 &   &   & 1 & 1 &   \\
2 &   &   &   & 1 & 1 \\
3 & 1 &   &   &   & 1 \\
4 & 1 & 1 &   &   &   \\
5 &   & 1 & 1 &   &   \\
\boldhline
\end{NiceTabular}

\end{document}

You need several compilations (because nicematrix uses PGF/Tikz nodes under the hood).

Output of the above code

2
  • Yeah, I tried this but then I wanted to apply the booktabs style like toprule, midrule and bottom rule but I couldn't add toprule as it extends all over 6 columns.
    – emnha
    Aug 18, 2022 at 16:11
  • 1
    I have modified my answer. Aug 18, 2022 at 16:24
2

Hope this may helps you:

\documentclass[journal]{IEEEtran}
\usepackage{mathtools}
\usepackage{booktabs}

\begin{document}
  \begin{table}[!t]
  \centering
  \caption{Adjacency Matrix}
  \label{tab:Adjacency Matrix}
  \begin{tabular}{|c|c|c|c|c|}
\cline{2-5}
\multicolumn{1}{c|}{$V_1$} & $V_2$ & $V_3$ & $V_4$ & $V_5$ \\
\hline
$V_1$ & & & & \\
\hline
$V_2$ & & & &\\
\hline
$V_3$ & & & &\\
\hline
$V_4$ & & & &\\
\hline
$V_5$ & & & &\\
\hline
\end{tabular}
\end{table}

\end{document}

enter image description here

2
  • Thanks but the node V1 is not correct in the first row. Also can I use the booktab style here as well?
    – emnha
    Aug 18, 2022 at 15:16
  • 1
    @emnha If you want to use the booktabs then try with the tag \specialrule
    – MadyYuvi
    Aug 18, 2022 at 15:23

You must log in to answer this question.

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