用边框/框架封闭领导者

用边框/框架封闭领导者

后续行动这个答案,我该如何编辑命令\FillAnswerRules以使用或用边框/框架将引线围起来tocolorboxmdframed以便获得相同的输出\CountAnswerRules

在此处输入图片描述

\documentclass{exam}

\usepackage{xparse,xcolor,tcolorbox,mdframed,pgffor,blindtext}

\renewcommand{\questionshook}{%
    \setlength{\leftmargin}{0pt}%
    \setlength{\labelwidth}{-\labelsep}%
    \setlength{\topsep}{0\baselineskip}
}

\ExplSyntaxOn
\keys_define:nn { diaa / answers }
{
    num-lines .int_set:N = \l_diaa_answers_numlines_int ,
    d .dim_set:N = \l__diaa_answers_distance_dim,
    t .dim_set:N = \l__diaa_answers_thickness_dim,
    c .tl_set:N  = \l__diaa_answers_color_tl,
    num-lines .initial:n = 3,
    d .initial:n = 10mm,
    t .initial:n = 0.2pt,
    c .initial:n = gray,
}

\NewDocumentCommand{\FillAnswerRules}{O{}}
{
    \group_begin:
    \par
    \keys_set:nn { diaa / answers } { #1 }
    \leaders \hbox:n
    {
        \makebox[\textwidth][s]{
            \color{\l__diaa_answers_color_tl}
            \vrule width 0pt height \l__diaa_answers_distance_dim % the distance
            \leaders\hrule height \l__diaa_answers_thickness_dim\hfill
        }
    }\vfill
    \clearpage
    \group_end:
}

\NewDocumentCommand{\CountAnswerRules}{O{}}
{
    \keys_set:nn { diaa / answers } { #1 }

    \begin{mdframed}[
        innerrightmargin = 10pt, innertopmargin = 20pt,
        innerleftmargin = 10pt, innerbottommargin = 20pt,
        leftmargin = 0pt,
        rightmargin = 0pt,
        linecolor=black, linewidth = 2pt]

        \setlength{\parskip}{0pt}
        \setlength{\baselineskip}{\l__diaa_answers_distance_dim}
        \foreach \i in {1,...,\l_diaa_answers_numlines_int} {\color{\l__diaa_answers_color_tl}%
            \noindent\rule{\linewidth}{\l__diaa_answers_thickness_dim}\par
        }

    \end{mdframed}
}

\ExplSyntaxOff

\begin{document}
    \begin{questions}
        \question short question \CountAnswerRules[d = 20pt]
        \question \blindtext[1]\FillAnswerRules[t = 1pt]
    \end{questions}
\end{document}

答案1

您可以使用tcolorbox height filltext fill选项来实现这一点。在下面的代码中,我将领导者包装在由以下定义的环境\FillAnswerRules中:tcolorbox

\NewTColorBox { fillAnswerRules } { O{} }
  {
    height~fill, text~fill, colback=white, boxrule=2pt, sharp~corners,
    boxsep=0pt, left=10pt, right=10pt, top=8pt, bottom=0pt, #1
  }

波浪线 ( ~) 在此处代表空格,因为我在\ExplSyntaxOn和之间这样做\ExplSyntaxOff;否则,您将使用正常空格代替波浪线。请注意,我指定了选项sharp corners以完美匹配框的外观mdframed,但tcolorbox我认为默认的圆角看起来更好。

我还修改了外部引线内的代码,以便框内的上部规则不会离框顶部太远,下部规则不会离框底部太近。这对应于以下几行:

\vrule width 0pt height 0.5\l__diaa_answers_distance_dim
       depth 0.5\l__diaa_answers_distance_dim
\leaders \hrule height \l__diaa_answers_thickness_dim \hfill

完整代码如下:

\documentclass{exam}
\usepackage{xparse,xcolor,tcolorbox,mdframed,pgffor,blindtext}
% The tcolorbox library 'breakable' is needed for /tcb/height fill; 'xparse' is
% not necessary here, but convenient and powerful (the use of \NewTColorBox
% may prove useful for future evolutions of the code).
\tcbuselibrary{breakable, xparse}

\renewcommand{\questionshook}{%
    \setlength{\leftmargin}{0pt}%
    \setlength{\labelwidth}{-\labelsep}%
    \setlength{\topsep}{0\baselineskip}
}

\ExplSyntaxOn

\keys_define:nn { diaa / answers }
{
    num-lines .int_set:N = \l_diaa_answers_numlines_int ,
    d .dim_set:N = \l__diaa_answers_distance_dim,
    t .dim_set:N = \l__diaa_answers_thickness_dim,
    c .tl_set:N  = \l__diaa_answers_color_tl,
    num-lines .initial:n = 3,
    d .initial:n = 10mm,
    t .initial:n = 0.2pt,
    c .initial:n = gray,
}

\NewTColorBox { fillAnswerRules } { O{} }
  {
    height~fill, text~fill, colback=white, boxrule=2pt, sharp~corners,
    boxsep=0pt, left=10pt, right=10pt, top=8pt, bottom=0pt, #1
  }

\NewDocumentCommand{\FillAnswerRules}{O{}}
{
  \begin{fillAnswerRules}
    \keys_set:nn { diaa / answers } { #1 }
    \leaders \hbox:n
      {
        \makebox[\textwidth][s]
          {
            \color{\l__diaa_answers_color_tl}
            % Vertically center the following \leaders inside a “line” that
            % is \l__diaa_answers_distance_dim high.
            \vrule width 0pt height 0.5\l__diaa_answers_distance_dim
                   depth 0.5\l__diaa_answers_distance_dim
            \leaders \hrule height \l__diaa_answers_thickness_dim \hfill
          }
      }\vfill
    \null
  \end{fillAnswerRules}
}

\NewDocumentCommand{\CountAnswerRules}{O{}}
{
    \keys_set:nn { diaa / answers } { #1 }

    \begin{mdframed}[
        innerrightmargin = 10pt, innertopmargin = 20pt,
        innerleftmargin = 10pt, innerbottommargin = 20pt,
        leftmargin = 0pt,
        rightmargin = 0pt,
        linecolor=black, linewidth = 2pt]

        \setlength{\parskip}{0pt}
        \setlength{\baselineskip}{\l__diaa_answers_distance_dim}
        \foreach \i in {1,...,\l_diaa_answers_numlines_int}
          {
            \color{\l__diaa_answers_color_tl}
            \noindent\rule{\linewidth}{\l__diaa_answers_thickness_dim}\par
          }

    \end{mdframed}
}

\ExplSyntaxOff

\begin{document}
  \begin{questions}
    \question Short question \CountAnswerRules[d=20pt, t=1pt]
    \question \blindtext[1]\FillAnswerRules[d=20pt, t=1pt]
  \end{questions}
\end{document}

在此处输入图片描述

如果您替换:

\vrule width 0pt height 0.5\l__diaa_answers_distance_dim
       depth 0.5\l__diaa_answers_distance_dim

与原版

\vrule width 0pt height \l__diaa_answers_distance_dim

第二个框如下所示:

在此处输入图片描述

top=8pt, bottom=0pt如果进一步从...的选项中删除\NewTColorBox { fillAnswerRules } { O{} } {...},则第二个框将变成:

在此处输入图片描述

在我看来,完整的例子看起来更好。

相关内容