# How can I keep my TikZ overlay picture on the same page?

I needed to draw squares to highlight submatrices and I found on the internet (perhaps here) a snippet that uses TikZ. By the way I found that if the math expression is at the end of the page, the square is drawn in the next page. Please compile the following code with pdflatex to understand what I mean. If you remove an 'a' paragraph, you get the correct behavior.

How do I solve the problem? Are there better solutions?

\documentclass[11pt,a4paper]{article}
\usepackage{blindtext}
\newcommand{\pder}[2]{\frac{\partial #1}{\partial #2}}

\usepackage{tikz}
\usetikzlibrary{decorations.pathreplacing,intersections,shapes.arrows}
\usetikzlibrary{calc}
\usetikzlibrary{matrix,positioning}

\newcommand{\tikzmark}[1]{\tikz[overlay,remember picture]\node(#1){};}
\newcommand{\DrawBox}[1][]{%
\tikz[overlay,remember picture]{
\draw[#1]
($(left)+(-0.5em,1.2em)$) rectangle
($(right)+(0.5em,-0.5em)$);}
}

\begin{document}

\blindtext[3]

a

a

a

a

a

a

a

$J(\psi \circ \phi^{-1})\big|_{\phi(q)} = \left( \begin{array}{cccc} \tikzmark{left}\pder{y^1}{x^1} & \ldots &\pder{y^1}{x^{n-1}} &\pder{y^1}{x^{n}} \\ \vdots & &\vdots &\vdots \\ \pder{y^{n-1}}{x^1} &\ldots &\pder{y^{n-1}}{x^{n-1}}\tikzmark{right} &\pder{y^{n-1}}{x^n} \\ 0 &\ldots &0 & \pder{y^n}{x^n} \\ \end{array}\right) \phi(q)$
\DrawBox[dashed]

\blindtext[6]
\end{document}

-
I compiled twice but I don't see any effect (unless I miss some pdflatex arguments). And the picture being shifted is the problem that I want to solve –  Pincopallino Mar 10 '13 at 10:30

What you might be experimenting with the array of as is filling up a page until TeX decides the page is full and ships it out as if you have written a code like

....
a

a

$math formula contents$
\newpage %<============
\DrawBox[dashed]

...


So you are providing the relative positions via \tikzmarks and they are recorded to the .aux file with respect to the current page(!). In the next run they are read from the .aux file. But since the snippet you are using is not page number aware, it's now pushed after a newpage and automatically places itself on the new page.

When you put the \DrawBox[dashed] command inside the display math, roughly speaking, it is considered to be a standalone item as it is placed within a page as it is and in case it doesn't fit, pushed to the new page. But \DrawBox will always be executed on the same page the formula is placed.

Long story short, move the \DrawBox[dashed] command inside the display math as the last item.

    $J(\psi \circ \phi^{-1})\big|_{\phi(q)} = \left( \begin{array}{cccc} \tikzmark{left}\pder{y^1}{x^1} & \ldots &\pder{y^1}{x^{n-1}} &\pder{y^1}{x^{n}} \\ \vdots & &\vdots &\vdots \\ \pder{y^{n-1}}{x^1} &\ldots &\pder{y^{n-1}}{x^{n-1}}\tikzmark{right}% &\pder{y^{n-1}}{x^n} \\ 0 &\ldots &0 & \pder{y^n}{x^n} \\ \end{array}\right) \phi(q) \DrawBox[dashed]$

-
I actually found this solution by myself, but you gave a great explanation of the situation! Thank you very much! –  Pincopallino Mar 10 '13 at 18:58