# How to indicate a sliding window in a table

I have a table as follows in beamer.

\documentclass{beamer}

\usepackage[utf8]{inputenc}
\usepackage{default}

\begin{document}
\begin{frame}
\frametitle{test}
\begin{center}
\huge \Huge
\begin{tabular}{|c|c|c|c|c|c|c|}
\hline
A&
B&
C&
B&
A&
B&
C\\
\hline
\end{tabular}
\end{center}
\end{frame}


I would like in the first frame for the first three cells to be highlighted by having the external border of the rectangle containing those three cells thicker and in a different color. For the next frame for cells 2 to 4 to have this and for the next one cells 3 to 5 and for the last one cells 4 to 6.

How can one do this?

I thought I could do it by having four mostly duplicated frames and in each one just stick tables together side by side. This doesn't seem a nice solution however.

• I don't really understand what you mean. The cells' borders are also the lines between the cells, together with the lines around the table, so what is and what is not supposed to change colour? Do you want the lines above and below the letters to change but not those to the left or right? Or...? – cfr Jun 11 '14 at 23:09
• @cfr You are right I wasn't clear. I have added a comment to your answer and edited the question slightly. – felix Jun 12 '14 at 3:10

You can do this with a TikZ matrix (instead of tabular).

# Code

\documentclass{beamer}
\usepackage{tikz}
\usetikzlibrary{matrix}

\begin{document}

\begin{frame}{test}
\centering

\begin{tikzpicture}
\tikzset{
mstyle/.style={column sep=-\pgflinewidth,nodes={draw},font=\huge},
window/.style={draw,very thick,blue},
}
\matrix(m)[matrix of nodes,ampersand replacement=\&,mstyle]{
A \& B \& C \& B \& A \& B \& C \\
};
\foreach \j [count=\i] in {3,...,7}{
\onslide<\i>{
\draw[window](m-1-\i.north west)rectangle(m-1-\j.south east);
}}
\end{tikzpicture}

\end{frame}

\end{document}


# Output

• This is a great solution. Thank you. If I want to substitute an image for one of the letters, how would I get the height right? I am trying \matrix(m)[matrix of nodes,ampersand replacement=\&,mstyle]{ \includegraphics[height=0.5cm]{can1} \& B \& C \& B \& A \& B \& C \\ }; which seems about right but is there an automatic way to get this right? – felix Jun 12 '14 at 9:25
• @felix: Sorry for the late reply. In that case, there are several keys you need to set to make things look right. Replace nodes={draw} in the definition of mstyle with the following: nodes={draw,anchor=south,minimum height=1cm, minimum width=1cm,inner sep=0pt},, then you can use \includegraphics[height=1cm]{image} to properly scale the image. Play with the values of minimum height and minimum width to get the ideal dimension that works for you. – Herr K. Jun 12 '14 at 17:08

You could try something like this:

\documentclass{beamer}

\usepackage[utf8]{inputenc}
\usepackage{default,tikz}
\usetikzlibrary{tikzmark,calc}

\begin{document}
\begin{frame}
\frametitle{test}
\centering
\begin{tikzpicture}[remember picture, ultra thick, font=\Huge]
\node{%
\begin{tabular}{|*{7}{c|}}
\hline
\subnode{a}{A}&
\subnode{b}{B}&
\subnode{c}{C}&
\subnode{d}{D}&
\subnode{e}{E}&
\subnode{f}{F}&
\subnode{g}{G}\\
\hline
\end{tabular}
};
\onslide<1>{%
\draw [blue] ($(a.north west) - (-.25em,.5em)$) -- ($(c.north east) - (.25em,.5em)$);
\draw [blue] ($(a.south west) - (-.25em,0)$) -- ($(c.south east) - (.25em,0)$);
}
\onslide<2>{%
\draw [blue] ($(b.north west) - (-.25em,.5em)$) -- ($(d.north east) - (.25em,.5em)$);
\draw [blue] ($(b.south west) - (-.25em,0)$) -- ($(d.south east) - (.25em,0)$);
}
\onslide<3>{%
\draw [blue] ($(c.north west) - (-.25em,.5em)$) -- ($(e.north east) - (.25em,.5em)$);
\draw [blue] ($(c.south west) - (-.25em,0)$) -- ($(e.south east) - (.25em,0)$);
}
\onslide<4>{%
\draw [blue] ($(d.north west) - (-.25em,.5em)$) -- ($(f.north east) - (.25em,.5em)$);
\draw [blue] ($(d.south west) - (-.25em,0)$) -- ($(f.south east) - (.25em,0)$);
}
\end{tikzpicture}
\end{frame}
\end{document}


