6

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?

Moriambar
  • 11,466

1 Answers1

10

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}
  \quad
  \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}
  \quad
  \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}
  \quad
  \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:

Circled elements in table

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.

David Carlisle
  • 757,742
Roelof Spijker
  • 17,663
  • 5
  • 55
  • 63