书籍类文件中子问题的正确答案编号

书籍类文件中子问题的正确答案编号

我正在尝试创建一个 latex 文件,在文档末尾展示问题及其解决方案。除了一件事之外,一切都很好,即答案编号对于问题中提到的多个问题。有人知道如何修复编号吗?请检查,MWE 如下所示。

\documentclass[12pt]{book}
%
\usepackage{enumitem}
\usepackage{ifthen}
\usepackage{tocloft}
\usepackage{exercise}
\usepackage{tasks}
%
% Set the Show Answers Boolean
\newboolean{showAns}
\setboolean{showAns}{false}
\newcommand{\showAns}{\setboolean{showAns}{true}}
%
% The length of the Answer line
\newlength{\answerlength}
\newcommand{\anslen}[1]{\settowidth{\answerlength}{#1}}
%
% ans command that indicates space for an answer or shows the answer in red
\newcommand{\ans}[1]{\settowidth{\answerlength}{\hspace{2ex}#1\hspace{2ex}}%
    \ifthenelse{\boolean{showAns}}%
    {\textcolor{red}{\underline{\hspace{2ex}#1\hspace{2ex}}}}%
    {\underline{\hspace{\answerlength}}}}%

% Formatting how multiple choices Questions are formated.
\settasks{counter-format={tsk[A])}}
%
% Some commands for the Exercise Question package
\renewcommand{\QuestionNB}{Q~\arabic{Question}.\ }
\renewcommand{\ExerciseHeader}{} %no header
\renewcommand{\QuestionBefore}{2em} %Space above each Q
\setlength{\QuestionIndent}{1cm} % Indent after Q number
% To create the list of answers with tocloft... 
\newcommand{\listanswername}{Answers}
\newlistof[Question]{answer}{Answers}{\listanswername}
% Creates a TOC for Answers
\newcounter{prevQ}
\newcommand{\answer}[1]{\refstepcounter{answer}%
    \ans{#1}%
    \ifnum\theQuestion=\theprevQ%
    \addcontentsline{Answers}{answer}{\protect\numberline{}#1}% don't include the Q number
    \else%
    \addcontentsline{Answers}{answer}{\protect\numberline{\theQuestion}#1}%
    \setcounter{prevQ}{\value{Question}}%
    \fi%
}%
%tocloft formatting listofanswers
\renewcommand{\cftAnswerstitlefont}{\bfseries\large}
\renewcommand{\cftanswerdotsep}{\cftnodots}
\cftpagenumbersoff{answer} 
\addtolength{\cftanswernumwidth}{10pt}

%%%% DOCUMENT START HERE %%%%%
\begin{document}
\showAns  %%% uncomment it to see the answers with the questions 
\begin{Exercise}    
\Question Fill in the blanks:
\begin{tasks}(2)
    \task $1 + \answer{7} = 8$ 
    \task $4  - \answer{0} = 4$ 
    \task $3 + \answer{9} = 12$ 
    \task $13 - \answer{6} = 7$     
\end{tasks}
\Question Solve $2+3=$~\answer{$5$}
\Question 9 + 6 = 15. Is it True?~\answer{False}    
\end{Exercise}
\listofanswer  %%% uncomment to see the answer at the end 
\clearpage
\end{document}

答案1

编辑:请参阅task下面的解决方案。


这是一个用subQuestion而不是 的解决方案task,其缺点是子问题没有设置在两列中。

其思路是检查\answer命令中的三种情况:没有子问题的新问题、有子问题的新问题或当前问题中的子问题。在第一种情况下,打印问题编号;在第二种情况下,打印问题编号和子问题编号;在第三种情况下,只打印子问题编号。

为了对齐数字,需要添加当前问题数字的宽度。这通常可以使用 来完成,\phantom但在 中效果不佳\protect\numberline,因此\settowidth改用 。

梅威瑟:

\documentclass[12pt]{book}
%
\usepackage{enumitem}
\usepackage{ifthen}
\usepackage{tocloft}
\usepackage{exercise}
%\usepackage{tasks}
%
% Set the Show Answers Boolean
\newboolean{showAns}
\setboolean{showAns}{false}
\newcommand{\showAns}{\setboolean{showAns}{true}}
%
% The length of the Answer line
\newlength{\answerlength}
\newcommand{\anslen}[1]{\settowidth{\answerlength}{#1}}
%
% ans command that indicates space for an answer or shows the answer in red
\newcommand{\ans}[1]{\settowidth{\answerlength}{\hspace{2ex}#1\hspace{2ex}}%
    \ifthenelse{\boolean{showAns}}%
    {\textcolor{red}{\underline{\hspace{2ex}#1\hspace{2ex}}}}%
    {\underline{\hspace{\answerlength}}}}%

% Formatting how multiple choices Questions are formated.
%\settasks{counter-format={tsk[A])}}
%
% Some commands for the Exercise Question package
\renewcommand{\QuestionNB}{Q~\arabic{Question}.\ }
\renewcommand{\ExerciseHeader}{} %no header
\renewcommand{\QuestionBefore}{2em} %Space above each Q
\setlength{\QuestionIndent}{1cm} % Indent after Q number
% To create the list of answers with tocloft... 
\newcommand{\listanswername}{Answers}
\newlistof[Question]{answer}{Answers}{\listanswername}
% Creates a TOC for Answers
\newcounter{prevQ}
\newlength{\myquestionwd}% length to store question number width
\newcommand{\answer}[1]{\refstepcounter{answer}%
    \ans{#1}%
    \ifnum\theQuestion=\theprevQ% subquestion within current question
    \addcontentsline{Answers}{answer}{\protect\numberline{\settowidth{\myquestionwd}{\theQuestion.}\hspace{\myquestionwd}\thesubQuestion}#1}% don't include the Q number
    \else%
    \ifnum\value{subQuestion}>0% new question with subquestions
    \addcontentsline{Answers}{answer}{\protect\numberline{\theQuestion.\thesubQuestion}#1}%
    \setcounter{prevQ}{\value{Question}}%
    \else% new question without subquestions
    \addcontentsline{Answers}{answer}{\protect\numberline{\theQuestion}#1}%
    \setcounter{prevQ}{\value{Question}}%
    \fi%
    \fi%
}%
%tocloft formatting listofanswers
\renewcommand{\cftAnswerstitlefont}{\bfseries\large}
\renewcommand{\cftanswerdotsep}{\cftnodots}
\cftpagenumbersoff{answer} 
\addtolength{\cftanswernumwidth}{10pt}

%%%% DOCUMENT START HERE %%%%%
\begin{document}
\showAns  %%% uncomment it to see the answers with the questions 
\begin{Exercise}    
\Question Fill in the blanks:
%\begin{tasks}(2)
    \subQuestion $1 + \answer{7} = 8$ 
    \subQuestion $4  - \answer{0} = 4$ 
    \subQuestion $3 + \answer{9} = 12$ 
    \subQuestion $13 - \answer{6} = 7$     
%    \task $1 + \answer{7} = 8$ 
%    \task $4  - \answer{0} = 4$ 
%    \task $3 + \answer{9} = 12$ 
%    \task $13 - \answer{6} = 7$
%\end{tasks}
\Question Solve $2+3=$~\answer{$5$}
\Question 9 + 6 = 15. Is it True?~\answer{False}    
\end{Exercise}
\listofanswer  %%% uncomment to see the answer at the end 
\clearpage
\end{document}

结果:

在此处输入图片描述


解决方案task:基本相同,但现在使用 expl3 语法\int_to_Alph:n \g__tasks_int而不是\thesubQuestion来访问内部tasks计数器\g__tasks_int并将其转换为大写字母。 测试此计数器是使用 完成的\int_compare:nNnTF。 此外,此计数器不会在环境结束时自动重置,因此从包中tasks添加了。\xapptocmdxpatch

梅威瑟:

\documentclass[12pt]{book}
%
\usepackage{enumitem}
\usepackage{ifthen}
\usepackage{tocloft}
\usepackage{exercise}
\usepackage{tasks}
\usepackage{xpatch}

% reset counter after tasks environment
\ExplSyntaxOn
\xapptocmd{\endtasks}{\int_gzero:N \g__tasks_int}{}{}
\ExplSyntaxOff
%
% Set the Show Answers Boolean
\newboolean{showAns}
\setboolean{showAns}{false}
\newcommand{\showAns}{\setboolean{showAns}{true}}
%
% The length of the Answer line
\newlength{\answerlength}
\newcommand{\anslen}[1]{\settowidth{\answerlength}{#1}}
%
% ans command that indicates space for an answer or shows the answer in red
\newcommand{\ans}[1]{\settowidth{\answerlength}{\hspace{2ex}#1\hspace{2ex}}%
    \ifthenelse{\boolean{showAns}}%
    {\textcolor{red}{\underline{\hspace{2ex}#1\hspace{2ex}}}}%
    {\underline{\hspace{\answerlength}}}}%

% Formatting how multiple choices Questions are formated.
\settasks{counter-format={tsk[A])}}
%
% Some commands for the Exercise Question package
\renewcommand{\QuestionNB}{Q~\arabic{Question}.\ }
\renewcommand{\ExerciseHeader}{} %no header
\renewcommand{\QuestionBefore}{2em} %Space above each Q
\setlength{\QuestionIndent}{1cm} % Indent after Q number
% To create the list of answers with tocloft... 
\newcommand{\listanswername}{Answers}
\newlistof[Question]{answer}{Answers}{\listanswername}
% Creates a TOC for Answers
\newcounter{prevQ}
\newlength{\myquestionwd}
\ExplSyntaxOn%
\newcommand{\answer}[1]{\refstepcounter{answer}%
    \ans{#1}%
    \ifnum\theQuestion=\theprevQ% subquestion within current question
     \addcontentsline{Answers}{answer}{\protect\numberline{\settowidth{\myquestionwd}{\theQuestion.}\hspace{\myquestionwd}\int_to_Alph:n \g__tasks_int} #1}% don't include the Q number
    \else%
    \int_compare:nNnTF{\g__tasks_int}>{0}{% new question with subquestions
     \addcontentsline{Answers}{answer}{\protect\numberline{\theQuestion.\int_to_Alph:n \g__tasks_int}#1}%
     \setcounter{prevQ}{\value{Question}}%
     }{% new question without subquestions
    \addcontentsline{Answers}{answer}{\protect\numberline{\theQuestion.}#1}%
    \setcounter{prevQ}{\value{Question}}%
    }%
    \fi%
}%
\ExplSyntaxOff%
%tocloft formatting listofanswers
\renewcommand{\cftAnswerstitlefont}{\bfseries\large}
\renewcommand{\cftanswerdotsep}{\cftnodots}
\cftpagenumbersoff{answer} 
\addtolength{\cftanswernumwidth}{10pt}

%%%% DOCUMENT START HERE %%%%%
\begin{document}
\showAns  %%% uncomment it to see the answers with the questions 
\begin{Exercise}    
\Question Fill in the blanks:
\begin{tasks}(2)
    \task $1 + \answer{7} = 8 $
    \task $4  - \answer{0} = 4$
    \task $3 + \answer{9} = 12$
    \task $13 - \answer{6} = 7$
\end{tasks}
\Question Solve $2+3=$~\answer{$5$}
\Question 9 + 6 = 15. Is it True?~\answer{False}    
\end{Exercise}
\listofanswer  %%% uncomment to see the answer at the end 
\clearpage
\end{document}

结果:

在此处输入图片描述

相关内容