在自定义环境中使用新命令

在自定义环境中使用新命令

正如您所看到的,MWE 如下:

\documentclass[twoside,a4paper,cleardoublepage=empty,14pt]{book}
\usepackage{exsheets}
\SetupExSheets[question]{type=exam}
\SetupExSheets{counter-format=ch.qu[1]}
\RenewQuSolPair{question}[headings-format=\large\bfseries,name=Problem]{solution}
\usepackage{enumerate,tikz}
\usepackage{environ}

\newcommand*\squared[1]{\tikz[baseline=(char.base)]{
        \node[shape=rectangle,draw,inner sep=3pt] (char) {#1};}}
\usepackage{enumitem}
\usepackage{multicol}
\setlist[enumerate]{label=\protect\squared{\arabic*}}
\newcounter{correctchoice}

\makeatletter
\NewEnviron{correctchoice}{\refstepcounter{correctchoice}%
    \edef\@currentlabel{\thechapter.\thequestion~\BODY}%
    \label{correctchoice:\thehint}%
}
\newcommand\printmycorrectchoice{% you could use \foreach, but this is faster
    \bgroup% use local registers
    \count1=\value{correctchoice}% a macro would also work
    \setcounter{correctchoice}{0}%
    \loop\ifnum\value{correctchoice}<\count1
    \stepcounter{correctchoice}%
    \noindent\ref{correctchoice:\thehint}\par
    \repeat
    \egroup}

\makeatother

\newcommand{\bubble}[1]% #1 is correct choice (out of 4)
{\bgroup
    \fboxsep=0pt
    \count1=0
    \count2=#1\relax
    \loop\ifnum\count1<4
    \advance\count1 by 1
    \ifnum\count1=\count2
    \fbox{\rule[-\dp\strutbox]{1em}{\baselineskip}}\quad
    \else
    \fbox{\makebox[1em]{\number\count1}\strut}\quad
    \fi
    \repeat
    \egroup}
\begin{document}
    
    \chapter{Analysis of Algorithms}
    
    \begin{question}
        Which of the following answers is correct for the given recurrence relation $T(n)=T(n-1)+n$?
        \begin{enumerate}
            \item $O(n^2)$
            \item $O(n^3)$
            \item $O(n^4)$
            \item $O(n\log n)$
        \end{enumerate}
\begin{correctchoice}
    \bubble{2}
\end{correctchoice}


    \end{question}
    

    \chapter{answer Sheet}

    \printmycorrectchoice
    
    
\end{document}

我有一个小问题,希望得到帮助来解决。当我在正确的选择环境中使用命令 \bubble 时,LaTeX 给出了以下错误:

在此处输入图片描述

我该如何解决这个问题?

答案1

如果\thehint定义了——也许应该这样\thecorrectchoice?——你可以轻松地做某事,比如

\NewEnviron{correctchoice}{%
  \begingroup
  \refstepcounter{correctchoice}%
  \edef\@currentlabel{%
    \thechapter\unexpanded{.}\thequestion\unexpanded{~}%
    \unexpanded\expandafter{\BODY}%
  }%
  \@onelevel@sanitize\@currentlabel
  \label{correctchoice:\thehint}%
  \endgroup
}

,但这很容易出错,因为correctchoice在这个环境中,用户可能会添加额外的周围空格或空行/\par标记,或者可能会以错误的方式发出命令\bubble,或者可能会向环境主体添加任何东西。

在一个对你的另一个问题的评论您提到您希望在每一章的末尾打印解决方案。

因此,让我们借助于 exsheets 的基础设施:

% The documentclass book does not offer an option 14pt !
% The documentclass book also does not offer an option cleardoublepage=... !
% (But scrbook does, afaik.)
% \documentclass[twoside,a4paper,cleardoublepage=empty,14pt]{book}
%
\documentclass[twoside,a4paper,12pt]{book}
\usepackage{exsheets}
\usepackage{enumerate,tikz}
\usepackage{enumitem}
%\usepackage{multicol} % This is not needed for the MRE/MCVE

\DeclareInstance{exsheets-heading}{namelessrunin}{default}{
  runin = true ,
  number-post-code = \space ,
  % attach = { main[l,vc]points[l,vc](\linewidth+\marginparsep,0pt) } ,
  join = {
           main[l,vc]number[l,vc](0pt,0pt)
        }
}
\SetupExSheets{counter-format=ch.qu[1], counter-within={chapter}}
\SetupExSheets[question]{type=exam, headings-format = {\large\bfseries}, name = Problem}
\SetupExSheets[solution]{headings=namelessrunin, headings-format={\normalsize\normalfont}}

\setlist[enumerate]{label=\protect\squared{\arabic*}}

\newcommand*\squared[1]{%
  \tikz[baseline=(char.base)]{%
    \node[shape=rectangle,draw,inner sep=3pt] (char) {#1};
  }%
}
\newcommand*\squaredblack[1]{%
  \tikz[baseline=(char.base)]{%
    \node[shape=rectangle,draw,inner sep=3pt, fill=black] (char) {\phantom{#1}};
  }%
}

\ExplSyntaxOn
\bool_new:N \correctchoice_bool
\cs_new_protected:Npn \bubble #1 { % #1 is the comma-list with correct answers.
  \group_begin:
  \fboxsep=0pt~
  \int_step_inline:nn {4} { % step from 1 to 4 a stepping variable which is denoted by ##1.
    \bool_set_false:N \correctchoice_bool
    \clist_map_inline:nn {#1} { % iterate the comma-list of numbers of correct answers;
                                % ####1 in each iteration denotes the number processed
                                % in the current iteration.
      % In the n-th step iteratively compare ##1 = n to all list-elements ####1
      % and in case n = ##1 is in the list of correct answers toggle the boolean
      \int_compare:nNnT {##1} = {####1}{ \bool_set_true:N \correctchoice_bool }
    } 
    % Depending on the boolean use \square or \squareblack:
    \use:c {squared \bool_if:NT \correctchoice_bool {black} }{\number##1}
    % If not dealing with number 4, the last number, append \quad:
    \int_compare:nNnT {##1} < {4} {\quad}
  }
  \group_end:
}
\ExplSyntaxOff


\begin{document}

\chapter{Analysis of Algorithms}

\begin{question}
Which of the following answers is correct for the given recurrence relation $T(n)=T(n-1)+n$?
\begin{enumerate}
\item $O(n^2)$
\item $O(n^3)$
\item $O(n^4)$
\item $O(n\log n)$
\end{enumerate}
\end{question}
\begin{solution}
\bubble{2}
\end{solution}

\begin{question}
Which of the following answers are incorrect for the given recurrence relation $T(n)=T(n-1)+n$?
\begin{enumerate}
\item $O(n^2)$
\item $O(n^3)$
\item $O(n^4)$
\item $O(n\log n)$
\end{enumerate}
\end{question}
\begin{solution}
\bubble{1, 3,4}
\end{solution}



\chapter*{answer Sheet}
\printsolutions[chapter]

\chapter{Basic Number Theory}

\begin{question}
Which are solutions to $(x_1)^2 + (x_2)^2 = (x_3)^2$?
\begin{enumerate}
\item $x_1=0$, $x_2=1$, $x_3=1$
\item $x_1=3$, $x_2=4$, $x_3=5$
\item $x_1=3$, $x_2=5$, $x_3=7$
\item $x_1=5$, $x_2=12$, $x_3=13$
\end{enumerate}
\end{question}
\begin{solution}
\bubble{1,2 ,4}
\end{solution}


\begin{question}
Which are not solutions to $(x_1)^2 + (x_2)^2 = (x_3)^2$?
\begin{enumerate}
\item $x_1=0$, $x_2=1$, $x_3=1$
\item $x_1=3$, $x_2=4$, $x_3=5$
\item $x_1=3$, $x_2=5$, $x_3=7$
\item $x_1=5$, $x_2=12$, $x_3=13$
\end{enumerate}
\end{question}
\begin{solution}
\bubble{3}
\end{solution}

\chapter*{answer Sheet}
\printsolutions[chapter]

\end{document}

在此处输入图片描述



如果您无法使用该环境,solution您可以定义自己的机制。

作为起点,我实现了一种机制,其中命令\correctchoices将发生里面(!)问题环境和命令\printpendingchoices可用于打印尚未打印的气泡。

该机制内部依赖于\label-\ref这意味着您需要多次编译代码直到所有内容匹配,并且您需要遵守控制台和 .log 文件中有关需要重新运行 LaTeX 的警告 - 这些警告具有以下模式:
LaTeX Warning: Label(s) may have changed. Rerun to get cross-references right.

的每个实例\correctchoices都会创建一个交叉引用标签,通过该标签\ref— —\ref*如果加载了包 hyperref — — 您就可以获得. 为您执行/ 。
\correctchoicesline{⟨number of the question⟩}{⟨comma-list of numbers of correct answers⟩}\printpendingchoices\ref\ref*

因此,修改气泡外观的起点是宏\correctchoicesline,它依次打印⟨问题编号⟩ 的问题和呼吁\bubble,特此提供 ⟨正确答案数量的逗号列表⟩ 作为\bubble的论点。

% The documentclass book does not offer an option 14pt !
% The documentclass book also does not offer an option cleardoublepage=... !
% (But scrbook does, afaik.)
% \documentclass[twoside,a4paper,cleardoublepage=empty,14pt]{book}
%
\documentclass[twoside,a4paper,12pt]{book}
\usepackage{exsheets}
\usepackage{enumerate,tikz}
\usepackage{enumitem}
%\usepackage{multicol} % This is not needed for the MRE/MCVE

\SetupExSheets[question]{type=exam}
\SetupExSheets{counter-format=ch.qu[1], counter-within={chapter}}
\RenewQuSolPair{question}[headings-format=\large\bfseries,name=Problem]{solution}
\setlist[enumerate]{label=\protect\squared{\arabic*}}

\newcommand*\squared[1]{\tikz[baseline=(char.base)]{
        \node[shape=rectangle,draw,inner sep=3pt] (char) {#1};}}
\newcommand*\squaredblack[1]{\tikz[baseline=(char.base)]{
        \node[shape=rectangle,draw,inner sep=3pt, fill=black] (char) {\phantom{#1}};}}


\ExplSyntaxOn
\int_new:N \g_correctchoices_int
\int_new:N \g_printpendingchoices_intervallstart_int
\int_zero:N \g_correctchoices_int
\int_gset:Nn \g_printpendingchoices_intervallstart_int {1 + \g_correctchoices_int}
\cs_new_protected:Npn \correctchoices #1 {
  \group_begin:
  \cs_set:cpn {\tl_to_str:n{@currentlabel}} {
    \token_to_str:N \correctchoicesline{\exp_args:No\QuestionNumber{\CurrentQuestionID}}{#1}
  }
  \int_gincr:N \g_correctchoices_int
  \label{\tl_to_str:n{@choice@:}\int_to_arabic:n{ \g_correctchoices_int }}
  \group_end:
}
\cs_new_protected:Npn \correctchoicesline #1#2 {%
  % #1 number of question, #2 comma list with numbers of correct answers
  \par
  \addvspace{4pt}
  \noindent#1\nobreakspace\bubble{#2}
}%
\bool_new:N \correctchoice_bool
\cs_new_protected:Npn \bubble #1 { % #1 is the comma-list with correct answers.
  \group_begin:
  \fboxsep=0pt~
  \int_step_inline:nn {4} { % step from 1 to 4 a stepping variable which is denoted by ##1.
    \bool_set_false:N \correctchoice_bool
    \clist_map_inline:nn {#1} { % iterate the comma-list of numbers of correct answers;
                                % ####1 in each iteration denotes the number processed
                                % in the current iteration.
      % In the n-th step iteratively compare ##1 = n to all list-elements ####1
      % and in case n = ##1 is in the list of correct answers toggle the boolean
      \int_compare:nNnT {##1} = {####1}{ \bool_set_true:N \correctchoice_bool }
    } 
    % Depending on the boolean use \square or \squareblack:
    \use:c {squared \bool_if:NT \correctchoice_bool {black} }{\number##1}
    % If not dealing with number 4, the last number, append \quad:
    \int_compare:nNnT {##1} < {4} {\quad}
  }
  \group_end:
}
\cs_new:Npn \printpendingchoices{
  \int_step_inline:nnn {\g_printpendingchoices_intervallstart_int}{\g_correctchoices_int} {
     \cs_if_exist:NTF \hyperref {\ref*} {\ref}  {\tl_to_str:n{@choice@:}\int_to_arabic:n{##1}}
  }
  \int_gset:Nn \g_printpendingchoices_intervallstart_int {1 + \g_correctchoices_int}
}
\ExplSyntaxOff


\begin{document}

\chapter{Analysis of Algorithms}

\begin{question}
Which of the following answers is correct for the given recurrence relation $T(n)=T(n-1)+n$?
\begin{enumerate}
\item $O(n^2)$
\item $O(n^3)$
\item $O(n^4)$
\item $O(n\log n)$
\end{enumerate}
\correctchoices{2}
\end{question}

\begin{question}
Which of the following answers are incorrect for the given recurrence relation $T(n)=T(n-1)+n$?
\begin{enumerate}
\item $O(n^2)$
\item $O(n^3)$
\item $O(n^4)$
\item $O(n\log n)$
\end{enumerate}
\correctchoices{1, 3,4}
\end{question}

\chapter*{answer Sheet}
\printpendingchoices



\chapter{Basic Number Theory}

\begin{question}
Which are solutions to $(x_1)^2 + (x_2)^2 = (x_3)^2$?
\begin{enumerate}
\item $x_1=0$, $x_2=1$, $x_3=1$
\item $x_1=3$, $x_2=4$, $x_3=5$
\item $x_1=3$, $x_2=5$, $x_3=7$
\item $x_1=5$, $x_2=12$, $x_3=13$
\end{enumerate}
\correctchoices{1,2 ,4}
\end{question}

\begin{question}
Which are not solutions to $(x_1)^2 + (x_2)^2 = (x_3)^2$?
\begin{enumerate}
\item $x_1=0$, $x_2=1$, $x_3=1$
\item $x_1=3$, $x_2=4$, $x_3=5$
\item $x_1=3$, $x_2=5$, $x_3=7$
\item $x_1=5$, $x_2=12$, $x_3=13$
\end{enumerate}
\correctchoices{3}
\end{question}

\chapter*{answer Sheet}
\printpendingchoices


\end{document}

相关内容