# EDIT

This code turns the thick lines into a 'window' by joining the verticals, as requested in the comments.

Because the thick lines now outline a rectangle, forming a continuous line, it is possible to use one rather than two \draw commands per frame and we can use -- cycle to complete the rectangular shapes:

\documentclass{beamer}

\usepackage[utf8]{inputenc}
\usepackage{default,tikz}
\usetikzlibrary{tikzmark,calc}

\begin{document}
\begin{frame}
\frametitle{test}
\centering
\begin{tikzpicture}[remember picture, ultra thick, font=\Huge]
\node{%
\begin{tabular}{|*{7}{c|}}
\hline
\subnode{a}{A}&
\subnode{b}{B}&
\subnode{c}{C}&
\subnode{d}{D}&
\subnode{e}{E}&
\subnode{f}{F}&
\subnode{g}{G}\\
\hline
\end{tabular}
};
\onslide<1>{%
\draw [blue] ($(a.north west) - (-.275em,.425em)$) -- ($(c.north east) - (.2em,.425em)$) -- ($(c.south east) - (.2em,.05em)$) -- ($(a.south west) - (-.275em,.05em)$) -- cycle;
}
\onslide<2>{%
\draw [blue] ($(b.north west) - (-.275em,.425em)$) -- ($(d.north east) - (.2em,.425em)$) -- ($(d.south east) - (.2em,.05em)$) -- ($(b.south west) - (-.275em,.05em)$) --cycle;
}
\onslide<3>{%
\draw [blue] ($(c.north west) - (-.275em,.425em)$) -- ($(e.north east) - (.2em,.425em)$) -- ($(e.south east) - (.2em,.05em)$) -- ($(c.south west) - (-.275em,.05em)$) -- cycle;
}
\onslide<4>{%
\draw [blue] ($(d.north west) - (-.275em,.425em)$) -- ($(f.north east) - (.2em,.425em)$) -- ($(f.south east) - (.2em,.05em)$) -- ($(d.south west) - (-.275em,.05em)$) -- cycle;
}
\end{tikzpicture}
\end{frame}
\end{document}


This code can be simplified using a \foreach to loop over the set of triples consisting of the slide number, node at the left of the desired 'window' and node at the right of the desired 'window'. We can use \slide/\leftside/\rightside for the variables we want to loop over as follows:

\documentclass{beamer}

\usepackage[utf8]{inputenc}
\usepackage{default,tikz}
\usetikzlibrary{tikzmark,calc}

\begin{document}
\begin{frame}
\frametitle{test}
\centering
\begin{tikzpicture}[remember picture, ultra thick, font=\Huge]
\node{%
\begin{tabular}{|*{7}{c|}}
\hline
\subnode{a}{A}&
\subnode{b}{B}&
\subnode{c}{C}&
\subnode{d}{D}&
\subnode{e}{E}&
\subnode{f}{F}&
\subnode{g}{G}\\
\hline
\end{tabular}
};
\foreach \slide/\leftside/\rightside in {1/a/c,2/b/d,3/c/e,4/d/f}
\onslide<\slide>{%
\draw [blue] ($(\leftside.north west) - (-.275em,.425em)$) -- ($(\rightside.north east) - (.2em,.425em)$) -- ($(\rightside.south east) - (.2em,.05em)$) -- ($(\leftside.south west) - (-.275em,.05em)$) -- cycle;
};
\end{tikzpicture}
\end{frame}
\end{document}


This makes it easier to adjust the size, location or other characteristics of the 'window', and straightforward to extend the sliding frame to a longer row of 'window panes'.

• This is great, thank you. Would it be hard to add thick vertical lines at the beginning and end of the sliding window too? So in the first frame the vertical line at the left end and between C and D would be thick and blue. – felix Jun 12 '14 at 3:18
• @felix That would be easy but I see you've accepted an answer. Do you still want to know how to do it this way or are you happy with that solution? – cfr Jun 12 '14 at 22:45
• I would love to, thank you. I am just at the start of the TikZ learning hill/mountain. – felix Jun 13 '14 at 7:56
• @felix Please see my edit. – cfr Jun 13 '14 at 22:08