# How to circle together certain entries of a matrix?

I am looking for a way to circle together the red entries of the following matrix:

$$\begin{matrix} \textcolor{blue}{[0,1]} & \times & \textcolor{blue}{[0,1]} & \times & \textcolor{blue}{[0,1]} & \times & \textcolor{blue}{[0,1]} & \times & \textcolor{blue}{[0,1]} & \times & \cdots & \times & \textcolor{blue}{[0,1]} & \times & \textcolor{blue}{[0,1]} & \times & \textcolor{blue}{[0,1]} \\ \textcolor{blue}{[2,3]} & \times & \textbf{\textcolor{red}{[0,1]}} & \times & \textbf{\textcolor{red}{[0,1]}} & \times & \textbf{\textcolor{red}{[0,1]}} & \times & \textbf{\textcolor{red}{[0,1]}} & \times & \cdots & \times & \textbf{\textcolor{red}{[0,1]}} & \times & \textbf{\textcolor{red}{[0,1]}} & \times & \textbf{\textcolor{red}{[0,1]}} \\ \textbf{\textcolor{red}{[1,2]}} & \times & \textcolor{blue}{[2,3]} & \times & [0,1] & \times & [0,1] & \times & [0,1] & \times & \cdots & \times & [0,1] & \times & [0,1] & \times & \textbf{\textcolor{red}{[0,1]}} \\ [1,2] & \times & [1,2] & \times & \textcolor{blue}{[2,3]} & \times & [0,1] & \times & [0,1] & \times & \cdots & \times & [0,1] & \times & [0,1] & \times & [0,1] \\ [1,2] & \times & [1,2] & \times & [1,2] & \times & \textcolor{blue}{[2,3]} & \times & [0,1] & \times & \cdots & \times & [0,1] & \times & [0,1] & \times & [0,1]\\ \vdots & \vdots & \vdots & \vdots & \vdots & \vdots & \vdots & \vdots & \vdots & \vdots & \vdots & \vdots & \vdots & \vdots & \vdots & \vdots & \vdots \\ [1,2] & \times & [1,2] & \times & [1,2] & \times & [1,2] & \times & [1,2] & \times & \cdots & \times & [1,2] & \times & \textcolor{blue}{[2,3]} & \times & [0,1]\\ [1,2] & \times & [1,2] & \times & [1,2] & \times & [1,2] & \times & [1,2] & \times & \cdots & \times & [1,2] & \times & [1,2] & \times & \textcolor{blue}{[2,3]}\\ \end{matrix}.$$


Actually, I need many circles like this: all entries before the blue entry in a line should be circled with all entries after the blue entry in the previous line. A simple thin circle would do the job. Is there a standard way of doing this?

Thanks!

PS: A picture of a possible output (simplified):

• Welcome to TeX.SX! Please help us help you and add a minimal working example (MWE) that illustrates your problem. Reproducing the problem and finding out what the issue is will be much easier when we see compilable code, starting with \documentclass{...} and ending with \end{document}. Commented Sep 9, 2019 at 16:07
• Could you please also add a sketch of your desired output? Commented Sep 9, 2019 at 16:33
• You might be interested in How to draw a circle in a matrix like this? and Put a circle around elements in a matrix environment Commented Sep 9, 2019 at 16:35
• Just updated the post with a picture that shows more or less what I'm looking for... :)
– ibr_
Commented Sep 9, 2019 at 17:46
• Commented Sep 9, 2019 at 17:55

Perhaps something like this would work?

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{calc,matrix,backgrounds}

\pgfdeclarelayer{overlay}
\pgfsetlayers{overlay,background,main}

\tikzset{circle/.style = {rounded corners,line width=1bp,color=#1}}%

\begin{document}

$\left[ \begin{tikzpicture}[baseline=-\the\dimexpr\fontdimen22\textfont2\relax] \matrix (m)[matrix of math nodes] { A & B & C & D \\ E & F & G & H \\ I & J & K & L \\ M & N & O & P\\ }; \begin{pgfonlayer}{overlay} \draw[circle=blue] (m-2-2.north east) -- (m-2-3.north east) -- (m-2-3.south east) -- (m-3-4.north east) -- (m-3-4.south east) -- (m-3-4.south west) -- (m-2-3.south east) -- (m-2-2.south west) -- (m-2-2.north west) -- (m-2-2.north east); \end{pgfonlayer} \end{tikzpicture} \right]$
\end{document}


• Welcome to TeX.SE. Commented Sep 9, 2019 at 19:00

Another possibility with pst-node. I slightly simplified your code:

    \documentclass[svgnames]{article}
\usepackage{array, mathtools}
\usepackage{pst-node, auto-pst-pdf}

\begin{document}

$\arraycolsep=1pt \setlength{\extrarowheight}{3pt} \begin{postscript \begin{array}{*{17}{c}} \color{blue} [0,1] & \times & \color{blue} [0,1] & \times & \color{blue} [0,1] & \times & \color{blue} [0,1] & \times & \color{blue} [0,1] & \times & \cdots & \times & \color{blue} [0,1] & \times & \color{blue} [0,1] & \times & \color{blue} [0,1] \\ \color{blue} [2,3] & \times & \pnode[-0.3ex,2.4ex]{C} \color{red}\mathbf{[0,1]} & \times & \color{red}\mathbf{[0,1]} & \times & \color{red} \mathbf{[0,1]} & \times & \color{red} \mathbf{[0,1]} & \times & \cdots & \times & \color{red} \mathbf{[0,1]} & \times & \color{red} \mathbf{[0,1]} & \times \pnode[0.2ex,-1ex]{G} & \color{red} \mathbf{[0,1]} \pnode[0.4ex,2.4ex]{D}\\ \pnode[-0.4ex,2.4ex]{A}\pnode[-0.4ex,-1.2ex]{L} \color{red} \mathbf{[0,1]}\pnode[0.4ex,2.4ex]{H}\pnode[0.4ex,-1.2ex]{K} &\times \pnode[0.2ex,2.4ex]{B} & \color{blue} [2,3] & \times & [0,1] & \times & [0,1] & \times & [0,1] & \times & \cdots & \times & [0,1] & \times & [0,1] & \times \pnode[0.2ex,-1ex]{F} & \color{red} \mathbf{[0,1]} \pnode[0.4ex,-1ex]{E} \\{} [1,2] & \times & [1,2] & \times & \color{blue} [2,3] & \times & [0,1] & \times & [0,1] & \times & \cdots & \times & [0,1] & \times & [0,1] & \times & [0,1] \\{} [1,2] & \times & [1,2] & \times & [1,2] & \times & \color{blue} [2,3] & \times & [0,1] & \times & \cdots & \times & [0,1] & \times & [0,1] & \times & [0,1]\\ \vdots & \vdots & \vdots & \vdots & \vdots & \vdots & \vdots & \vdots & \vdots & \vdots & \vdots & \vdots & \vdots & \vdots & \vdots & \vdots & \vdots \\{} [1,2] & \times & [1,2] & \times & [1,2] & \times & [1,2] & \times & [1,2] & \times & \cdots & \times & [1,2] & \times & \color{blue} [2,3] & \times & [0,1]\\{} [1,2] & \times & [1,2] & \times & [1,2] & \times & [1,2] & \times & [1,2] & \times & \cdots & \times & [1,2] & \times & [1,2] & \times & \color{blue} [2,3]\\ \end{array}. %% \psline[linecolor=Crimson!60, linejoin=1,linearc=2pt](A)(B)(C)(D)(E)(F)(G)(H)(K)(L)(A) \end{postscript}$%

\end{document}