# Lines between array cells

I'm looking for a natural way to selectively add horizontal and vertical lines between the cells of an array.

Here is an example.

 +-----------+
| A   B   C | D   E
|           +-------+
| F   G   H | I   J |
+-----------+       |
K   L   M  | N   O |
|       |
P   Q   R  | S   T |
+-------+


I know that I could use multicolumn and multirow commands to achieve that, but it is neither efficient nor elegant, and the resulting code looks horrible.

As every reasonably modern WYSIWYG word processor has an intuitive way to add such lines, I'm sure there is a comparably comfortable method in LaTeX. I just don't know it yet.

• Look at the package nicematrix. Jul 13, 2021 at 20:09
• Do you need the dashed lines (with different dash sizes)? Jul 13, 2021 at 20:31
• tex.stackexchange.com/q/462753/134144 may be interesting. Jul 13, 2021 at 20:33
• @projetmbc thank you for the pointer to nicematrix. That looks pretty powerful. Jul 13, 2021 at 20:59
• @Bernard it is not about dashed lines. Jul 13, 2021 at 21:00

The nicematrix package combines very well with tikz.

Notice that the original data : A, B, etc. are "untouched"

\documentclass{article}

\usepackage{nicematrix}
\usepackage{tikz}

\begin{document}

\begin{NiceTabular}{ccccc}[cell-space-limits=5pt] % expand the cells
A&B&C&D&E\\
F&G&H&I&J\\
K&L&M&N&O\\
P&Q&R&S&T
\CodeAfter
\tikz \draw [dotted] (1-|1) -- (1-|4);
\tikz \draw [dotted] (3-|1) -- (3-|4);
\tikz \draw [dashed] (2-|4) -- (2-|6);
\tikz \draw [dashed] (5-|4) -- (5-|6);
\tikz \draw  (1-|1) -- (3-|1);
\tikz \draw  (1-|4) -- (6-|4);
\tikz \draw  (2-|6) -- (5-|6);
\end{NiceTabular}
\end{document}


Alternative

With the tabularray package, the style of the table is totally separated from the content. (tabularray)

Also adds extra space above and below the table rows to make the table look better.

\documentclass{article}

\usepackage{tabularray}

\begin{document}

\begin{tblr}{% style definition
vline{1} = {1-2}{solid},
vline{4} = {1-5}{solid},
vline{6} = {2-5}{solid},
hline{1} = {1-3}{dotted},
hline{3} = {1-3}{dotted},
hline{2} = {4-6}{dashed},
hline{5} = {4-6}{dashed},
}
A&B&C&D&E\\
F&G&H&I&J\\
K&L&M&N&O\\
P&Q&R&S&T\\
\end{tblr}

\end{document}

• It may be simpler to write hline{1,3} = {1-3}{dotted} and hline{2,5} = {4-6}{dashed}. Jul 14, 2021 at 1:13
• This is an artificial example. For the actual work, I found the independence between content and style to be more relevant. In my workflow, I do calculations, queries, and sorting outside of LaTeX and then import the data for publishing only. It is not unusual for the data to change between drafts and the final version. Jul 14, 2021 at 13:15

\documentclass{article}
\begin{document}
\begin{table}[h]
\begin{tabular}{ccc|cc}
\cline{1-3}
\multicolumn{1}{|c}{A} & B & C & D & E                      \\ \cline{4-5}
\multicolumn{1}{|c}{F} & G & H & I & \multicolumn{1}{c|}{J} \\ \cline{1-3}
K                      & L & M & N & \multicolumn{1}{c|}{O} \\
P                      & Q & R & S & \multicolumn{1}{c|}{T} \\ \cline{4-5}
\end{tabular}
\end{table}
\end{document}


This is generated from https://www.tablesgenerator.com

• Thank you. Didn't know tablesgenerator.com. The resulting code is better than what I thought when I wrote "I know that I could use multicolumn and multirow commands to achieve that but it is neither efficient nor elegant, and the resulting code looks horrible.". But still... Jul 13, 2021 at 21:01
• @azimut, you are welcome. Jul 13, 2021 at 21:21

Use hhline and some abbreviations.

\documentclass{article}
\usepackage{hhline}

\newcommand{\RR}[1]{\multicolumn{1}{c|}{#1}}
\newcommand{\LR}[1]{\multicolumn{1}{|c}{#1}}

\begin{document}

\begin{tabular}{ccccc}
\hhline{|---|~~}
\LR{A} & B & \RR{C} & D & E \\
\hhline{|~~~|--|}
\LR{F} & G & \RR{H} & I & \RR{J} \\
\hhline{|---|~~|}
K      & L & \RR{M} & N & \RR{O} \\
P      & Q & \RR{R} & S & \RR{T} \\
\hhline{~~~|--|}
\end{tabular}

\end{document}


In the argument to \hhline a hyphen means “rule in this column”, a tilde means “no rule”. With | one specifies a “meeting” of rules.

A solution with nicematrix have been given by Simon Dispa. Here is another solution with nicematrix. It uses the command \Block. That command has been designed to merge cells (horizontally and vertically) and put contents at the center of the resulting rectangle. However, it's possible to use it without content in order to use only the key draw.

\documentclass{article}
\usepackage{nicematrix}

\begin{document}

\begin{NiceTabular}{ccccc}[cell-space-limits=5pt]
\Block[draw]{2-3}{}
A & B & C & D & E \\
F & G & H & \Block[draw]{3-2}{}
I & J \\
K & L & M & N & O \\
P & Q & R & S & T
\end{NiceTabular}

\end{document}