# Framing cells vertically in a table

I want to add a box in a tabularx table that would frame several cells vertically. There is this post that shows how to frame cells horizontally (Framing cells in a table), but I cannot figure out how to do to have the box extend over several rows. Here is what I would roughly like to get (but with a nice box):

Column 1 & Column 2 & Column 3
+-----+
bla      & bla      & |bla  |
|     |
bla      & bla      & |bla  |
+-----+


Any idea of how I could do that?

Edit: the solutions proposed below are very nice. But they are compatible with the tabular environment only, it seems, and not with tabularx and booktabs. Below is the code I have used. Does anyone have an idea of how to extend these solutions to the tabularx style? If not, then I will just rewrite all my tables using the simple tabular style.

   \documentclass{article}
\usepackage{tabularx, booktabs}
\usepackage{tikz}
\usetikzlibrary{calc}

\newcommand{\tikzmark}[1]{\tikz[overlay,remember picture] \node (#1) {};}
\newcommand{\DrawBox}[1][]{%
\tikz[overlay,remember picture]{
\draw[black,#1]
($(left)+(-0.5em,2.0ex)$) rectangle
($(right)+(0.75em,-0.75ex)$);}
}

\begin{document}
\begin{table}[H]
\centering
\begin{tabularx}{0.3\textwidth}{Y*{4}{c}}
\toprule
& Masc  & Fem & Neut\\
\midrule  \cline{4-4}
Nom & hic & haec & \multicolumn{1}{|c|}{hoc} \\
Acc & hunc & hanc & \multicolumn{1}{|c|}{hoc} \\ \cline{4-4}
\bottomrule
\end{tabularx}
\end{table}
\begin{table}[H]
\centering
\begin{tabularx}{0.3\textwidth}{Y*{4}{c}}
\toprule
& Masc  & Fem & Neut\\
\midrule
Nom & hic & haec & \tikzmark{left}hoc \\
Acc & hunc & hanc & hoc\tikzmark{right} \\
\bottomrule
\end{tabularx}
\end{table}
\end{document}

• Please always provide a complete test document, but you can do the horizontal parts with \cline and the vertical parts with | Mar 26 '14 at 22:07
• Both solutions are compatible with tabularx. Show a complete MWE that is not working. Mar 27 '14 at 3:55
• You are noy using \DrawBox in your second code, so the box is not drawn. Add \DrawBox just before \end{table}. Mar 27 '14 at 13:58
• Have updated solution which provides for a more flexible \DrawBox macro (using booktabs), along with some usage notes. This will work just a well with tabularx. Mar 28 '14 at 19:51

\documentclass{article}

\begin{document}
\begin{tabular}{*6{c}}\hline
Col 1 & Col 2 & Col 3 & Col 4 & Col 5 & Col 6 \\\cline{1-1}
\multicolumn{1}{|c|}{bla}   & bla   & bla   & bla   & bla   & bla \\
\multicolumn{1}{|c|}{bla}   & bla   & bla   & bla   & bla   & bla \\
\multicolumn{1}{|c|}{ble} & ble & ble & bla & bla & bla \\ \cline{1-1}
bla   & bla   & bla   & bla   & bla   & bla \\ \hline
\end{tabular}
\end{document}


With tabularx and booktabs:

   \documentclass{article}
\usepackage{tabularx, booktabs}
\begin{document}
\begin{table}[H]
\centering
\begin{tabularx}{0.4\textwidth}{X*{4}{c}}      %% 0.3\textwidth too narrow
%% and Y column type not known
\toprule
& Masc  & Fem & Neut\\
\midrule  \cline{4-4}
Nom & hic & haec & \multicolumn{1}{|c|}{hoc} \\
Acc & hunc & hanc & \multicolumn{1}{|c|}{hoc} \\ \cline{4-4}
\bottomrule
\end{tabularx}
\end{table}
\end{document}


There is always the tikz solution. Here is one that use bascially the same code from Highlight elements in the matrix but with a more flexible \DrawBox macro. And since it is tikz you get all the additional flexibility that comes with that:

## Usage:

1. You use \tikzmark{<name>} to "mark" a particular location with <name>.
2. These named locations are passed to the \DrawMacro which uses these are the boundary of the drawing. You need one \DrawBox call for each box that you wish to draw.

## Code:

\documentclass{article}
\usepackage{tikz}
\usepackage{booktabs}
\usetikzlibrary{calc}

\newcommand{\tikzmark}[1]{\tikz[overlay,remember picture] \node (#1) {};}
\newcommand{\DrawBox}[3][]{%
\tikz[overlay,remember picture]{
\draw[black,#1]
($(#2)+(-0.5em,2.0ex)$) rectangle
($(#3)+(0.75em,-0.75ex)$);}
}

\begin{document}
\begin{tabular}{*{7}{c}}\toprule
Col 1 &  Col 2                   &       Col  3                 & Col 4                   & Col 5 & Col 6                         & Col 7 \\
\cmidrule(lr){2-6}
0    &   1                      & \tikzmark{top left 1}abcd    &   0                     &   0   &   0                           &   1   \\
1    &   \tikzmark{top left 3}0 & www                          &   0                     &   0   &   0                           &   1   \\
0    &   1                      & xyz\tikzmark{bottom right 1} &  \tikzmark{top left 2}1 &   0   &   0                           &   0   \\
0    &   0                      & 1                            &   0                     &   0   &   0\tikzmark{bottom right 3}  &   0   \\
0    &   0                      & 0                            &   0                     &   0   &   1                           &   0\tikzmark{bottom right 2} \\
0    &   0                      & 1                            &   0                     &   0   &   0                           &   0 \\
\bottomrule
\end{tabular}

\DrawBox[ultra thick, red]{top left 1}{bottom right 1}
\DrawBox[ultra thick, blue]{top left 2}{bottom right 2}
\DrawBox[ultra thick, draw=green, dotted, fill=yellow!15, fill opacity=0.3]{top left 3}{bottom right 3}
\end{document}


In the environment {NiceTabular} of nicematrix, you have a command \Block which can merge cells but which can also be used to draw a frame around a rectangle of cells.

\documentclass{article}
\usepackage{nicematrix}

\begin{document}

\begin{NiceTabular}{ccc}
Column 1 & Column 2 & Column 3 \\
bla & bla & \Block[draw=red,rounded-corners]{2-1}{} bla \\
bla & bla & bla
\end{NiceTabular}

\end{document}


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

If you prefer a frame adjusted to the contents of the cells, you can do that with Tikz in the \CodeAfter by using the PGF/Tikz nodes created by nicematrix under the cells, rows and columns.

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

\begin{document}

\begin{NiceTabular}{ccc}
Column 1 & Column 2 & Column 3 \\
bla & bla & bla \\
bla & bla & bla
\CodeAfter
\tikz \node [draw=red,rounded corners,fit=(2-3)(3-3)] {} ;
\end{NiceTabular}

\end{document}