7

I am using report class and have been using \framebox[\linewidth]{\rule{0pt}{2cm}} to create an empty box with a certain height. Now, I would like to write in that empty box. By referring to How can I write something in a box with \makeemptybox{1.8in}?, I achieved the following:

\documentclass[a4paper,11pt]{report}

\newcommand{\makenonemptybox}[2]{%
\par\nobreak\vspace{\ht\strutbox}\noindent
\fbox{%
\parbox[c][\dimexpr#1-2\fboxsep][t]{\dimexpr\linewidth-2\fboxsep}{
  \hrule width \hsize height 0pt
  #2
 }%
}%
\par\vspace{\ht\strutbox}
}
\makeatother

\begin{document}
\begin{enumerate}
\item Tell me about yourself.
    \begin{enumerate}
    \item What is your name?
    \makenonemptybox{2cm}{My name is Nadia.}
    \item Where are you from?
    \makenonemptybox{2cm}{Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam a venenatis lacus. Nunc vitae mollis neque. Maecenas vel arcu erat.}
    \item How old are you?
    \item[] \framebox[\linewidth]{\rule{0pt}{2cm}}
    \end{enumerate}
\end{enumerate}

\end{document}

As you can see \makenoemptybox makes a bigger vertical space compared to \framebox (refer to the coloured arrows). How can I make the vertical space made by \makenoemptybox follows that of \framebox?

enter image description here

  • Related Question: mdframed box of fixed minimum height. – Peter Grill Sep 8 '18 at 7:05
  • Perhaps I am missing something, but if you just want an empty box of the same size, why not just use \makenonemptybox{2cm}{}? – Peter Grill Sep 8 '18 at 7:08
  • @PeterGrill I've just edited the picture. – Nadia Sep 8 '18 at 7:47
  • 1
    side remark: as got clarified later the \parbox[c][\dimexpr#1-2\fboxsep] in \makenonemptybox should have been \parbox[c][#1] to start with, because when \framebox[\linewidth]{\rule{0pt}{2cm}} is used the total height is 2cm + 2\fboxsep + 2\fboxrule. To do the same with \makenonemptybox for the height it thus should be \parbox[c][#1] when #1 is 2cm. Besides, removing \fboxsep makes no sense if not removing also \fboxrule, then total height including frame and separation would be 2cm. But \framebox[\linewidth]{\rule{0pt}{2cm}} gives 2cm+2\fboxsep+2\fboxrule. – user4686 Sep 8 '18 at 9:31
7

Use in the definition of \makenonemptybox the same method you use for \framebox to close current paragraph. Of course, that will restrict its usage to list environments.

\documentclass[a4paper,11pt]{report}

\newcommand{\makenonemptybox}[2]{%
%\par\nobreak\vspace{\ht\strutbox}\noindent
\item[]
\fbox{% added -2\fboxrule to specified width to avoid overfull hboxes
% and removed the -2\fboxsep from height specification (image not updated)
% because in MWE 2cm is should be height of contents excluding sep and frame
\parbox[c][#1][t]{\dimexpr\linewidth-2\fboxsep-2\fboxrule}{
  \hrule width \hsize height 0pt
  #2
 }%
}%
\par\vspace{\ht\strutbox}
}
\makeatother

\begin{document}
\begin{enumerate}
\item Tell me about yourself.
    \begin{enumerate}
    \item What is your name?
          \makenonemptybox{2cm}{My name is Nadia.}
    \item Where are you from?
          \makenonemptybox{2cm}{Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam a venenatis lacus. Nunc vitae mollis neque. Maecenas vel arcu erat.}
    \item How old are you?
    \item[] \framebox[\linewidth]{\rule{0pt}{2cm}}
    \end{enumerate}
\end{enumerate}

\end{document}

enter image description here

|improve this answer|||||
  • 1
    you could also drop the \item[] from the macro definition and use it explicitely as input mark-up like you do before the \framebox. Then the macro could be used in other contexts, with some adapted prefix such as a \par. – user4686 Sep 8 '18 at 8:12
  • Why does \makenonemptybox give me overfull \hbox? – Nadia Sep 8 '18 at 8:55
  • @Nadia because the original code forgot to reduce width by 2\fboxrule. I will add that. – user4686 Sep 8 '18 at 9:10
  • Added also -2\fboxrule to height specification. – user4686 Sep 8 '18 at 9:13
  • wait.... why are 2\fboxsep removed from height specs to start with? they should not because 2cm is used by the \rule in \framebox. I will remove them (and the 2\fboxrule I temporarily added) – user4686 Sep 8 '18 at 9:17
0

A \par and a minipage works:

\documentclass[a4paper,11pt]{report}
\newcommand{\makenonemptybox}[2]{%
\par{\fboxsep1ex\fbox{%
\begin{minipage}[c][#1][t]{\dimexpr\linewidth-2\fboxsep-2\fboxrule}%
#2\end{minipage}}}}}%
\begin{document}
\begin{enumerate}
\item Tell me about yourself.
    \begin{enumerate}
    \item What is your name?
    \makenonemptybox{2cm}{My name is Nadia.}
    \item Where are you from?
    \makenonemptybox{2cm}{Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam a venenatis lacus. Nunc vitae mollis neque. Maecenas vel arcu erat.}
    \item How old are you?
    \item[] \framebox[\linewidth]{\rule{0pt}{2cm}}
    \end{enumerate}
\end{enumerate}
\end{document}
|improve this answer|||||

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.