考试:创建一个 \NewDocumentCommand 来动态更改问题格式

考试:创建一个 \NewDocumentCommand 来动态更改问题格式

我需要创建一个命令\QuestionFormat,该命令接受两个可选参数,第一个参数具有默认值,同时测试第二个参数是否存在。

下面的代码不起作用,而文档中每一行下面都写着所需的输出。那么,这里出了什么问题?

\documentclass[addpoints]{exam}

\usepackage{xparse}
\usepackage[xparse]{tcolorbox}

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

\ExplSyntaxOn

\NewTColorBox { MarksTCBox } { O{} }
{
    left~skip= 0pt,right~skip=0pt, left=2pt,right=2pt, capture=hbox,
halign=center, valign=center, boxrule=0pt, arc=0pt, top=2pt,
bottom=2pt, boxsep=0pt, colback=lg, nobeforeafter, box~align = base, baseline=4pt, #1
}

\NewDocumentCommand \QuestionFormat { O{Question} O{} }
{
    \tl_if_blank:nTF {#2}
    {\qformat{\textbf{%
                \underline{%
                    {%
                        \large #1 (\thequestion) 
                        \begin{MarksTCBox}
                            [\totalpoints\ Marks]
                        \end{MarksTCBox}%
                    }%
                }%
            }\hfill%
    }}
    {\qformat{\textbf{%
                \underline{%
                    {%
                        \large #1 (\thequestion) (#2)
                        \begin{MarksTCBox}
                            [\totalpoints\ Marks]
                        \end{MarksTCBox}%
                    }%
                }%
            }%\hfill%
    }}%
}
\ExplSyntaxOff


\begin{document}
    \begin{questions}

        \QuestionFormat
        \question[15]\hspace*{0pt}\vspace*{\baselineskip}
        The output should be ``Question 1 [15 Marks]"

        \QuestionFormat[Part]
        \question[10]\hspace*{0pt}\vspace*{\baselineskip}
        The output should be ``Part 2 [10 Marks]"

        \QuestionFormat[Part][Subtitle]
        \question[5]\hspace*{0pt}\vspace*{\baselineskip}
        The output should be ``Part 3 [5 Marks] (Subtitle)"

        \QuestionFormat[][Subtitle]
        \question[10]\hspace*{0pt}\vspace*{\baselineskip}
        The output should be ``Question 4 [10 Marks] (Subtitle)"

    \end{questions}
\end{document}

答案1

你不需要\ExplSyntaxOn到处都是。

\documentclass[addpoints]{exam}

\usepackage{xparse}
\usepackage[xparse]{tcolorbox}

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

\NewTColorBox{MarksTCBox} { O{} }{
  left skip= 0pt,
  right skip=0pt,
  left=2pt,
  right=2pt,
  capture=hbox,
  halign=center,
  valign=center,
  boxrule=0pt,
  arc=0pt,
  top=2pt,
  bottom=2pt,
  boxsep=0pt,
%  colback=lg,
  nobeforeafter,
  box align = base,
  baseline=4pt,
  #1
}

\ExplSyntaxOn
\NewDocumentCommand \QuestionFormat { O{Question} o }{%
  \qformat{
    \textbf{
      \underline{
        \large \tl_if_blank:nTF {#1} { Question } { #1 }~
        (\thequestion)\ \IfValueT{#2}{(#2)\ }
        \begin{MarksTCBox}
        \scan_stop: [\totalpoints\ Marks]
        \end{MarksTCBox}
      }
    }
  }
}
\ExplSyntaxOff

\begin{document}
    \begin{questions}

        \QuestionFormat
        \question[15]\hspace*{0pt}\vspace*{\baselineskip}
        The output should be ``Question 1 [15 Marks]''

        \QuestionFormat[Part]
        \question[10]\hspace*{0pt}\vspace*{\baselineskip}
        The output should be ``Part 2 [10 Marks]''

        \QuestionFormat[Part][Subtitle]
        \question[5]\hspace*{0pt}\vspace*{\baselineskip}
        The output should be ``Part 3 [5 Marks] (Subtitle)''

        \QuestionFormat[][Subtitle]
        \question[10]\hspace*{0pt}\vspace*{\baselineskip}
        The output should be ``Question 4 [10 Marks] (Subtitle)''

    \end{questions}
\end{document}

请注意\scan_stop:,这会阻止 LaTeX 认为这[会启动一个可选参数。还请注意如何使用参数o说明符和来避免代码重复\IfValueT

在此处输入图片描述

我将其注释掉,colback=lg因为缺少颜色定义。

顺便问一下,需要加下划线吗?

相关内容