3

I am trying to write a problem assignment, with textboxes containing the written solutions. The textboxes should be distributed evenly on the page, consuming all the available space. The intended result should look like the following:

\documentclass{scrartcl}
\begin{document}
    Please justify all your answers to the following questions.
    \begin{enumerate}
        \item Is this = that?\hfill 2 points

        \fbox{\parbox[t][5cm][t]{\dimexpr\linewidth-2\fboxrule-2\fboxsep}{%
            Solution.

            Bla bla bla.
        }}

        \item Is that = this?\hfill 2 points

        \fbox{\parbox[t][5cm][t]{\dimexpr\linewidth-2\fboxrule-2\fboxsep}{%
                Solution.
        }}
    \end{enumerate}
\end{document}

enter image description here

Of course I would like the boxes to consume the entire available space. How can I achieve this?

  • Do you mean the whole page,i.e, one solution box in one page? – ferahfeza Feb 12 at 11:32
  • @ferahfeza There may be multiple solution boxes on one page; in this case, the available space should be evenly distributed. – Bubaya Feb 12 at 12:16
  • 1
    Related: Stretching a framebox over the whole page. The solutions there could be adapted for this task. The calculated height would just need to be splitted over multiple boxes. – Martin Scharrer Feb 12 at 12:42
  • @MartinScharrer I've tried to do this in my own answer. However, I am not sure how unstable this is (first guess: very), and whether it contains any major flaws. – Bubaya Feb 13 at 14:37
1

After thinking about it, I figured that the easiest way to archive the filling is just use \vfill and let TeX do the work. Unfortunatly you need to box the content to frame it and \vfill does not work inside a box (expecially not in an \hbox). The solution is to use TikZ and its remember picture and overlay feature to first mark the beginning and then to draw the frame.

Note that you can even reuse the same node name and don't need to number them, which would also be possible.

This works after the second compilation run. The first shows only wrong frames. If you need the enlarge the vertical space for one question you can e.g. place another \vfill at the end of the answer text to make it twice the size of the others.

\documentclass[a4paper]{scrartcl}
\usepackage{tikz}

\newenvironment{questions}{%
    \enumerate
}{%
    \endenumerate
    \newpage
}

