[Tex/LaTex] Box with same vertical height, even when empty

boxesspacing

I'm trying to draw a vertical box around one or more lines of text (for a field in a pre-filled form), but some of the fields may be empty and in these cases, the box should be the same vertical height as when there is only one line (or even just one character) of content within the box.

I am currently defining a \field command for this, like so:

\newcommand{\field}[1]{
    \framebox[\textwidth][l]{        
        \parbox[l]{0.96\textwidth}{
            \raggedright #1 \addvspace{0.1\baselineskip}
        }
    }
}

But this gives me a 'missing item' error because \addvspace isn't in vertical mode. If I add a \par to fix this, the box has too much vertical padding after the last line of text. If I remove the \addvspace entirely then the box is not tall enough when it contains no text.

Could someone please suggest the best way of creating a box, the full width of the page, that is a minimum of one line of text high (even when empty) but can expand to contain multiple lines of text?

Best Answer

Typically one would include a \strut at the beginning. Note also that you have some spurious spaces in your macro definition (you need some strategic placements of %). See What is the use of percent signs (%) at the end of lines? I'm also guessing your use of .96\textwidth results from some overfull \hboxes. You can adjust this width to fit perfectly within the text block as well...

Here's perhaps something that you're after:

\newcommand{\field}[1]{%
  \noindent%
  \framebox[\linewidth][l]{%
    \parbox[t]{\dimexpr\linewidth-2\fboxsep-2\fboxrule}{%
      \raggedright\strut#1%
    }%
  }%
}

Here is its use in a MWE:

enter image description here

\documentclass{article}
\newcommand{\field}[1]{%
  \noindent%
  \framebox[\linewidth][l]{%
    \parbox[t]{\dimexpr\linewidth-2\fboxsep-2\fboxrule}{%
      \raggedright\strut#1%
    }%
  }%
}

\begin{document}
\field{Some test}

\field{}

\field{I'm trying to draw a vertical box around one or more lines of
  text (for a field in a pre-filled form), but some of the fields may
  be empty and in these cases, the box should be the same vertical 
  height as when there is only one line (or even just one character) 
  of content within the box.}
\end{document}

Depending on your usage, you may also consider ending with a \strut, allowing for adequate space if the last line has no descender.