5

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.

azimut
  • 863
  • 10
  • 18

4 Answers4

12

The nicematrix package combines very well with tikz.

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

c

\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.

D

\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}

Simon Dispa
  • 39,141
  • 2
    It may be simpler to write hline{1,3} = {1-3}{dotted} and hline{2,5} = {4-6}{dashed}. – L.J.R. Jul 14 '21 at 01: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. – Simon Dispa Jul 14 '21 at 13:15
7

How about the following code?

\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}

enter image description here

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

enter image description here

citsahcots
  • 7,992
  • 1
    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... – azimut Jul 13 '21 at 21:01
  • @azimut, you are welcome. – citsahcots Jul 13 '21 at 21:21
4

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.

enter image description here

egreg
  • 1,121,712
3

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}

Output of the above code

F. Pantigny
  • 40,250