# Highlight a table row with a rectangular overlay

I am trying to highlight the row of a table by drawing a rectangle over that row. A wrinkle is that the code for the table is pre-written from external statistical software and then included in the main document using the \input command. For this reason, I'd like to avoid placing code within the table, if at all possible. I have included a simple example:

\documentclass{article}

\begin{document}

\begin{center}
\input{table.tex}
\end{center}

\end{document}


And the code for table.tex is:

\begin{tabular}{l*{3}{c}}
& A & B & C \\
\hline
1 & blah & blah & blah \\
2 & blah & blah & blah \\
3 & blah & blah & blah \\
\hline
\end{tabular}


I'd like to add a red rectangle (perhaps rounded) around the second row, possibly using tikz or some other solution. If it matters, the table will ultimately be in a beamer slide, but I figured that including that code was not necessary.

-
Do you have the capability of adding code to the table that is exported from the "external statistical software" package? So, instead of blah, you output \somecode{stuff>}blah? –  Werner Apr 16 '12 at 15:41
I can go into table.tex and add the code. However, because the statistical process is rerun over and over again, I would like to avoid that if possible. I don't expect the spacing of the table to change much, e.g. there will always be 4 digit numbers in the cells, albeit different digits. So, if a rectangle is overlaid at the right coordinates once, I wouldn't have to change the code much thereafter, if that makes any sense. –  profj Apr 16 '12 at 15:46
Could you make the statistics software output just the raw numbers, without the LaTeX code? Then you could use pgfplotstable to typeset the table and add highlighting using the row number, or a keyword/key value. –  Jake Apr 16 '12 at 16:35
Hi Jake, not sure I could use that solution, because the statistics software outputs table headings, and other labels, and sometimes latex code, not just numbers. –  profj Apr 16 '12 at 16:43
@stanford202: Ah, too bad. Never mind then. –  Jake Apr 16 '12 at 16:49

You could put the \input in a node and draw a rectangle relative to the corners of the node.

\begin{filecontents*}{table.tex}
\begin{tabular}{l*{3}{c}}
& A & B & C \\
\hline
1 & blah & blah & blah \\
2 & blah & blah & blah \\
3 & blah & blah & blah \\
\hline
\end{tabular}
\end{filecontents*}
\documentclass{beamer}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\begin{frame}
\begin{center}
\begin{tikzpicture}
\node (table) {\input{table.tex}};
\draw [red,ultra thick,rounded corners]
($(table.south west) !.3! (table.north west)$)
rectangle
($(table.south east) !.5! (table.north east)$);
\end{tikzpicture}
\end{center}
\end{frame}
\end{document}


## Some explanation to the above code

There are several standard anchors on a node by default, denoted by compass directions, accessed with the syntax <node name>.<anchor>. For example will MyNode.north west be the top left corner of MyNode.

To get a coordinate between two existing coordinates you can use the TikZ library calc. This is described in section 13.5 Coordinate Calculations of the manual. It provides the syntax

($(<nodename or coordinate>) ! <factor> ! (<nodename or coordinate>)$)


Dollar signs indicate that a coordinate calculation should be done. The <factor> is a number saying how far, on the line between the two specified nodes, relative to the distance between the nodes, the point should be. For example,

($(0,0) !.3! (1,0)$)


is 30% of the way between (0,0) and (1,0), meaning (0.3,0).

In other words,

\draw [red,ultra thick,rounded corners]
($(table.south west) !.3! (table.north west)$)
rectangle
($(table.south east) !.5! (table.north east)$);


draws a rectangle from the point that is 30% of the distance from the lower left corner to the upper left corner, to the point that is halfway between the lower right and upper right corners.

-
Thank you for the solution. Is there a reference in the tikz manual that I can use to decode your definition of the coordinates for the rectangle? This is for cases where the table is a different size, etc. –  profj Apr 16 '12 at 16:19
@stanford202: TikZ is part of pgf and is described inside the pgf documentation. So texdoc pgfmanual.pdf should open it. –  Speravir Apr 16 '12 at 17:14
Great, thanks for the explanation. That makes perfect sense. Yes, I meant to say the pgf manual. –  profj Apr 16 '12 at 17:35

Here's a sans-TikZ way of obtaining the rectangle via a coloured \fbox:

\documentclass{article}
\usepackage{xcolor}% http://ctan.org/pkg/xcolor
\newsavebox{\mytable}
\begin{document}

\begin{center}
\begin{lrbox}{\mytable}
\input{table.tex}
\end{lrbox}
%\setlength{\fboxrule}{2pt}% Modify rule width if necessary
\ooalign{%
\hss\usebox{\mytable}\hss \cr
{\color{red}%
\fbox{\phantom{\rule[-5pt]{\dimexpr\wd\mytable+10pt}{\dimexpr\baselineskip+5pt}}}%
}
}
\end{center}

\end{document}​


The table \input is stored in a box \mytable in order to obtain the width. The box is subsequently positioned with a 5pt overlap on either side of the table by setting a blank box of appropriate dimensions inside the coloured \fbox.

Within \ooalign, the table is centrred (since it's less wide than the 10pt-wider \fbox) using \hss...\hss. This is overprint with the coloured \fbox. Here is a quick course on \ooalign.

You could also reverse the sequence to use a shaded highlighting:

\documentclass{article}
\usepackage{xcolor}% http://ctan.org/pkg/xcolor
\newsavebox{\mytable}
\begin{document}

\begin{center}
\begin{lrbox}{\mytable}
\input{table.tex}
\end{lrbox}%
\setlength{\fboxrule}{2pt}% Modify rule width if necessary
\ooalign{%
{\fcolorbox{red!50}{red!30}{%
\phantom{\rule[-5pt]{\dimexpr\wd\mytable+10pt}{\dimexpr\baselineskip+5pt}}%
}} \cr
\hss\usebox{\mytable}\hss
}
\end{center}

\end{document}​

-
Thank you for the solution. –  profj Apr 16 '12 at 16:19