6

I am looking to add borders to some cells in a table, such that the borders surround the contents and there is a space between each cell. The best image I could find that shows sort of what I want to achieve is this:

enter image description here

This is possible in HTML via the cellspacing setting in tables. How can I achieve the same in LaTeX? I would only want a few of the table cells to have this internal border, not all of them.

The closest I have come is by using fbox, but I couldn't get them to extend all the way to the edges of the table cells.

Bill Cheatham
  • 2,323
  • 1
  • 20
  • 18

2 Answers2

6

For simple tables an easy solution is the hhline package, but I think that is not a good solution for complex tables with \multiwrow and \multicolumn cells, as showed in your question.

In this MWE are showed both problems:

enter image description here

\documentclass{article}
\usepackage[table]{xcolor}
\usepackage{hhline}
\usepackage{multirow}
\begin{document}
\arrayrulecolor{black} 
\doublerulesepcolor{gray!40} 
\begin{tabular}{||>{\columncolor[gray]{0.9}\bfseries}c||c||c||c||c||}
\hhline{|t:=:t:=:t:=:t:=:t:=:t|}
\rowcolor{red!30}
\multirow{2}{*}{alfa} & \bfseries beta & \multicolumn{2}{|c||}{\bfseries beta} & \bfseries alfa \\
\hhline{||~|:=::=::=::=:|}
& beta & alfa & beta & alfa \\
\hhline{|:=::=::=::=::=:|}
alfa & beta & alfa & beta & alfa \\
\hhline{|:=::=::=::=::=:|}
alfa & beta & alfa & beta & alfa \\
\hhline{|:=::=::=::=::=:|}
\rowcolor{green!30} gamma & delta & gamma & delta & gamma \\
\hhline{|b:=:b:=:b:=:b:=:b:=:b|}
\end{tabular}
\end{document}
Moriambar
  • 11,466
Fran
  • 80,769
  • You can put multirow in the second row: & \bfseries beta & \multicolumn{2}{|c||}{\bfseries beta} & \bfseries alfa \\hhline{||~|:=::=::=::=:|} \multirow{-2}{*}{alfa}& beta & alfa & beta & alfa \\ so that the text doesn't get clipped. –  Apr 10 '13 at 23:52
  • @HarishKumar, yes, much better in this way. But the multirow cell still have 3 colors. Too much for my taste! – Fran Apr 11 '13 at 00:36
0

Here is a solution with {NiceTabular} of nicemtrix.

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

\begin{document}

\newcommand{\B} { \Block [
tikz = clip , tikz = { draw, line width=3pt } , tikz = { draw, color = gray!10, line width = 2.5pt} ] }

\NiceMatrixOptions{cell-space-limits=5pt}

\begin{NiceTabular}{>{\bfseries}ccccc} \CodeBefore \arraycolor{lightgray} \rectanglecolor{white}{3-2}{6-5} \Body \RowStyle[nb-rows=2]{\bfseries} \B{2-1}{Question} & \B{1-2}{Women} && \B{1-2}{Men} \ & \B{}{Yes} & \B{}{No} & \B{}{Yes} & \B{}{No} \ \B{}{Question 1} & \B{}{42%} & \B{}{58%} & \B{}{61%} & \B{}{39%} \ \B{}{Question 2} & \B{}{53%} & \B{}{47%} & \B{}{69%} & \B{}{31%} \ \B{}{Question 3} & \B{}{26%} & \B{}{74%} & \B{}{51%} & \B{}{49%} \ \RowStyle{\bfseries} \B{}{Average} & \B{}{40%} & \B{}{60%} & \B{}{60%} & \B{}{40%} \end{NiceTabular}

\end{document}

Output of the above code

You need several compilations.

F. Pantigny
  • 40,250