\newcommand\question[3]{%
    \item {#1}\hfill{#2}\par\noindent
    \tikz[remember picture, overlay] \node (question start) at (-\fboxsep-\fboxrule, \ht\strutbox+\fboxsep+\fboxrule) {};
    #3\vfill\par\noindent
    \tikz[remember picture, overlay] \draw (\linewidth+\fboxsep+\fboxrule, 0) rectangle (question start);
}

\begin{document}
    Please justify all your answers to the following questions.
    \begin{questions}
        \question{Is this = that?}{2 points}{%
                Bla bla bla.
        }
        \question{Is this = this?}{2 points}{%
                Bla bla bla.
        }
    \end{questions}
    \begin{questions}
        \question{Is this = that?}{2 points}{%
                Bla bla bla.
        }
        \question{Is this = this?}{2 points}{%
                Bla bla bla.
        }
        \question{Is this = there?}{2 points}{%
                Bla bla bla.
        }
    \end{questions}
\end{document}

enter image description here

  • This is indeed a very neat solution! – Bubaya Feb 14 at 9:50
1

This is not a perfect solution but just a starting point. tcolorbox offers option equal height group which makes several boxes to have (after two compilations) the same height of largest (natural height) box in this group. You can assure a certain size to all boxes with minimum for equal height group, therefore combining both options all boxes will have a minimum size no depending on their natural height.

I don't know how much available space on each page, but you can fix a temptative minimum for equal height group that can be fixed after some experiments. And you cal also use height fill option for last box in every page. This option will assure that all vertical space is used.

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

\tcbset{
    colframe=black, colback=white, notitle, before upper={Solution:},
    equal height group=A,   
    minimum for equal height group=A:7cm}
\begin{document}
Please justify all your answers to the following questions.
\begin{enumerate}
\item Is this = that?\hfill 2 points
\begin{tcolorbox}
\end{tcolorbox}
\item Is this = that?\hfill 2 points
\begin{tcolorbox}[height fill]
\end{tcolorbox}
\end{enumerate}

%Following line creates a new group with different size
\tcbset{equal height group=B, minimum for equal height group=B:3cm}

\begin{enumerate}
\item Is this = that?\hfill 2 points
\begin{tcolorbox}
\end{tcolorbox}
\item Is this = that?\hfill 2 points
\begin{tcolorbox}
\end{tcolorbox}
\item Is this = that?\hfill 2 points
\begin{tcolorbox}
\end{tcolorbox}
\item Is this = that?\hfill 2 points
\begin{tcolorbox}[height fill]
\end{tcolorbox}
\end{enumerate}

\end{document}

enter image description here

0

Because I wasn't fully satisfied with tcolorbox, I tried to find an own solution. The following MWE stabilises after two compilations. It writes the available space per box to the aux file and reads them the next turn. However, things get easily messed up, in which case deleting the repective from the aux file helps.

\documentclass[a4paper]{scrartcl}

\makeatletter


\newif\ifresetboxes

% Remaining space on the current page.
\def\restofpage{\dimexpr\pagegoal-\pagetotal-\baselineskip\relax}

% Save available space of current group to the aux file.
\newlength\restofpage@lastrun
\newcounter{vfill@write@groups}
\newcommand{\saverestofpage}{
    \stepcounter{vfill@write@groups}%

    % Name of the macro the rest of the page is to be saved to.
    \def\length@name{restofpage@\roman{vfill@write@groups}}%

    % Rest of this page from last compilation. If undefined or if reset mode,
    % set this to 0pt.
    \expandafter\ifcsname\length@name\endcsname
        \expandafter\expandafter\expandafter\restofpage@lastrun\expandafter\expandafter\expandafter=\expandafter\csname\length@name\endcsname
    \else
        \setlength{\restofpage@lastrun}{0pt}
    \fi%
    \ifresetboxes
        \setlength{\restofpage@lastrun}{0pt}
    \fi

    % Save new rest of page = old rest of page + current rest of page.
    \immediate\write\@auxout{%
        \noexpand\global\noexpand\@namedef{\length@name}{%
            \the\dimexpr
                \restofpage / \thevfillboxes + \restofpage@lastrun
            \relax
        }
    }
    \typeout{Total space available: \the\restofpage}
    \typeout{Boxes on this page: \thevfillboxes}
    \setcounter{vfillboxes}{0}
    \resetboxesfalse
}

% Read available space for the next group from the aux file.
\newlength\remainingspace@perbox
\newcounter{vfill@read@groups}
\newcommand{\getrestofpage}{
    % Define macros for the names values for the current page are to be read from.
    \stepcounter{vfill@read@groups}
    \def\length@name{restofpage@\roman{vfill@read@groups}}

    % Set \remainingspace to the value of the length named by \length@name
    \ifresetboxes
        \setlength{\remainingspace@perbox}{0pt}
    \else
        \setlength{\remainingspace@perbox}{
            \expandafter\ifcsname\length@name\endcsname%
                \expandafter\csname\length@name\endcsname
            \else
                0pt
            \fi
        }
    \fi%

    % Make setting global
    \global\remainingspace@perbox=\remainingspace@perbox%
}



% Produce a box that consumes all available space,
% shared evenly among all boxes of this group.
\newcounter{vfillboxes}
\newcommand{\vfillbox}[2][\dimexpr\linewidth-2\fboxrule-2\fboxsep\relax]{%
    \stepcounter{vfillboxes}%
    \parbox[t][\remainingspace@perbox][t]{#1}{%
        #2
    }%
}

% Same as previous, with additional proportiality factor.
\newcommand{\Vfillbox}[3][\dimexpr\linewidth-2\fboxrule-2\fboxsep]{%
    \addtocounter{vfillboxes}{2}%
    \parbox[t][2\remainingspace@perbox][t]{#1}{%
        #3
    }%
}
\makeatother


\begin{document}
    %\resetboxestrue
    \getrestofpage
    Please justify all your answers to the following questions.
    \begin{enumerate}
        \item Is this = that?\hfill 2 points

        \fbox{%
            \Vfillbox{3}{%
                Bla bla bla.
            }%
        }

        \item Is that = this?\hfill 2 points

        \fbox{%
            \vfillbox{%
                Bla bla bla.
            }%
        }
    \end{enumerate}
    \saverestofpage
    \pagebreak
    \getrestofpage
    \begin{enumerate}
        \item Is this = that?\hfill 2 points

        \fbox{%
            \vfillbox{%
                Bla bla bla.
            }%
        }

        \item Is that = this?\hfill 2 points

        \fbox{%
            \vfillbox{%
                Bla bla bla.
            }%
        }
        \item Is this = that?\hfill 2 points

        \fbox{%
            \vfillbox[\dimexpr\linewidth-2\fboxrule-2\fboxsep]{%
                Bla bla bla.
            }%
        }
        \item Is this = that?\hfill 2 points

        \fbox{%
            \vfillbox[\dimexpr\linewidth-2\fboxrule-2\fboxsep]{%
                Bla bla bla.
            }%
        }
    \end{enumerate}
\saverestofpage
\end{document}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.