[Tex/LaTex] How to circle multiple cells? (special case)

circlestables

I need to circle multiple cells, on different rows/columns. This is a problem which has been solved.

However, I have a more particular case of the problem. I need to draw a circle that needs to illustrate that the elements of the last row are together with the ones of the first row, resulting in a matrix like in the example.

Here is the way I have implemented the table:

\begin{tabular}{c c|c|c|c|c|}
    \multirow{7}{*}{\parbox{1.5cm}{$DataQ_2$}}
    & \multicolumn{5}{c}{$Q_1Q_0$} \\
    \cline{3-6}
    \multicolumn{2}{c|}{} & 00 & 01 & 11 & 10 \\ 
    \cline{2-6}
    & \multicolumn{1}{|c|}{00} & X & 1 & 1 & 1 \\
    \cline{2-6}
    & \multicolumn{1}{|c|}{01} & 0 & 1 & 1 & 0 \\
    \cline{2-6}
    & \multicolumn{1}{|c|}{11} & 1 & 1 & 1 & 1 \\
    \cline{2-6}
    & \multicolumn{1}{|c|}{10} & X & 1 & 1 & 0 \\
    \cline{2-6}
\end{tabular}

This is the way it looks:
Table now

This is how I want it to look(sorry for the shitty drawing quality… I hope it's obvious that I want though):
Table as it should be

PS If you tell me a more elegant way to implement the table to look like this, it would be appreciated.

Best Answer

One option is to use the tikzmark library (from TikZ) to place some marks and then to use those marks to draw the frames; one possibility (adjust the settings according to your needs):

enter image description here

The code:

\documentclass{article}
\usepackage{multirow}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{tikzmark}

\begin{document}

\[
\renewcommand\arraystretch{1.2}
\begin{array}{c|c|c|c|c|c|}
    \multicolumn{1}{c}{}\multirow{7}{*}{$\text{Data}Q_2$}
     & \multicolumn{5}{c}{Q_1Q_0} \\
    \cline{3-6}
    \multicolumn{1}{c}{} & & 00 & 01 & 11 & 10 \\ 
    \cline{2-6}
    & 00 & \tikzmark{startup}X & 1 & 1 & 1\tikzmark{endup} \\
    \cline{2-6}
    & 01 & 0 & 1 & 1 & 0 \\
    \cline{2-6}
    & 11 & 1 & 1 & 1 & 1 \\
    \cline{2-6}
    & 10 & \tikzmark{startdown}X & 1 & 1 & 0\tikzmark{enddown} \\
    \cline{2-6}
\end{array}
\]

\begin{tikzpicture}[remember picture,overlay]
\foreach \Val in {up,down}
{
\draw[rounded corners,red,thick]
  ([shift={(-0.5\tabcolsep,-0.5ex)}]pic cs:start\Val) 
    rectangle 
  ([shift={(0.5\tabcolsep,2ex)}]pic cs:end\Val);
}
\end{tikzpicture}

\end{document}

If the boxes should have one end open:

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

\begin{document}

\[
\renewcommand\arraystretch{1.2}
\text{Data}Q_2\begin{array}{c|c|c|c|c|c|}
    \multicolumn{1}{c}{}
     & \multicolumn{5}{c}{Q_1Q_0} \\
    \cline{3-6}
    \multicolumn{1}{c}{} & & 00 & 01 & 11 & 10 \\ 
    \cline{2-6}
    & 00 & \tikzmark{startup}X & 1 & 1 & 1\tikzmark{endup} \\
    \cline{2-6}
    & 01 & 0 & 1 & 1 & 0 \\
    \cline{2-6}
    & 11 & 1 & 1 & 1 & 1 \\
    \cline{2-6}
    & 10 & \tikzmark{startdown}X & 1 & 1 & 0\tikzmark{enddown} \\
    \cline{2-6}
\end{array}
\]

\begin{tikzpicture}[remember picture,overlay]
\draw[rounded corners,red,thick]
  ([shift={(-0.5\tabcolsep,2ex)}]pic cs:startup) -- 
  ++(0,-2.6ex) -- 
  ([shift={(0.5\tabcolsep,-0.6ex)}]pic cs:endup) --
  ++(0,2.6ex);
\draw[rounded corners,red,thick]
  ([shift={(-0.5\tabcolsep,-0.8ex)}]pic cs:startdown) -- 
  ++(0,2.8ex) -- 
  ([shift={(0.5\tabcolsep,2ex)}]pic cs:enddown) --
  ++(0,-2.8ex);
\end{tikzpicture}

\end{document}

enter image description here

Remarks:

  • Since some internal calculations are involved, the code needs two runs to stabilize.

  • Since the content of the table is mostly maths, I change to array instead of tabular.

  • I used

    \renewcommand\arraystretch{1.2}
    

    to have some more "air" around the cell content.

  • In the second example code I suppressed the \multirow and simply placed \text{Data}Q_2 next to the array.

Related Question