12

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}
ben
  • 616

3 Answers3

20

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:

enter image description here

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.

Note:

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}

Peter Grill
  • 223,288
12
\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}

enter image description here

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}

enter image description here

1

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

Output of the first code

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}

Output of the above code

F. Pantigny
  • 40,250