如何在固定高度的框中书写?

如何在固定高度的框中书写?

我正在使用report类并一直使用它\framebox[\linewidth]{\rule{0pt}{2cm}}来创建一个具有一定高度的空框。现在,我想在那个空框中写入。通过引用如何使用 \makeemptybox{1.8in} 在框中写字?,我取得了以下成果:

\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}

如您所见,\makenoemptybox与 相比, 的垂直空间更大\framebox(参见彩色箭头)。我如何才能使 形成的垂直空间与 的垂直空间\makenoemptybox一致\framebox

在此处输入图片描述

答案1

在定义中使用\makenonemptybox与 for 相同的方法来\framebox关闭当前段落。当然,这会限制其在列表环境中的使用。

\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}

在此处输入图片描述

答案2

\par 和 aminipage起作用:

\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}

相关内容