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.

emnha
  • 445
  • 3
  • 11

3 Answers3

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}

Celdor
  • 9,058
  • 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? https://ibb.co/g7kTy0T – emnha Aug 18 '22 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 '22 at 16:16
  • You're right. It works now. How would you fix the width size of columns and rows? – emnha Aug 18 '22 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 '22 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 '22 at 16:49
  • This is done in colspec, e.g. colspec={Q[4em,c] *5{Q[2.5em,c]}}. Expand *5 to individual column spec. if you need every one of them to have an individual width. Besides c is equivalent to Q[c], which is used by tabularray in case you want even more tunings. – Celdor Aug 18 '22 at 17:00
  • If I add two more columns like the first row & V_1 & V_2 & V_3 & V_4 & V_5 & V_6 & V_7\\ then I get a warning like this from Overleaf: "Package tabularray Warning: Table width is too small, need 12.20003pt more!". Do you have any idea to remove this warning? – emnha Aug 18 '22 at 20:32
  • It's because the document class IEEEtran sets two column layout and if your table doesn't fit a column width you get a warning. You have to reduce the table width somehow or enclose it within table* (starred) instead of regular table environment. – Celdor Aug 18 '22 at 21:29
  • The table will no longer flow with text and will be placed at the top of the next page but will span two columns and fit the whole page width. – Celdor Aug 18 '22 at 21:31
  • Thanks, I solved it by reducing the column's width. – emnha Aug 18 '22 at 22:25
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

F. Pantigny
  • 40,250
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

MadyYuvi
  • 13,693