使用 xsim 在边距段落中键入提示和解决方案页面引用练习

使用 xsim 在边距段落中键入提示和解决方案页面引用练习

我正在按照我的书写练习,并以此xsim为目的。

对于某些练习,将会在章节末尾出现提示(在“练习提示”部分下)。

同样,对于某些练习,也会有相应的解决方案。同样 - 我希望它们出现在章节末尾的“练习解决方案”部分下。

我如何做到?

我尝试效仿提示存储库以及交叉引用,但我不知道如何完整地实现它。

  1. 首先要声明一个名为的新模板myTemplate,首先要为练习和解决方案创建一个标签,但我如何为提示创建一个标签呢?
\DeclareExerciseEnvironmentTemplate{myTemplate}
    {%
    \IfInsideSolutionTF
      {\label{sol:\ExerciseID}}
      {\label{ex:\ExerciseID}}
      % how to create a label for the hints??
  1. 我还需要帮助创建一个命令来打印章节末尾的提示。我希望提示显示为

练习 3 提示

利用事实$x=1$。

练习 5 提示

回想一下$x=y$。

ETC。

我现在拥有的该文档的 MWE 如下:

%!TEX TS-program = xelatex
\documentclass[openany,oneside]{book}
\usepackage{xsim,xcolor}

\newcommand{\marginRef}[1]{%
  \marginpar{\colorbox{lightgray}{\parbox{\dimexpr\marginparwidth-2\fboxsep}{#1}}}
}

\xsimsetup{solution/print = false,}
\xsimsetup{
  exercise/name={Problem},
  exercises/name={Problems},
  solution/name={Solution},
  solutions/name={Solutions},
  exercise/within = chapter,
  exercise/template=myTemplate,
  solution/template =myTemplate ,
  print-solutions/headings-template = myTemplate
}

\DeclareExerciseProperty{hint}
\newcommand\hint[1]{\SetExerciseProperty{hint}{#1}}

\DeclareExerciseHeadingTemplate{myTemplate}{%
    \chapter{Solutions to Problems in chapter \thechapter}%
  }
\DeclareExerciseEnvironmentTemplate{myTemplate}
    {%
    \IfInsideSolutionTF
      {\label{sol:\ExerciseID}}
      {\label{ex:\ExerciseID}}
     % how to create a label for the hints??
  \GetExerciseHeadingF{\subsection*}%
    {%
    \XSIMmixedcase{\arial% <- font changed
        \GetExerciseName}\nobreakspace
    \GetExerciseProperty{counter}%
    \IfInsideSolutionF
    {%
    \GetExercisePropertyT{subtitle}
    { {\large\itshape\PropertyValue}}%
    }%
    }\marginpar{% how to print the hint page ref here?
                         }%
    \marginRef{solution in page \pageref{ex:\ExerciseID} }
%    \marginpar{\colorbox{lightgray}{\parbox{\dimexpr\marginparwidth-2\fboxsep}{solution in page \pageref{ex:\ExerciseID}}}}
    }
{\par}

% lang package
\usepackage{polyglossia}
\setdefaultlanguage{english}
\newfontfamily\arial{Arial}[Script=Latin,Language=English]

\begin{document}
\chapter{Geometry}
\section{Introduction}
\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}

\printsolutions[headings=true,headings-template=myTemplate]
\end{document}

输出:

输出

答案1

xsim以下使用手册中的 MWE 和示例 11(“给出提示”) :

\documentclass[openany,oneside]{book}
\usepackage{xsim,xcolor}

\newcommand{\marginRef}[1]{%
  \marginpar{\colorbox{lightgray}{\parbox{\dimexpr\marginparwidth-2\fboxsep}{#1}}}
}

\xsimsetup{solution/print = false,}
\xsimsetup{
  exercise/name={Problem},
  exercises/name={Problems},
  solution/name={Solution},
  solutions/name={Solutions},
  exercise/within = chapter,
  exercise/template=myTemplate,
  solution/template =myTemplate ,
  print-solutions/headings-template = myTemplate
}

\DeclareExerciseProperty{hint}
\newcommand\hint[1]{\SetExerciseProperty{hint}{#1}}

\DeclareExerciseHeadingTemplate{myTemplate}{%
  \chapter{Solutions to Problems in chapter \thechapter}%
}
\DeclareExerciseEnvironmentTemplate{myTemplate}{%
  \IfInsideSolutionTF
    {\label{sol:\ExerciseID}}
    {\label{ex:\ExerciseID}}%
     % how to create a label for the hints??
  \GetExerciseHeadingF{\subsection*}{%
    \XSIMmixedcase{% \arial% <- font changed
        \GetExerciseName}\nobreakspace
    \GetExerciseProperty{counter}%
    \IfInsideSolutionF{%
      \GetExercisePropertyT{subtitle}{ {\large\itshape\PropertyValue}}%
      \marginRef{%
        solution in page \pageref{sol:\ExerciseID}%
        \IfExercisePropertySetT{hint}{, hint in page \pageref{hint:\ExerciseID}}%
      }%
    }%
  }%
}
{\par}

\newcommand\printhints{%
  \section{Hints}
  \begin{description}
    \ForEachUsedExerciseByType{%
      \GetExercisePropertyT{hint}
        {\item[Hint for \XSIMmixedcase{\GetExerciseName}\label{hint:##2}~##3]####1}%
    }%
  \end{description}
}

% lang package
% \usepackage{polyglossia}
% \setdefaultlanguage{english}
% \newfontfamily\arial{Arial}[Script=Latin,Language=English]

\usepackage{hyperref}

\begin{document}
\chapter{Geometry}
\section{Introduction}
\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}

\newpage
\printhints

\printsolutions[headings=true,headings-template=myTemplate]
\end{document}

在此处输入图片描述

在此处输入图片描述

您可以扩展上述功能,为每个提示添加边注,计算每个问题的提示,...

\documentclass[openany,oneside]{book}
\usepackage{xsim,xcolor}

\newcommand{\marginRef}[1]{%
  \marginpar{\colorbox{lightgray}{\parbox{\dimexpr\marginparwidth-2\fboxsep}{#1}}}
}

\xsimsetup{solution/print = false,}
\xsimsetup{
  exercise/name={Problem},
  exercises/name={Problems},
  solution/name={Solution},
  solutions/name={Solutions},
  exercise/within = chapter,
  exercise/template=myTemplate,
  solution/template =myTemplate ,
  print-solutions/headings-template = myTemplate
}

\DeclareExerciseProperty{hint}
\newcounter{hint}[exercise]
\newcommand\thishint{}
\newcommand\hint[1]{%
  \stepcounter{hint}%
  \eappto\thishint{Hint \thehint: \unexpanded{#1}\par}%
  \marginRef{hint \thehint\ in page \pageref{hint:\ExerciseID}}%
}

\DeclareExerciseHeadingTemplate{myTemplate}{%
  \chapter{Solutions to Problems in chapter \thechapter}%
}
\DeclareExerciseEnvironmentTemplate{myTemplate}{%
  \IfInsideSolutionTF
    {\label{sol:\ExerciseID}}
    {\label{ex:\ExerciseID}}%
  \GetExerciseHeadingF{\subsection*}{%
    \XSIMmixedcase{\GetExerciseName}\nobreakspace
    \GetExerciseProperty{counter}%
    \IfInsideSolutionF{%
      \GetExercisePropertyT{subtitle}{ {\large\itshape\PropertyValue}}%
      \marginRef{solution in page \pageref{sol:\ExerciseID}}%
    }%
  }%
}%
{%
  \par
  \IfInsideSolutionF{%
    \ifdefvoid\thishint{}
      {\SetExpandedExerciseProperty{hint}{\expandonce{\thishint}}}%
  }%
}

\newcommand\printhints{%
  \section{Hints}
  \begin{description}
    \ForEachUsedExerciseByType{%
      \GetExercisePropertyT{hint}
        {\item[Hints for \XSIMmixedcase{\GetExerciseName}\label{hint:##2}~##3]####1}%
    }%
  \end{description}
}

\usepackage{hyperref}

\begin{document}
\chapter{Geometry}
\section{Introduction}
\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.}
  \hint{Another hint.}
\end{exercise}
\begin{solution}
  The proof is easy.
\end{solution}

\newpage
\begin{exercise}
  No hints here.
\end{exercise}
\begin{solution}
  The solution.
\end{solution}
\begin{exercise}
  One hint here.\hint{see?}
\end{exercise}
\begin{solution}
  The solution.
\end{solution}

\newpage
\printhints

\printsolutions[headings=true,headings-template=myTemplate]
\end{document}

在此处输入图片描述

在此处输入图片描述

答案2

的设计xsim似乎只允许练习+解决方案。很难添加第三个类别。所以我用LaTeX3实现了你要求的功能。

  • 您可能需要编译两次才能获得正确的边注
  • 当边注很奇怪时,请尝试清除辅助文件
  • 你需要\PrintSolHint在每章结束时打电话
%!TEX TS-program = xelatex
\documentclass[openany,oneside]{book}
\usepackage{unicode-math}
\usepackage{xparse}
\usepackage{xcolor}
\usepackage{blindtext}
\usepackage{hyperref}


\makeatletter
\ExplSyntaxOn


\cs_new:Npn \myex_add_to_solutions:n #1 {
  \tl_if_exist:NTF \@@myex@solutions{
        \tl_gput_right:Nn \@@myex@solutions {,#1}
    } {
        \tl_gset:Nn \@@myex@solutions {#1}
    }
}

\cs_new:Npn \myex_add_to_hints:n #1 {
    \tl_if_exist:NTF \@@myex@hints{
        \tl_gput_right:Nn \@@myex@hints {,#1}
    } {
        \tl_gset:Nn \@@myex@hints {#1}
    }
}


% #1: type (solution/hint)
% #2: number
\cs_new:Npn \myex_if_problem_has:nnTF #1#2#3#4 {
  \cs_if_exist:cTF {@@myex@#1s@old} {
    \clist_if_in:cnTF {@@myex@#1s@old} {#2} {#3} {#4}
  } {#4}
}

\cs_generate_variant:Nn \myex_if_problem_has:nnTF {neTF}

\AtEndDocument{
    \tl_if_exist:NT \@@myex@solutions {
        \write\@auxout{\gdef\string\@@myex@solutions@old{\@@myex@solutions}}
    }
    \tl_if_exist:NT \@@myex@hints {
        \write\@auxout{\gdef\string\@@myex@hints@old{\@@myex@hints}}
    }
}

\seq_new:N \g_myex_solutions_seq
\seq_new:N \g_myex_hints_seq
\seq_new:N \l_myex_tmpa_seq
\tl_new:N \l_myex_tmpa_tl

% create counter
\newcounter{problem}
\numberwithin{problem}{chapter}

\newcommand{\marginRef}[1]{%
  \marginpar{\colorbox{lightgray}{\parbox{\dimexpr\marginparwidth-2\fboxsep}{\footnotesize #1}}}
}

% declare exercise environment
\DeclareDocumentEnvironment{exercise}{O{}}{
    \begin{trivlist}
    % increment the counter
    \refstepcounter{problem}
    % produce heading
    \item{\arial\bfseries\large Problem\ \theproblem\ {\itshape #1}}\label{prob:\theproblem}
    % cross referencing
    \seq_clear:N \l_myex_tmpa_seq
    \myex_if_problem_has:neTF {hint} {\theproblem} {
      \seq_put_right:Nx \l_myex_tmpa_seq {hint~on~page~\exp_not:N\pageref{hint:\theproblem}}
    } {}
    \myex_if_problem_has:neTF {solution} {\theproblem} {
      \seq_put_right:Nx \l_myex_tmpa_seq {solution~on~page~\exp_not:N\pageref{sol:\theproblem}}
    } {}
    \tl_set:Nx \l_myex_tmpa_tl {\seq_use:Nn \l_myex_tmpa_seq {;\ }}
    \tl_if_empty:NF \l_myex_tmpa_tl {
      \marginRef{\exp_args:NV \text_titlecase:n \l_myex_tmpa_tl}
    }
    % body text
    \item
}{\end{trivlist}}


% declare solution and hint environments
\DeclareDocumentEnvironment{solution}{+b}{
    \exp_args:Ne \myex_add_to_solutions:n {\theproblem}
}{\seq_gput_right:Nx \g_myex_solutions_seq {{\theproblem}{\exp_not:n {#1}}}}

\DeclareDocumentEnvironment{hint}{+b}{
    \exp_args:Ne \myex_add_to_hints:n {\theproblem}
}{\seq_gput_right:Nx \g_myex_hints_seq {{\theproblem}{\exp_not:n {#1}}}}

\newcommand{\PrintSolHint}{
  \seq_if_empty:NF \g_myex_hints_seq {
    \section{Hints~to~Problems~in~Chapter\ \thechapter}
    \begin{trivlist}
    \seq_map_inline:Nn \g_myex_hints_seq {
      \item {\arial\large\bfseries Hint~\tl_item:nn {##1} {1}} \label{hint:\tl_item:nn {##1} {1}}
      \item \tl_item:nn {##1} {2}
    }
    \end{trivlist}
  }
  \seq_if_empty:NF \g_myex_solutions_seq {
    \section{Solutions~to~Problems~in~Chapter\ \thechapter}
    \begin{trivlist}
    \seq_map_inline:Nn \g_myex_solutions_seq {
      \item {\arial\large\bfseries Solution~\tl_item:nn {##1} {1}} \label{sol:\tl_item:nn {##1} {1}}
      \item \tl_item:nn {##1} {2}
    }
    \end{trivlist}
  }
  % clear the solution/hint storage after each print
  \seq_gclear:N \g_myex_solutions_seq
  \seq_gclear:N \g_myex_hints_seq
}

\ExplSyntaxOff
\makeatother




% lang package
\usepackage{polyglossia}
\setdefaultlanguage{english}
\newfontfamily\arial{Arial}[Script=Latin,Language=English]

\begin{document}

\chapter{Geometry}
\section{Introduction}
\begin{exercise}[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{hint}
Draw the altitude from point $C$, and call $H$ its intersection with the side $AB$.
\end{hint}

\begin{exercise}[Some other theorem]
Please determine what gravity is.
\end{exercise}
\begin{solution}
The proof is easy.
\end{solution}
\begin{hint}
The solution to the equation is very easy:
\begin{align}
i\hbar\frac{\partial}{\partial t} \Psi(\mathbf{r},\,t) =
    -\frac{\hbar^2}{2m}\nabla^2 \Psi(\mathbf{r},\,t) + V(\mathbf{r})\Psi(\mathbf{r},\,t)
\end{align}
\end{hint}

\PrintSolHint

\chapter{Algebra}
\section{Introduction}
\begin{exercise}[Theorem 1]
\Blindtext[3]
\end{exercise}
\begin{solution}
\Blindtext[5]
\end{solution}
\begin{hint}
\Blindtext[5]
\end{hint}

\begin{exercise}[Theorem 2]
\Blindtext[3]
\end{exercise}
\begin{hint}
\Blindtext[5]
\end{hint}

\begin{exercise}[Theorem 3]
\Blindtext[3]
\end{exercise}
\begin{solution}
\Blindtext[5]
\end{solution}


\begin{exercise}[Theorem 4]
\Blindtext[8]
\end{exercise}



\PrintSolHint
\end{document}

相关内容