I defined a new environment that draws a sort of answer box using fbox around a minipage. The environment also puts some text above the answer box.
Here is a minimal working example:
\documentclass{article} \usepackage{listings} \newsavebox{\mybox} \newenvironment{ansbox}[1]{Write answer in the box:\newline \begin{lrbox}{\mybox}\begin{minipage}[t][#1]{\linewidth}} {\vfill\hfill\end{minipage}\end{lrbox}\fbox{\usebox{\mybox}}} \setlength{\parskip}{\baselineskip} \setlength{\parindent}{0ex} \begin{document} Description of problem. \begin{ansbox}{2in} %empty box \end{ansbox} What is your name? \begin{ansbox}{2in} My name is \end{ansbox} Implement a factorial function. \begin{ansbox}{2in} \begin{lstlisting} def factorial(n): \end{lstlisting} \end{ansbox} \end{document} For convenience I attach a screenshot of the first page:
As you can see, there is a vertical space when the box is empty, or when it begins immediately with a lstlisting environment. I tried searching around but have no idea how this space is added or how to get rid of it.
Also, how can I keep the "header" text together with the box when it breaks into the next page?


