# Highlighting certain parts of matrix without TikZ

I have a matrix, given in the following snippet:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage{blkarray}

\begin{document}

$$\begin{blockarray}{ccccccccccc} & \O & 12 & 12,34 & 13 & 13,24 & 14 & 14,23 & 23 & 24 & 34 \\ \begin{block}{c[cccccccccc]} 01 & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 1 & 1 & 1 \\ 01,23 & 1 & 1 & 0 & 1 & 0 & 0 & 0 & 1 & 1 & 1 \\ 01,24 & 1 & 1 & 0 & 0 & 0 & 1 & 0 & 1 & 1 & 1 \\ 01,34 & 1 & 0 & 0 & 1 & 0 & 1 & 0 & 1 & 1 & 1 \\ \end{block} \end{blockarray}$$
\end{document}


I'd like to have a green box around the first 7 columns of the matrix, and a red box around the other 3 columns. I've tried implementing the solution given in this post, but I think that using blkarray inside of TikZ is causing a problem.

Is there an alternative I can use? Or perhaps a way to use TikZ which will work?

• You could use a TikZ matrix instead of blkarray. – Skillmon Jul 23 '18 at 20:49
• @Skillmon I'm open to doing that, but I don't know how to label the rows and columns without blkarray – Alex Jul 23 '18 at 20:53

with tikz ...

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{backgrounds, fit, matrix}

\begin{document}

$$\begin{tikzpicture}[baseline, every label/.append style={label distance=6pt, inner sep=0pt} ] \matrix (m) [matrix of nodes, nodes={minimum width=2em, minimum height=3ex, text depth=2pt, inner xsep=1pt, anchor=base}, column sep=4pt, row sep=0pt, ] {|[label=left:01, label=\emptyset]| 1 & |[label=12]| 1 & |[label={12,34}]| 0 & |[label=13]| 0 & |[label={13,24}]| 0 & |[label=14]| 0 & |[label={14,23}]| 0 & |[label=23]| 1 & |[label=24]| 1 & |[label=34]| 1 \\ |[label=left:{01,23}]| 1 & 1 & 0 & 1 & 0 & 0 & 0 & 1 & 1 & 1 \\ |[label=left:{01,24}]| 1 & 1 & 0 & 0 & 0 & 1 & 0 & 1 & 1 & 1 \\ |[label=left:{01,34}]| 1 & 0 & 0 & 1 & 0 & 1 & 0 & 1 & 1 & 1 \\ }; \draw (m-1-1.north west) + ( 3pt,0) -| (m-4-1.south west) -- + ( 3pt,0); \draw (m-1-9.north east) + (-3pt,0) -| (m-4-9.south east) -- + (-3pt,0); % \node[draw=red, inner sep=1pt, fit={([xshift=5pt] m-1-1.north west) (m-4-6)}] {}; \node[draw=green, inner sep=1pt, fit={(m-1-7) ([xshift=-5pt] m-4-9.south east)}] {}; \end{tikzpicture}$$
\end{document}

• +1 for not using my package – David Carlisle Jul 23 '18 at 22:06
• @DavidCarlisle, you are welcome! and thank you very much! however i like to use your packages (actually i can live in latex without them) when know how :-) – Zarko Jul 23 '18 at 22:16
• @DavidCarlisle, just now i stuck using tabularx which has 26 columns. i cant remember (and find here) how to increase number of columns and now i'm not able to enjoy to chat room to ask for help ... my bad – Zarko Jul 23 '18 at 22:21
• why is the number of columns an issue, tabularx hasn't got any maximum other than generic tex limits has it? – David Carlisle Jul 23 '18 at 22:25
• because the width of cell are corrupted. can we discus this in chat room? how i can reach you in it? – Zarko Jul 23 '18 at 22:31

It is simple with pstricks: you define the opposite vertices of a frame as empty nodes (\pnode command) at the relevant places in the matrix and use the \psframe command. I also added some improvements to your matrix:

\documentclass[svgnames]{article}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage{amssymb}
\usepackage{blkarray, array, bigstrut, makebox}
\usepackage{pst-node}
\usepackage{auto-pst-pdf}

\begin{document}

\begin{postscript}
\psset{linejoin=1}
$$\setlength{\BAextrarowheight}{3pt}\bigstrutjot = 4pt \begin{blockarray}{cr*{9}{c}} & \varnothing & 12 & \makebox*{12}{12{,}34} & 13 & \makebox*{12}{13{,}24} & 14 & \makebox*{12}{14{,}23} & 23 & 24 & 34 \\ \begin{block}{c[r*{9}{c}]} 01 &\enspace \pnode[-1ex, 2.1ex]{A} 1 & 0 & 0 & 0 & 0 & 0 & 0 & \pnode[-1ex, 2.1ex]{C} 1 & 1 & 1 \bigstrut[t]\\ 01{,}23 & 1 & 1 & 0 & 1 & 0 & 0 & 0 & 1 & 1 & 1 \\ 01{,}24 & 1 & 1 & 0 & 0 & 0 & 1 & 0 & 1 & 1 & 1 \\ 01{,}34 & 1 & 0 & 0 & 1 & 0 & 1 & 0\pnode[1ex, -0.6ex]{B} & 1 & 1 & 1 \pnode[1ex, -0.6ex]{D}\bigstrut[b]\enspace \\ \end{block} \end{blockarray}$$
\psframe[linecolor=IndianRed](A)(B)
\psframe[linecolor=DarkSeaGreen](C)(D)
\end{postscript}

\end{document}


An option based on these examples: Highlight elements in the matrix in tikz, colored upper triangular matrix.

RESULT:

MWE:

\documentclass[tikz,border=14pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix,shapes,decorations.pathreplacing,fit,backgrounds}

\begin{document}
\begin{tikzpicture}[
%Global config
Highlight/.style={
draw,
fill,
rounded corners=2pt,
fill opacity=0.05,
},
Delimiter/.style={
left delimiter=[,
right delimiter=],
inner xsep=-1pt,
%inner ysep=-1pt,
}
]

\matrix[matrix of nodes,align=center,nodes in empty cells,] at (0,0) (M){ % Matrix contents
& \O & 12 & 12,34 & 13 & 13,24 & 14 & 14,23 & 23 & 24 & 34 \\
01 & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 1 & 1 & 1 \\
01,23 & 1 & 1 & 0 & 1 & 0 & 0 & 0 & 1 & 1 & 1 \\
01,24 & 1 & 1 & 0 & 0 & 0 & 1 & 0 & 1 & 1 & 1 \\
01,34 & 1 & 0 & 0 & 1 & 0 & 1 & 0 & 1 & 1 & 1 \\
};

\begin{scope}[on background layer]
%FOR MATRIX M
%To delimit internal braces
\node[Delimiter,fit=(M-2-2)(M-5-11)](Internal){};
%To delimit internal area groups
\draw[Highlight,red](M-5-2.south west) rectangle (M-2-8.north -| M-1-8.east);
\draw[Highlight,green](M-5-9.south west) rectangle (M-2-11.north east);
\end{scope}
\end{tikzpicture}
\end{document}