5

I am trying to create a red box that covers multiple rows and columns for highlighting sections of a table.

I have created a MWE using @Steven B. Slegetes answer to this question. The code is:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{graphicx}
\usepackage{tabularx}
\usepackage{booktabs}
\usepackage[table]{xcolor}
\usepackage{multirow}

%Create Red Boxes, Thanks Steven \def\boxit#1{% \smash{\color{red}\fboxrule=1pt\relax\fboxsep=2pt\relax% \llap{\rlap{\fbox{\vphantom{0}\makebox[#1]{}}}~}}\ignorespaces }

\begin{document}

\begin{table}[h] \caption{Red Box Test} \vspace{0.5em} \centering \begin{tabular}{cccc} & A & B & C \\toprule I & 1 & 2 & 3 \ \boxit{0.4in} J & 4 & 5 & 6 \ K & 7 & 8 & 9 \\bottomrule \end{tabular} \end{table}

Which creates the following table:

enter image description here

Question: How do I get this to cover the row that begins with K as well as the row with J? I figured it must have something to with the multirow package, hence my including it. I have tried playing around with it to no avail.

Brennan
  • 199

4 Answers4

7

One way is to use tikz with its tikzmark library, which can mark arbitrary points on a page and then draw pictures based on the coordinates of the marked points. An example:

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

\begin{document}

\begin{table}[h] \caption{Red Box Test} \vspace{0.5em} \centering \begin{tabular}{cccc} & A & B & C \\toprule I & 1 & 2 & 3 \ \tikzmark{J}J & 4 & 5 & 6 \ K & 7\tikzmark{7} & 8 & 9 \\bottomrule \end{tabular} \begin{tikzpicture}[overlay,remember picture] \draw[red] ([shift={(-1ex,2ex)}]pic cs:J) rectangle ([shift={(1ex,-0.5ex)}]pic cs:7); \end{tikzpicture} \end{table} \end{document}

The result:

enter image description here

7

I have edited this answer because, since version 5.5 (2020-10-20), one must no longer write \omit\CodeAfter here (now, \CodeAfter works in all circonstancies).


You can do that with {NiceTabular} of nicematrix. This environment creates PGF/Tikz nodes under the cells of the array and it's possible to use that nodes after the construction of the array.

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

\begin{document}

\begin{table}[h] \caption{Red Box Test} \vspace{0.5em} \centering \begin{NiceTabular}{cccc} & A & B & C \\toprule I & 1 & 2 & 3 \ J & 4 & 5 & 6 \ K & 7 & 8 & 9 \\bottomrule \CodeAfter \tikz \node [draw=red, fit = (3-1) (4-2)] { } ; \end{NiceTabular} \end{table}

\end{document}

Output of the above code

F. Pantigny
  • 40,250
5

A (very) small variation of the @Sergei Golovan answer. From the tikzmark library is used \tikzmarknode macro, box is drawn by node which fit marked nodes:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{fit,            % new
                tikzmark}       % new
\tikzset{FIT/.style = {draw=red, thick, inner ysep=2pt, fit=#1}} % new
\usepackage{booktabs}
\usepackage[skip=0.33\lineskip]{caption} % new

\begin{document} \begin{table}[ht] \caption{Red Box Test} \label{tab:redbox} \centering \begin{tabular}{cccc} & A & B & C \ \toprule I & 1 & 2 & 3 \ \tikzmarknode{J}J & 4 & 5 & 6 \ % <--- K & 7\tikzmarknode{7} & 8 & 9 \ % <--- \bottomrule \end{tabular} \begin{tikzpicture}[overlay,remember picture] \node[FIT=(J) (7)] {}; % <--- \end{tikzpicture} \end{table} \end{document}

enter image description here

Zarko
  • 296,517
2

A solution with pstricks: two empty nodes, and a \psframe joining these nodes.

\documentclass[svgnames]{article}
\usepackage{graphicx}
\usepackage{tabularx}
\usepackage{booktabs}
\usepackage[table]{xcolor}
\usepackage{multirow}
\usepackage{pst-node}

\begin{document}

\begin{table}[h] \caption{Red Box Test} \vspace{0.5em} \centering \begin{tabular}{cccc} & A & B & C \\toprule I & 1 & 2 & 3 \ \pnode[-1.5ex, 2.5ex]{A}J & 4 & 5 & 6 \ K & 7\pnode[1.5ex, -1ex]{B} & 8 & 9 \\bottomrule \end{tabular} \psframelinecolor=Coral(B) \end{table}

\end{document}

enter image description here

Bernard
  • 271,350