# Circle around content of a table: is it possible?

I managed to create three tables with letters in it:

\begin{minipage}{0.37\textwidth}
\begin{tabular}{c | c | c | c | c}
E & G & P & A & L\\
\hline
I & I & U & G & F\\
\hline
G & A & F & Z & U\\
\hline
\textbf{H} & R & T & U & Z\\
\hline
\textbf{A} & I & N & F & H\\
\hline
\textbf{U} & I & A & C & T\\
\hline
\textbf{S} & N & E & R & Z\\
\end{tabular}
\end{minipage}
\begin{minipage}{0.37\textwidth}
\begin{tabular}{c | c | c | c | c}
E & G & P & A & L\\
\hline
I & I & U & G & F\\
\hline
G & A & F & Z & U\\
\hline
\textbf{H} & \textbf{A} & \textbf{U} & \textbf{S} & Z\\
\hline
L & I & N & F & H\\
\hline
G & I & A & C & T\\
\hline
D & N & E & R & Z\\
\end{tabular}
\end{minipage}
\begin{minipage}{0.37\textwidth}
\begin{tabular}{c | c | c | c | c}
E & G & P & A & L\\
\hline
I & I & U & G & F\\
\hline
G & A & F & Z & U\\
\hline
\textbf{H} & B & R & T & Z\\
\hline
L & \textbf{A} & N & F & H\\
\hline
G & I & \textbf{U} & C & T\\
\hline
D & N & E & \textbf{S} & Z\\
\end{tabular}
\end{minipage}


Now I want to draw one circle around all the letters that are printed bold. Is this easily possible?

-
You may use \textcircled (see section 3.14 of usrguide). –  Schweinebacke Nov 23 '11 at 11:14
I don't want to draw a circle around every letter, I would prefer to only draw one single circle. –  RoflcoptrException Nov 23 '11 at 11:18
Do you want to put all the letter in one ellipse ? –  projetmbc Nov 23 '11 at 11:19
Do you want this to be automated? As in, can there be bold letters scattered around the table and you want the convex hull? Can the shapes be more complicated than in your example? –  Roelof Spijker Nov 23 '11 at 11:21
Is this answer something like what you're after? tex.stackexchange.com/q/4861/586 –  Torbjørn T. Nov 23 '11 at 11:37

The answer Torbjørn is referencing doesn't deal with the diagonal case. To get this to work you would have to rotate the rectangle accordingly. This solution works by placing a starting and ending point and calculating the angle at which to draw the rectangle. The rectangle is actually a node. This is the code including examples:

\documentclass{report}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\def\startCirc#1{\tikz[remember picture,overlay]\path node[inner sep=0, anchor=south] (st) {\textbf{#1}} coordinate (start) at (st.center);}%
\def\endCirc#1{\tikz[remember picture,overlay]\path node[inner sep=0, anchor=south] (en) {\textbf{#1}} coordinate (end) at (en.center);%
\begin{tikzpicture}[overlay, remember picture]%
\path (start);%
\pgfgetlastxy{\startx}{\starty}%
\path (end);%
\pgfgetlastxy{\endx}{\endy}%
\pgfmathsetlengthmacro{\xdiff}{\endx-\startx}%
\pgfmathsetlengthmacro{\ydiff}{\endy-\starty}%
\pgfmathtruncatemacro{\xdifft}{\xdiff}%
\pgfmathsetmacro{\xdiffFixed}{ifthenelse(equal(\xdifft,0),1,\xdiff)}%
\pgfmathsetmacro{\angle}{ifthenelse(equal(\xdiffFixed,1),90,atan(\ydiff/\xdiffFixed))}%
\pgfmathsetlengthmacro{\xydiff}{sqrt(abs(\xdiff^2) + abs(\ydiff^2))}%
\path node[draw,rectangle, rounded corners=2mm, dashed, rotate=\angle, minimum width=\xydiff+4ex, minimum height=2.5ex] at ($(start)!.5!(end)$) {};%
\end{tikzpicture}%
}
\noindent
\begin{tabular}{c | c | c | c | c}
E & G & P & A & L\\
\hline
I & I & U & G & F\\
\hline
G & A & F & Z & U\\
\hline
\startCirc{H} & R & T & U & Z\\
\hline
\textbf{A} & I & N & F & H\\
\hline
\textbf{U} & I & A & C & T\\
\hline
\endCirc{S} & N & E & R & Z\\
\end{tabular}
\begin{tabular}{c | c | c | c | c}
E & G & P & A & L\\
\hline
I & I & U & G & F\\
\hline
G & A & F & Z & U\\
\hline
\startCirc{H} & \textbf{A} & \textbf{U} & \endCirc{S} & Z\\
\hline
L & I & N & F & H\\
\hline
G & I & A & C & T\\
\hline
D & N & E & R & Z\\
\end{tabular}
\begin{tabular}{c | c | c | c | c}
E & G & P & A & L\\
\hline
I & I & U & G & F\\
\hline
G & A & F & Z & U\\
\hline
\startCirc{H} & B & R & T & Z\\
\hline
L & \textbf{A} & N & F & H\\
\hline
G & I & \textbf{U} & C & T\\
\hline
D & N & E & \endCirc{S} & Z\\
\end{tabular}
\begin{tabular}{c | c | c | c | c}
E & G & P & A & L\\
\hline
I & I & U & G & F\\
\hline
G & A & F & Z & U\\
\hline
H & B & R & T & \startCirc{Z}\\
\hline
L & A & N & \textbf{F} & H\\
\hline
G & I & \textbf{U} & C & T\\
\hline
D & \endCirc{N} & E & S & Z\\
\end{tabular}
\end{document}


The abs is needed on the squares, because for some reason, TikZ thinks that \x^2 where \x<0 is equal to -\y^2 where \y=-\x. The result then looks like this:

Note that this will fail for highlighting multiple rows next to each other in a single 'circle'.

Edit: To get the line a little closer in the horizontal case, you will have to make the node height conditional on the angle. You can easily do this by replacing the line that draws the node by the following:

\pgfmathsetlengthmacro{\myheight}{ifthenelse(equal(\angle,0),1.5ex,2.5ex)}
\path node[draw,rectangle, rounded corners=1mm, dashed, rotate=\angle, minimum width=\xydiff+4ex, minimum height=\myheight] at ($(start)!.5!(end)$) {};%


Note that I have also changed the rounded corners to 1 mm here. Otherwise the node starts to look 'wrong'. You can of course also create a conditional for this, in exactly the same manner, allowing you to keep a rounded corners value of 2 mm on the other nodes.

-
Thanks a lot, this is almost perfect! –  RoflcoptrException Nov 23 '11 at 13:27
is it possible to draw the top line in the horizontal case a little bit closer to the letters? I can't figure out how. –  RoflcoptrException Nov 23 '11 at 14:58
@Roflcoptr: see the updated answer –  Roelof Spijker Nov 23 '11 at 15:07
All of this can be used for one word game called "Mots mé^lés" in french. That's great ! –  projetmbc Nov 23 '11 at 17:35