6

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?

5

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.

  • Perfect, exactly what I was hoping for - many thanks! – Malvineous Jul 19 '13 at 1:33
  • Is it possible to have a list inside this environment without top spacing. I've been using enumitem to embed a list as ``` \field{% \begin{itemize}[topsep=0pt,partopsep=0pt] \item Lorem \item Ipsum \end{itemize} } ``` But I still get a top vertical spacing. Is it because of something I didn't understand in the field environment? – Vser Jul 28 '16 at 13:19
  • @Werner I have part of my answer: it seems to depend on the environment since outside the environment I do not have the top spacing. After some trials, it seems to be because of the \strut. What will it break to remove it? – Vser Jul 28 '16 at 13:26
  • I have my answer again… And empty box won't have the desired height. So I ended with an if/else clause with the strut only if there is no list given. – Vser Jul 28 '16 at 13:30

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.