考试:创建一个解决方案环境,其宽度由与问题级别对应的可选参数设置

考试:创建一个解决方案环境,其宽度由与问题级别对应的可选参数设置

灵感来自这个答案,我希望有一个自定义的解决方案环境,它接受一个可选参数。此参数可以是数字或字母,其值对应于需要将其宽度设置为解决方案环境的问题级别。

例如,假设问题级别有一些索引:1问题、2零件、3子零件和4子子零件。那么新的解决方案环境可能类似于\begin{solution}[2]如果我希望解决方案跨越零件环境的整个线宽,而不管我在哪里打印这个解决方案。

最后,如果我不输入可选参数,我希望宽度由父问题级别设置。

\documentclass[answers]{exam}
\usepackage{lipsum}
\usepackage{mdframed}

\renewenvironment{solution}
{\begingroup\par\parshape0%
\begin{mdframed}[skipabove=\baselineskip,
                 innertopmargin=\baselineskip,
                 innerbottommargin=\baselineskip,
                 userdefinedwidth=\textwidth]
\textbf{Solution:}\enspace\ignorespaces}
{\end{mdframed}\par\endgroup}

\begin{document}
    \begin{questions}
        \question A question.
        \begin{parts}
            \part part
            \begin{solution}
                The long solution continues onto another page: \lipsum[1-8]
            \end{solution}
        \end{parts}
    \end{questions}

    \lipsum[1]
\end{document}

答案1

您想要的已经作为一个包实现,以便在多个文档之间干净地重用:该包被调用examwsolns并且可以找到这里. 文档也在那里。

示例用法

以下example.tex文件展示了如何使用该包:

\documentclass[answers]{exam}
\usepackage[framemethod=tikz]{mdframed}
%\usepackage[replace-solution-env]{examwsolns}
\usepackage{examwsolns}
\usepackage{xcolor}
\usepackage{lipsum}
\usepackage{tikz}

% Also possible in the preamble:
% \examwsolnsSetup{replace-solution-env}
% \examwsolnsSetup{replace-solution-env=true}

% Default additional arguments to pass to the 'mdframed' environment
\examwsolnsSetMdFramedDefaultArgs{
  skipabove = \baselineskip,
  innertopmargin = \baselineskip,
  innerbottommargin = \baselineskip,
  userdefinedwidth = \linewidth
}

\begin{document}
Here is a full line:
\par\noindent\hrulefill

\begin{wsolution}
  Autodetected outer level.
\end{wsolution}

\begin{questions}
  \question A question.

    \begin{wsolution}
      Autodetected question level.
    \end{wsolution}

    \begin{parts}
       \part A part
         \begin{wsolution}[][innertopmargin = 2\baselineskip,
                             innerbottommargin = 2\baselineskip,
                             backgroundcolor=gray!40,
                             roundcorner=8pt]
           Autodetected part level.
         \end{wsolution}

         \begin{subparts}
            \subpart A subpart
              \begin{wsolution}
                Autodetected subpart level.
              \end{wsolution}

             \subpart
               \begin{subsubparts}
                 \subsubpart A subsubpart
                   \begin{wsolution}
                     Autodetected subsubpart level.
                   \end{wsolution}

                 \subsubpart Another subsubpart
                   \begin{wsolution}
                     Autodetected subsubpart level.
                   \end{wsolution}
               \end{subsubparts}
          \end{subparts}

       \part Other part
         \begin{wsolution}
           Autodetected part level.
         \end{wsolution}
    \end{parts}

  \question Other question
    \begin{wsolution}
      Autodetected question level.
    \end{wsolution}

    \begin{parts}
      \part A part
        \begin{wsolution}[1]
          Forced at level 1.
        \end{wsolution}

        \begin{wsolution}[2]
          Forced at level 2.
        \end{wsolution}

        \begin{wsolution}[3]
          Forced at level 3. \lipsum[2-8]
        \end{wsolution}

        \begin{wsolution}[4]
          Forced at level 4.

          \lipsum[9]\lipsum[10][1-2]
        \end{wsolution}

        {% \examwsolnsSetMdFramedPreText and \examwsolnsSetMdFramedPostText
         % respect TeX's grouping rules.
          \examwsolnsSetMdFramedPreText{\emph{Sample pre-text} $\langle$}%
          \examwsolnsSetMdFramedPostText{$\rangle$ \emph{Sample post-text}}%
        \begin{wsolution}[3]
          Forced at level 3.
        \end{wsolution}
        }

        \begin{wsolution}[2]
          Forced at level 2.
        \end{wsolution}
    \end{parts}

  \question Last question
    \begin{wsolution}[1]
      Forced at level 1.
    \end{wsolution}

    \begin{wsolution}[0]
      Forced at level 0.
    \end{wsolution}
\end{questions}

Here is a full line for comparison:
\par\noindent\hrulefill
\end{document}

上面给出的 example.tex 文件的输出

第 1 页:

第 1 页的截图


第2页:

第 2 页的截图


第 3 页:

第 3 页的截图

相关内容