我正在用来xsim
打印练习的提示和解决方案。
所有的练习都有对应的答案,但是并不是所有的练习都有提示,如下所示。
当练习没有提示时,LaTeX 仍然会打印提示参考,但参考未知(如下图红色所示)。
我该如何修复这个问题,以便在没有提示的情况下,不会打印边距段落上的参考资料?
\documentclass[]{book}
\setlength{\parskip}{10pt}
\usepackage{xsim}
\xsimsetup{
exercise/name={Question},
exercises/name={Questions},
solution/name={Solution},
solutions/name={Solutions},
exercise/within = chapter,
exercise/template=myTemplate,
solution/template =myTemplate ,
print-solutions/headings-template = myTemplate
}
\DeclareExerciseEnvironmentTemplate{myTemplate}
{%
\IfInsideSolutionTF
{\label{sol:\ExerciseID}}
{\label{ex:\ExerciseID}}
{\par\normalfont\bfseries\GetExerciseName~\GetExerciseProperty{counter}~\GetExercisePropertyT{subtitle}{{(\PropertyValue)}}\newline}
\IfInsideSolutionTF
{\marginpar{Question in page \pageref{ex:\ExerciseID}}}%
{%
\marginpar{Hint in page \pageref{hint:\ExerciseID}}
\marginpar{Solution in page \pageref{sol:\ExerciseID}}}%
}
{\par
}
% Add hints for the exercises
\DeclareExerciseProperty{hint}
\newcommand\hint[1]{\SetExerciseProperty{hint}{#1}}
\newcommand\printhints{%
\begin{description}
\ForEachUsedExerciseByType{%
\GetExercisePropertyT{hint}
{\item[Hint to question\label{hint:##2}~##3]~\marginpar{Question in page \pageref{ex:\ExerciseID}}\marginpar{Solution in page \pageref{sol:\ExerciseID}}####1}%
}%
\end{description}
}
\begin{document}
\chapter{Geometry}
\section{Introduction}
In mathematics, the Euclidean plane is a Euclidean space of dimension two.
\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}
\newpage
\section*{Hints}
\printhints
\printsolutions[headings=true]
\end{document}
答案1
代替
\marginpar{Hint in page \pageref{hint:\ExerciseID}}
经过
\expandafter\ifx \csname r@hint:\ExerciseID \endcsname \relax \else
\marginpar{Hint in page \pageref{hint:\ExerciseID}}\fi
此代码检查 是否已\r@hint:number
定义。如果未定义,则不执行任何其他操作,只打印\marginpar
。\r@hint:number
当且仅当知道相关页码时, 才定义。
答案2
\ifhint
为了解决你的问题,我修改了练习环境,以便只有条件为真时它才会引用提示,然后我定义了一个新环境myex
,它检查标记是否\hint
出现在环境主体中,如果是,则将条件\ifhint
设置为真,否则什么也不会发生,然后它将主体传递给环境exercise
。
\documentclass[]{book}
\setlength{\parskip}{10pt}
\usepackage{xsim}
\xsimsetup{
exercise/name={Question},
exercises/name={Questions},
solution/name={Solution},
solutions/name={Solutions},
exercise/within = chapter,
exercise/template=myTemplate,
solution/template =myTemplate ,
print-solutions/headings-template = myTemplate
}
\DeclareExerciseEnvironmentTemplate{myTemplate}
{%
\IfInsideSolutionTF
{\label{sol:\ExerciseID}}
{\label{ex:\ExerciseID}}
{\par\normalfont\bfseries\GetExerciseName~\GetExerciseProperty{counter}~\GetExercisePropertyT{subtitle}{{(\PropertyValue)}}\newline}
\IfInsideSolutionTF
{\marginpar{Question in page \pageref{ex:\ExerciseID}}}%
{%
\ifhint\marginpar{Hint in page \pageref{hint:\ExerciseID}}\fi
\marginpar{Solution in page \pageref{sol:\ExerciseID}}}%
}
{\par
}
% Add hints for the exercises
\DeclareExerciseProperty{hint}
\newcommand\hint[1]{\SetExerciseProperty{hint}{#1}}
\newcommand\printhints{%
\begin{description}
\ForEachUsedExerciseByType{%
\GetExercisePropertyT{hint}
{\item[Hint to question\label{hint:##2}~##3]~\marginpar{Question in page \pageref{ex:\ExerciseID}}\marginpar{Solution in page \pageref{sol:\ExerciseID}}####1}%
}%
\end{description}
}
\newif\ifhint
\ExplSyntaxOn
\NewDocumentEnvironment {myex} { O{} +b }
{
\tl_if_in:nnTF {\hint} {#2} {\hinttrue} {}
\begin{exercise}[#1]
#2
\end{exercise}
\hintfalse
}
{}
\ExplSyntaxOff
\begin{document}
\chapter{Geometry}
\section{Introduction}
In mathematics, the Euclidean plane is a Euclidean space of dimension two.
\begin{myex}[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{myex}
\begin{solution}
The proof is easy.
\end{solution}
\begin{myex}[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{myex}
\begin{solution}
Since the sum of the angles in a triangle is equal to $180°$\ldots
\end{solution}
\newpage
\section*{Hints}
\printhints
\printsolutions[headings=true]
\end{document}