在 exsheets 计数器格式中使用颜色会导致错误

在 exsheets 计数器格式中使用颜色会导致错误

使用该exsheets包,我定义了一种新的问题类型,该问题类型应通过彩色数字来区分。以下 MWE 抛出错误消息“ Use of \SaveCounterPattern doesn't match its definition”。但是——意外发现——当我包含该siunitx包(我实际上并不想要/需要)时,错误消失了。有没有更好的方法来设置颜色,这样我就不需要包含siunitx?(或者甚至只是一个“修复”事情的好理由siunitx?)

\documentclass{article}

\usepackage{color}
%\usepackage{siunitx}

\usepackage{exsheets}

\NewQuSolPair
    {questionqr}[name=QR Question,pre-hook={\SetupExSheets{counter-format=
    {\color{blue}qu[1]}}},post-hook={\SetupExSheets{counter-format={qu[1]}}} ]
    {solutionqr}[name=QR Solution]          

\begin{document}

\begin{question}
    First question.
\end{question}

\begin{questionqr}
    Second question.
\end{questionqr}

\end{document}

答案1

添加\robustify\color到您的序言中(在加载的包\robustify中定义)etoolboxexsheets

\documentclass{article}

\usepackage{color,etoolbox} % etoolbox is loaded by exsheets anyway
\robustify\color

\usepackage{exsheets}

\NewQuSolPair
    {questionqr}[name=QR Question,pre-hook={\SetupExSheets{counter-format=
    {\color{blue}qu[1]}}},post-hook={\SetupExSheets{counter-format={qu[1]}}} ]
    {solutionqr}[name=QR Solution]          

\begin{document}

\begin{question}
    First question.
\end{question}

\begin{questionqr}
    Second question.
\end{questionqr}

\end{document}

在此处输入图片描述


使用exsheets'继任者xsim

\documentclass{article}

\usepackage{xcolor}
\usepackage{xsim}

\DeclareExerciseType{QR}{
  exercise-env      = questionqr ,
  solution-env      = solutionqr ,
  exercise-name     = QR Question ,
  solution-name     = QR Solution ,
  counter           = exercise , % <<< leave away if the new type should be
                                 % numbered independently
  exercise-template = bluenumber ,
  solution-template = bluenumber
}

\DeclareExerciseEnvironmentTemplate{bluenumber}
  {%
    \subsection*
      {%
        \GetExerciseName\nobreakspace
        \textcolor{blue}{\GetExerciseProperty{counter}}%
        \IfInsideSolutionF
          {%
            \GetExercisePropertyT{subtitle}
              { {\normalfont\itshape\PropertyValue}}%
          }%
      }%
  }
  {}

\begin{document}

\begin{exercise}
  First question.
\end{exercise}

\begin{questionqr}
  Second question.
\end{questionqr}

\end{document}

在此处输入图片描述

答案2

这里是防止出现错误的包的“精简”版本siunitx。似乎siunitx使它受到\color保护,因此可以用它来为问题着色。

\documentclass{article}
\usepackage{exsheets}
\usepackage{xcolor}

\usepackage{expl3}[2015/09/11]
\usepackage{xparse}

\ExplSyntaxOn

\AtBeginDocument {
  \tl_map_function:nN { \color }
    \__siunitx_protect_symbols:N
}
\cs_new_protected:Npn \__siunitx_protect_symbols:N #1 {
   \cs_set_protected:Npx #1 { \exp_not:V #1 } 
}
\ExplSyntaxOff


\NewQuSolPair
    {questionqr}[name=QR Question,pre-hook={\SetupExSheets{counter-format=
    {\color{red}qu[1]}}},post-hook={\SetupExSheets{counter-format={qu[1]}}} ]
    {solutionqr}[name=QR Solution]          

\begin{document}

\begin{question}
    First question.
\end{question}

\begin{questionqr}
    Second question.
\end{questionqr}

\end{document}

相关内容