# tikzpicture, scope and matrix

I am trying to highlight a particular column in a matrix. I therefore draw the matrix (with easybmat) in a node and I draw the shape of the column in a scope (on the background). However, the scope is shifted right, and I do not know why.

The highlight should be from (second line, first column) to (last line, first column).

\documentclass{article}
\usepackage{etex}
\usepackage{easybmat}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{tikz}
\usetikzlibrary{backgrounds,fit}

\tikzset{%
highlight/.style={rectangle,rounded corners,fill=orange!35,draw,thick,inner sep=2pt}
}
\newcommand{\tikzmark}[2]{\tikz[remember picture,baseline=(#1.base),inner sep=0,outer sep=0pt] \node (#1) {#2};}

\begin{document}

\begin{tikzpicture}[remember picture]
\node (){
$\left( \begin{BMAT}(r){cccccccc}{cccccccc} a_{1\,1}^{(k)} & a_{1\,2}^{(k)} & \cdots & & & & \cdots & a_{1\,n}^{(k)} \\ \tikzmark{left}{0} & a_{2\,2}^{(k)} & & & & & & a_{2\,n}^{(k)} \\ \vdots & & \ddots & & & && \vdots \\ & \vdots & & a_{k-1\, k-1}^{(k)} & & & & \\ & & & & a_{k\, k}^{(k)} & & & \\ & & \cdots &\vdots & & a_{k+1\, k+1}^{(k)} & & \\ & & & & \vdots & \vdots &\ddots & \vdots \\ \tikzmark{right}{0} & & & & & a_{n\, k+1}^{(k)} & & a_{n\,n}^{(k)} \\ \end{BMAT} \right)$
};
\begin{scope}[on background layer]
\node[highlight,overlay,fit=(left.north west) (right.south east)] {};
\end{scope}
\end{tikzpicture}
\end{document}


• Welcome to TeX.SX! A tip: If you indent lines by 4 spaces, they'll be marked as a code sample. You can also highlight the code and click the "code" button (with "{}" on it). May 6, 2014 at 9:45
• There is no problem on TeXLive 2013, Ubuntu 10.04.
– user31034
May 6, 2014 at 10:23
• You have to compile at least two times to get that rectangle right. Compile 2-3 times and it should work.
– user11232
May 6, 2014 at 11:03
• Why don't you use the TikZ Matrix Library? It is very simple to color an entire row or column May 6, 2014 at 12:40
• Of course, I compiled twice.. May 6, 2014 at 13:22

A layer approach. No need to enclose everything in one tikzpicture. Run pdflatex twice.

\documentclass{article}
\usepackage{etex}
\usepackage{easybmat}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{tikz}
\usetikzlibrary{fit}

\tikzset{%
highlight/.style={rectangle,rounded corners,fill=orange!35,draw,thick,inner sep=2pt}
}
\newcommand{\tikzmark}[2]{\tikz[remember picture,baseline=(#1.base),inner sep=0,outer sep=0pt] \node (#1) {#2};}

\begin{document}

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

\begin{pgfonlayer}{foreground}
$\left( \begin{BMAT}(r){cccccccc}{cccccccc} a_{1\,1}^{(k)} & a_{1\,2}^{(k)} & \cdots & & & & \cdots & a_{1\,n}^{(k)} \\ \tikzmark{left}{0} & a_{2\,2}^{(k)} & & & & & & a_{2\,n}^{(k)} \\ \vdots & & \ddots & & & && \vdots \\ & \vdots & & a_{k-1\, k-1}^{(k)} & & & & \\ & & & & a_{k\, k}^{(k)} & & & \\ & & \cdots &\vdots & & a_{k+1\, k+1}^{(k)} & & \\ & & & & \vdots & \vdots &\ddots & \vdots \\ \tikzmark{right}{0} & & & & & a_{n\, k+1}^{(k)} & & a_{n\,n}^{(k)} \\ \end{BMAT} \right)$
\end{pgfonlayer}

\begin{tikzpicture}[inner sep=0pt,thick,remember picture,overlay]
\begin{pgfonlayer}{background}
\node[highlight,fit=(left) (right)] {};
\end{pgfonlayer}
\end{tikzpicture}
\end{document}

• I thank you for your answer. However It does not work with latex->dvips->ps2pdf. It only draws the orange highlight. Do you know why ? May 6, 2014 at 23:16
• I don't why latex->dvips->ps2pdf doesn't work. Why don't you use pdflatex instead ? May 7, 2014 at 4:19
• @hindi pgfmanual v3.0 (p.1037): "To use remember picture, you have to use a backend driver that supports position tracking. pdfTeX is one such driver, dvips currently is not." May 7, 2014 at 7:04
• Well for several reasons. First I use .ps pictures. Then I include screenshots and the pdf generated by latex is a wrong quality one. I therefor have to make latex->dvips and the generate the pdf with Adobe Distille (from the .dvi). May 7, 2014 at 7:59

This is not an answer but an extension of my comment. Also see the manual for adding delimiters to the matrix.

\documentclass{beamer}
\usepackage{tikz}
\usetikzlibrary{matrix}
\begin{document}
\begin{tikzpicture}[column 2/.style=red]
\matrix [matrix of nodes]
{
8 & 1 & 6 \\
3 & 5 & 7 \\
4 & 9 & 2 \\
};
\end{tikzpicture}
\end{document}

• well it is strange it works on Ubuntu. I do am on mac. Thanks Sergio for your answer, but it does not solve the problem.. May 6, 2014 at 14:38