使用 xsim 收集练习提示

使用 xsim 收集练习提示

我的书有几个章节。每个章节里都有几个练习,每个练习都有答案,有些还附有提示。

本书的最后一章将收集提示和解决方案,按章节进行收集。

命令\printhint建议的这里一遍又一遍地打印所有提示。

所以我正在寻找一个命令或方法,可以只打印第 1 章的提示和第 2 章的提示等等。

一位 MWE 表示:

\documentclass[a4paper,openany]{book}
\usepackage{xsim}

\xsimsetup{
  exercise/within = chapter,
}

% Add hints for the exercises
\DeclareExerciseProperty{hint}
\newcommand\hint[1]{\SetExerciseProperty{hint}{#1}}
\newcommand\printhints{%
  \begin{description}
    \ForEachUsedExerciseByType{%
      \GetExercisePropertyT{hint}
        {\item[Hint to~##3]####1}%
    }%
  \end{description}
}

\begin{document}
\chapter{Algebra}
\begin{exercise}[subtitle={Real numbers}]
Explain why the real numbers form a field.
\end{exercise}
\begin{solution}
Since addition and multiplication are defined and have the usual properties.
\end{solution}

\begin{exercise}
Explain what is a prime number.
\hint{a natural number greater than 1}
\end{exercise}
\begin{solution}
It is not a product of two smaller natural numbers.
\end{solution}

\chapter{Geometry}

\begin{exercise}[subtitle={Pythagoras' theorem}]
  Prove that the area of the square whose side is the hypotenuse is equal to the sum of the areas of the squares on the other two sides.
  \hint{Draw the altitude from point C, and call H its intersection with the side AB.}
\end{exercise}
\begin{solution}
  The proof is easy.
\end{solution}

\begin{exercise}[subtitle={Thales's theorem}]
If A, B, and C are distinct points on a circle where the line AC is a diameter, the angle ABC is a right angle.
\end{exercise}
\begin{solution}
Since the sum of the angles in a triangle is equal to $180$\ldots
\end{solution}

\chapter*{Hints and solutions}
\section*{Hints to exercises from chapter 1}
\printhints %All of them are printed <<<
\section*{Solutions to exercises from chapter 1}
\printsolutions[headings=false,chapter=1]

\section*{Hints to exercises from chapter 2}
\printhints %They are all printed again, not good <<<

\section*{Solutions to exercises from chapter 2}
\printsolutions[headings=false,chapter=2]
\end{document}

答案1

请注意,##3参数的形式为\thechapter.\theexercise,因此我们可以提取章节编号并检查它是否等于“\printhints”的参数。

编辑

似乎你可以得到练习所在章节的编号值\ExercisePropertyGet,所以一个更简单的解决方案是

\documentclass[a4paper,openany]{book}
\usepackage{xsim}

\xsimsetup{
    exercise/within = chapter,
}

% Add hints for the exercises
\DeclareExerciseProperty{hint}
\newcommand\hint[1]{\SetExerciseProperty{hint}{#1}}
\newcommand\printhints[1]{%
    \begin{description}
        \ForEachUsedExerciseByType{%
            \GetExercisePropertyT{hint}
            {%
            \ifnum \ExercisePropertyGet{##1}{##2}{chapter-value}=#1
            \item[Hint to~##3]####1
            \fi
            }%
        }%
    \end{description}
}

\begin{document}
    \chapter{Algebra}
    \begin{exercise}[subtitle={Real numbers}]
        Explain why the real numbers form a field.
    \end{exercise}
    \begin{solution}
        Since addition and multiplication are defined and have the usual properties.
    \end{solution}
    
    \begin{exercise}
        Explain what is a prime number.
        \hint{a natural number greater than 1}
    \end{exercise}
    \begin{solution}
        It is not a product of two smaller natural numbers.
    \end{solution}
    
    \chapter{Geometry}
    
    \begin{exercise}[subtitle={Pythagoras' theorem}]
        Prove that the area of the square whose side is the hypotenuse is equal to the sum of the areas of the squares on the other two sides.
        \hint{Draw the altitude from point C, and call H its intersection with the side AB.}
    \end{exercise}
    \begin{solution}
        The proof is easy.
    \end{solution}
    
    \begin{exercise}[subtitle={Thales's theorem}]
        If A, B, and C are distinct points on a circle where the line AC is a diameter, the angle ABC is a right angle.
    \end{exercise}
    \begin{solution}
        Since the sum of the angles in a triangle is equal to $180$\ldots
    \end{solution}
    
    \chapter*{Hints and solutions}
    \section*{Hints to exercises from chapter 1}
    \printhints{1} 
    \section*{Solutions to exercises from chapter 1}
    \printsolutions[headings=false,chapter=1]
    
    \section*{Hints to exercises from chapter 2}
    \printhints{2} 
    
    \section*{Solutions to exercises from chapter 2}
    \printsolutions[headings=false,chapter=2]
\end{document}

在 expl3 的帮助下,您可以轻松地概括\printhints出以逗号分隔的章节编号列表,而不是一个数字。

\documentclass[a4paper,openany]{book}
\usepackage{xsim}

\xsimsetup{
    exercise/within = chapter,
}

% Add hints for the exercises
\DeclareExerciseProperty{hint}
\newcommand\hint[1]{\SetExerciseProperty{hint}{#1}}

\ExplSyntaxOn

\NewDocumentCommand \printhints { m } {
    \seq_set_split:Nnn \l_a_seq { , } { #1 }
    \begin{description}
        \ForEachUsedExerciseByType{
            \GetExercisePropertyT{hint}
            {
            \seq_set_split:Nnn \l_b_seq { . } { ##3 }
            \seq_get_left:NN \l_b_seq \l_a_tl
            \seq_if_in:NVT \l_a_seq { \l_a_tl }
            {
             \item[Hint to~##3]####1
            }
        }
    }
    \end{description}
}

\ExplSyntaxOff

\begin{document}
    \chapter{Algebra}
    \begin{exercise}[subtitle={Real numbers}]
        Explain why the real numbers form a field.
    \end{exercise}
    \begin{solution}
        Since addition and multiplication are defined and have the usual properties.
    \end{solution}
    
    \begin{exercise}
        Explain what is a prime number.
        \hint{a natural number greater than 1}
    \end{exercise}
    \begin{solution}
        It is not a product of two smaller natural numbers.
    \end{solution}
    
    \chapter{Geometry}
    
    \begin{exercise}[subtitle={Pythagoras' theorem}]
        Prove that the area of the square whose side is the hypotenuse is equal to the sum of the areas of the squares on the other two sides.
        \hint{Draw the altitude from point C, and call H its intersection with the side AB.}
    \end{exercise}
    \begin{solution}
        The proof is easy.
    \end{solution}
    
    \begin{exercise}[subtitle={Thales's theorem}]
        If A, B, and C are distinct points on a circle where the line AC is a diameter, the angle ABC is a right angle.
    \end{exercise}
    \begin{solution}
        Since the sum of the angles in a triangle is equal to $180$\ldots
    \end{solution}
    
    \chapter*{Hints and solutions}
    \section*{Hints to exercises from chapter 1}
    \printhints{1} 
    \section*{Solutions to exercises from chapter 1}
    \printsolutions[headings=false,chapter=1]
    
    \section*{Hints to exercises from chapter 2}
    \printhints{2} 
    
    \section*{Solutions to exercises from chapter 2}
    \printsolutions[headings=false,chapter=2]
    
    \section*{Hints to exercises from chapters 1 and 2}
    \printhints{1,2}
\end{document}

另一个选择是,第 n 次调用\printhints将打印第 n 章的提示

\documentclass[a4paper,openany]{book}
\usepackage{xsim}

\xsimsetup{
    exercise/within = chapter,
}

% Add hints for the exercises
\DeclareExerciseProperty{hint}
\newcommand\hint[1]{\SetExerciseProperty{hint}{#1}}
\newcounter{hintchapcount}
\newcommand\printhints{%
    \stepcounter{hintchapcount}
    \begin{description}
        \ForEachUsedExerciseByType{%
            \GetExercisePropertyT{hint}
            {%
                 \ifnum \ExercisePropertyGet{##1}{##2}{chapter-value}=\value{hintchapcount}
                \item[Hint to~##3]####1
                \fi
            }%
        }%
    \end{description}
}

\begin{document}
    \chapter{Algebra}
    \begin{exercise}[subtitle={Real numbers}]
        Explain why the real numbers form a field.
    \end{exercise}
    \begin{solution}
        Since addition and multiplication are defined and have the usual properties.
    \end{solution}
    
    \begin{exercise}
        Explain what is a prime number.
        \hint{a natural number greater than 1}
    \end{exercise}
    \begin{solution}
        It is not a product of two smaller natural numbers.
    \end{solution}
    
    \chapter{Geometry}
    
    \begin{exercise}[subtitle={Pythagoras' theorem}]
        Prove that the area of the square whose side is the hypotenuse is equal to the sum of the areas of the squares on the other two sides.
        \hint{Draw the altitude from point C, and call H its intersection with the side AB.}
    \end{exercise}
    \begin{solution}
        The proof is easy.
    \end{solution}
    
    \begin{exercise}[subtitle={Thales's theorem}]
        If A, B, and C are distinct points on a circle where the line AC is a diameter, the angle ABC is a right angle.
    \end{exercise}
    \begin{solution}
        Since the sum of the angles in a triangle is equal to $180$\ldots
    \end{solution}
    
    \chapter*{Hints and solutions}
    \section*{Hints to exercises from chapter 1}
    \printhints 
    \section*{Solutions to exercises from chapter 1}
    \printsolutions[headings=false,chapter=1]
    
    \section*{Hints to exercises from chapter 2}
    \printhints 
    
    \section*{Solutions to exercises from chapter 2}
    \printsolutions[headings=false,chapter=2]
\end{document}

相关内容