[Tex/LaTex] Variable box size from input text

boxes

How can I make a framed box such that, when spacing is added in the correct places, the box height is exactly 15 cm? Explicitly, how do I define a \newcommand taking in three inputs {A}{B}{C} and displaying a framed box with the value of x computed from the sizes of A, B and C from below to make the box exactly 15cm:

A
\vspace{x}
B
\vspace{x}
C

As I'm trying to learn one thing at a time, a LaTeX answer is preferred, though feel free to post other solutions!

Best Answer

You could use \vfill for filling the space, stretching the content to the full height, such as

\documentclass{article}
\newcommand*{\mybox}[3]{%
  \fbox{
    \parbox[t][15cm]{\textwidth}{%
      #1
      \vfill
      #2
      \vfill
      #3
    }
  }
}
\begin{document}
\noindent
\mybox{A}{B}{C}
\end{document}

I used the optional height argument for \parbox and t for top alignment. Though top alignment is the default, id doesn't work without. The syntax of \parbox with all optional arguments is

\parbox[position][height][inner-pos]{width}{text}

The parbox, which means the inner box, has the desired size. If you would like to have an "outer" height of 15 cm, you could calculate the inner height using the calc package.

\vfill is equivalent to \vspace{\fill} and inserts space that can stretch as much as possible, so filling the parbox. Inserted twice they share the available space equally.

framed box

Related Question