\parbox 高度可伸缩

\parbox 高度可伸缩

我正在尝试创建一个\parbox可以拉伸以填充可用空间的框架。我尝试过

\fbox{\parbox[c][0pt plus 1fill][t]{3in}{...Content...}}

\parbox似乎根本没有拉伸。 有没有办法将 设置\parbox为可拉伸高度? 或者,有没有办法保存一个空的可拉伸框的尺寸,然后使用一些技巧,明确地将 设置为\parbox如果可以拉伸则应具有的高度?

答案1

这与\parbox和重叠\vfill。它还使用 TikZ 和\tikzmark在其周围绘制一个框架。

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{tikzmark,calc}

\usepackage{lipsum}% MWE only

\newcounter{boxnumber}% for unique tikzmark names

\newcommand{\stretchable}[1]{\par\stepcounter{boxnumber}%
  \strut\raisebox{0pt}[0pt][0pt]{\parbox[t]{3in}{#1}}%
  \begin{tikzpicture}[overlay,remember picture]
    \coordinate (top) at (0,\ht\strutbox);
    \coordinate (bottom) at (pic cs:boxnumber.\theboxnumber);
    \draw ($(top)+(2pt,2pt)$) rectangle ($(bottom)+(-2pt,-2pt)$);
  \end{tikzpicture}\par
  \vfill\tikzmark{boxnumber.\theboxnumber}\par}
  
\begin{document}

Some text before the box.

\stretchable{\lipsum[1]}

Some text between the boxes.

\stretchable{\lipsum[2]}

Bottom of page.
\end{document}

当然,在考试课上你可以使用\begin{solutionbox}{\stretch{1}} ... \end{solutionbox}

\documentclass{exam}
%\printanswers
\begin{document}
\begin{questions}

\question How high is up?

\begin{solutionbox}{\stretch{1}}
More than you can reach.
\end{solutionbox}

\question Why?

\begin{solutionbox}{\stretch{1}}
Why not?
\end{solutionbox}

\end{questions}
\end{document}

答案2

根据@John Kormylo的回答,我得到了一些代码,允许将宽度和高度设置为任何有效的skips。我对如何从 计算框的大小不满意tikzmarks,而且我觉得我正在重新创建 的部分tcolorbox,但这里是...

\documentclass[12pt]{exam}

\usepackage{expl3}
\usepackage{tikz}
    \usetikzlibrary{tikzmark}
    \usetikzlibrary{math}
    \usetikzlibrary{calc}

\usepackage{lipsum}

\pagestyle{empty}

% Compute the size of the box with named top and bottom tikzmarker's.
% This cannot be done using Expl3 syntax because it relies on tikz understanding the ":" character.
\NewDocumentCommand{\ComputeBoxDims}{m m}{%
    \begin{tikzpicture}[overlay,remember picture]
        % The 0.7\baselineskip is the default height of \strut.
        % This sets the top of the box to be right above the height of the text.
        \coordinate (top) at ($(pic cs:#1)+(0,0.7\baselineskip)$);
        \coordinate (bottom) at (pic cs:#2);
            \draw[very thick, black, fill=blue!10!white] ($(top)$) rectangle ($(bottom)$);
            % Compute the actual width and height of the box.
            \tikzmath{
                coordinate \C;
                \C = (bottom) - (top);
                \Cwidth = abs(\Cx);
                \Cheight = abs(\Cy);
            }
        \global\let\gTmpBoxWidth\Cwidth
        \global\let\gTmpBoxHeight\Cheight
    \end{tikzpicture}%
}

\ExplSyntaxOn
% Make a box of zero size whose content is still shown
% I'm not sure how to do this with only expl3 boxes...
\NewDocumentCommand{\zero_area_box}{+m}{
    \makebox[0pt][l]{
        \raisebox{0pt}[0pt][0pt]{
            \vbox_top:n {
                #1
            }
        }
    }
}

% A box that takes up no space and will be negatively offset
% by the given width and height
\NewDocumentCommand{\offset_box}{m m +m}{
    \zero_area_box{\vspace{-#2}\hspace{-#1}\zero_area_box{#3}}
}


\skip_new:N \l_fullbox_skip_preferred_width
\skip_new:N \l_fullbox_skip_preferred_height
\dim_new:N \l_fullbox_dim_padding
\dim_new:N \l_fullbox_dim_box_width
\dim_new:N \l_fullbox_dim_box_height
\keys_define:nn { fullbox } {
    width .skip_set:N = \l_fullbox_skip_preferred_width,
    width .initial:n = {0pt plus 1fill},
    height .skip_set:N = \l_fullbox_skip_preferred_height,
    height .initial:n = {0pt plus 1fill},
    padding .skip_set:N = \l_fullbox_dim_padding,
    padding .initial:n = {0.3\baselineskip},
}
\newcounter{fullbox_counter}
\NewDocumentCommand{\fullbox}{o +m}{
    \IfValueTF {#1}{
        \keys_set:nn { fullbox } {#1}
    }{
        \keys_set:nn { fullbox } {}
    }

    \stepcounter{fullbox_counter}
    \tikzmark{fullbox_top.\thefullbox_counter}
    \skip_vertical:N \l_fullbox_skip_preferred_height
    \skip_horizontal:N \l_fullbox_skip_preferred_width
    \tikzmark{fullbox_bottom.\thefullbox_counter}

    % For some reason the tikz picture has a non-zero size,
    % so we stuff it in a size-zero box.
    \zero_area_box{
        \ComputeBoxDims
            {fullbox_top.\thefullbox_counter}
            {fullbox_bottom.\thefullbox_counter}
    }
    \dim_gset:Nn \l_fullbox_dim_box_width {\gTmpBoxWidth pt}
    \dim_gset:Nn \l_fullbox_dim_box_height {\gTmpBoxHeight pt}
    \offset_box
        {
            \dim_eval:n {\l_fullbox_dim_box_width - \l_fullbox_dim_padding}
        }
        {
            \dim_eval:n {\l_fullbox_dim_box_height - \l_fullbox_dim_padding}
        }
        {
            \parbox[l][0pt]{
                \dim_eval:n {\l_fullbox_dim_box_width - 2\l_fullbox_dim_padding}
            }{#2}
        }
}
\ExplSyntaxOff

\begin{document}
    \begin{questions}
        \question Answer well.
        \begin{parts}
            \part What do you think?

            \fullbox{}
            \part What do you think?

            \fullbox[height=\stretch{2}]{
                \parskip=5pt
                \lipsum[1-2]
            }
            \part Last question...

            \fullbox[height=1in]{}
        \end{parts}
    \end{questions}
\end{document}

示例渲染

相关内容