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}