You can calculate the remaining height of a page with the help of \pagegoal
and \pagetotal
. The latter contains the height of the text so far and the first the height available for text (not \textheight
, because there may be floats and/or footnotes on the page). \baselineskip
must be substracted, because the picture starts in a new line. And \parskip
is substracted, so the box gets set further below the text in case a non-zero \parskip
is used.
I also took the freedom to adjust the width to \textwidth
. The -0.4pt
is an adjustment for the width of the lines. Edit: Otherwise there will be an overfull \hbox
warning.
Edit: I changed the code, so it works for pages with no text. Again, the -0.4pt
adjusts for the line width and prevents an overfull \vbox
warning.
This needs a \newpage
before the rectangle on a page with no text.
Edit 2: the rectangle didn't go all the way to the bottom of the page.
I replaced -\baselineskip
by -\lineskip-0.4pt
to correct this (again, -0.4pt
to adjust for the lines).
Note: if another line width is used for the frames, all occurrences of -0.4pt
must be replaced by -<new line width>
. Otherwise overfull boxes may occur or the rectangle on a page with text may be moved to the next page.
Edit 3: removed -parskip
. This wasn't necessary.
\documentclass[a4paper]{article}
\usepackage{tikz}
\usepackage{lipsum}
\newdimen\remainingheight
\newcommand*{\calcremainingheight}{%
\ifdim\pagegoal=\maxdimen
\remainingheight\dimexpr\textheight-0.4pt\relax
\else
% edit 2: replaced -\baselineskip by -\lineskip-0.4pt
% edit 3: removed -\parskip
\remainingheight\dimexpr\pagegoal-\pagetotal-\lineskip-0.4pt\relax
\fi
}
\parskip2ex
\parindent0pt
\begin{document}
\lipsum[1]
\noindent
\begin{tikzpicture}
\calcremainingheight
\node (rect) at (0,0) [draw, minimum width=\textwidth-0.4pt, minimum height=\remainingheight]{};
%\node[below right, text width=16.6 cm] at (rect.north west) {};
\end{tikzpicture}
\lipsum[1-2]
\noindent
\begin{tikzpicture}
\calcremainingheight
\node (rect) at (0,0) [draw, minimum width=\textwidth-0.4pt, minimum height=\remainingheight]{};
%\node[below right, text width=16.6 cm] at (rect.north west) {};
\end{tikzpicture}
% needed for a page with no text before the rectangle
\newpage
\noindent
\begin{tikzpicture}
\calcremainingheight
\node (rect) at (0,0) [draw, minimum width=\textwidth-0.4pt, minimum height=\remainingheight]{};
%\node[below right, text width=16.6 cm] at (rect.north west) {};
\end{tikzpicture}
\end{document}
\documentclass{...}
, the required\usepackage
's,\begin{document}
, and\end{document}
.