2

Sometext that occupies one third of the page. After this a rectangle needs to be drawn that will carry on till the end of the current page. Basically its a question paper and I am providing spaces for the students to write their answers in the empty box.

\begin{tikzpicture}
\node (rect) at (0,0) [draw, text width=16.6 cm, minimum height=7cm]{};
\node[below right, text width=16.6 cm] at (rect.north west) {};
\end{tikzpicture}

\pagebreak

Some text with one line. After this the rectangle should be drawn till the end of the page.

\begin{tikzpicture}
\node (rect) at (0,0) [draw, text width=16.6 cm, minimum height=7cm]{};
\node[below right, text width=16.6 cm] at (rect.north west) {};
\end{tikzpicture}

As you see, I am explicitly writing the text width and minimum height. I would like to do it automatically, with the page width and height so that the rectangle is made automatically depending on the space left after the last line. The beginning of the rectangle, however, could be from anywhere in the page. ( EDIT: The beginning of the rectangle starts after the end of the previous line )

2
  • 1
    Please make your code complete, with \documentclass{...}, the required \usepackage's, \begin{document}, and \end{document}.
    – CarLaTeX
    Jul 9, 2017 at 15:04
  • 1
    Okay, give me a second. I will also exactly elaborate the beginning part of the rectangle. Jul 9, 2017 at 15:41

3 Answers 3

5

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}
9
  • 1
    works like charm, except that when the rectangle starts from an empty page i.e no text above the rectangle, then the calcremainingheight doesnt work. Jul 9, 2017 at 16:01
  • 1
    well, I simply wrote Contd... as text and then the \calcremainingheight doesnt complain ;) Jul 9, 2017 at 16:05
  • 1
    @infoclogged I enhanced \calcremainingheight to handle empty pages und added an explanation for the -0.4pt (see edit).
    – Mike
    Jul 9, 2017 at 16:21
  • I wrote Contd.. on the top of the page, as a workaround for your previous solution. However strangely, your new code, doesnt work on that page anymore. It does work on completely empty page, but not with one line like Contd... on the top. Can you please check at ur end? Jul 10, 2017 at 11:37
  • @infoclogged the code for the rectangle must start in a new paragraph. So there must be a blank line between "Contd..." and the code.
    – Mike
    Jul 10, 2017 at 20:44
2

Here are two solutions. One is intended to be placed at the end of the previous line of text. The other is intended to be placed at the start of a new line of text. It ignores indentation or centering.

Note that in overlay mode, the origin will be located on the baseline, so some offset is needed. \strutbox is the same size as \strut.

Don't forget to run it twice.

\documentclass{article}
\usepackage{tikzpagenodes}
\usepackage{lipsum}% MWE only

\begin{document}
Some text on one line.
\begin{tikzpicture}[remember picture,overlay]% still on same line as text
  \path (0pt,-\dp\strutbox) -| (current page text area.west) coordinate[midway] (start);
  \draw (start) rectangle (current page text area.south east);
\end{tikzpicture}

\lipsum[1-6]

\begin{tikzpicture}[remember picture,overlay]% start of new paragraph
  \path (0pt,\ht\strutbox) -| (current page text area.west) coordinate[midway] (start);
  \draw (start) rectangle (current page text area.south east);
\end{tikzpicture}
\lipsum[7-11]
\end{document}
4
  • 1
    The OP said "The beginning of the rectangle, however, could be from anywhere in the page."...
    – CarLaTeX
    Jul 9, 2017 at 15:29
  • 1
    @CarLaTeX - From the current baseline, top of current line, below current line? Any of these are possible, if that is the case. Jul 9, 2017 at 15:36
  • 2
    I think the OP question is too unclear...
    – CarLaTeX
    Jul 9, 2017 at 15:38
  • 1
    @CarLaTeX edited. Jul 9, 2017 at 15:49
2

tcolorbox offers height fill option to solve this kind of problems:

\documentclass{article}
\usepackage[most]{tcolorbox}
\usepackage{lipsum}

\begin{document}
\lipsum[1]
\begin{tcolorbox}[height fill, title=Your solution]
\end{tcolorbox}

\lipsum[2]
\begin{tcolorbox}[height fill, sharp corners, colback=white]
\end{tcolorbox}
\end{document}

enter image description here

2

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .