我正在为学生编写一本包含家庭作业练习及其解决方案的练习册。对于“家庭作业练习”,homework-exercise-solution
我使用 定义了自定义环境amsthm
。
最终,我正在寻找解决方法这是我的另一个问题我自己通过将“家庭作业练习”包装homework-exercise-solution
到包中的 colorbox 环境中,tcolorbox
使用选项invisible
通过自定义包开关显示空幻影框。(一旦我把一切都做得更完美了,我就会回答我自己的问题。)
现在,这意味着家庭作业练习和解决方案分散在整个练习册中,这是没问题的,因为它们放在相关内容的旁边。
不过,我想此外在文档末尾打印包含所有homework-exercise
和环境的副本的页面,以确保学生对必须完成的任务有一个很好的简短概述(并且我的导师有一个很好的简短的方法来检查填写的解决方案表)。homework-exercise-solution
我如何才能生成某些类型的所有环境的副本,同时保留它们第一次出现时分配的编号?(但是,必须调整重复项的标签,以免重写对最初放置的环境的引用)
%
%
%
\documentclass{article}
\begin{filecontents*}{testpackage.sty}
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{mw88-script}[2021/08/25 LaTeX package for toggling solutions]
\RequirePackage{tcolorbox}
\newcommand{\visibility}{invisible}
\DeclareOption{showsolutions}{\renewcommand{\visibility}{visible}}
\DeclareOption*{\PackageWarning{examplepackage}{Unknown ‘\CurrentOption’}}
\ProcessOptions\relax
\tcolorboxenvironment{exercise-homework}{
}
\tcolorboxenvironment{exercise-homework-solution}{
\visibility
}
\end{filecontents*}
\usepackage{mwe}
\usepackage{amssymb, amsmath, amsthm, mathtools}
% \usepackage{testpackage}
\usepackage[showsolutions]{testpackage}
\theoremstyle{plain}{
\newtheorem{exercise-homework}{Hausaufgabe}
}
\theoremstyle{plain}{
\newtheorem{exercise-homework-solution}{Lösung}
}
%
%
%
\begin{document}
\section{This is the guided workflow}
Here are explanatory things and prerequisites necessary to solve the exercise.
\begin{exercise-homework}
Show that $(\mathbb{R}, +, \cdot)$ is a field.
\end{exercise-homework}
\begin{exercise-homework-solution}%
Let $a, b, c \in \mathbb{R}$ arbitrary. Then, we show the following properties:
\begin{description}
\item[Commutativity of addition] $a + b = b+ a$.
\item[Associativity of addition] $a +(b + c) = (a+b) +c$.
\item[Additive identity] $0 \in \mathbb{R}$, and $a +0 = a $.
\item[Additive inverse] for each $a \in \mathbb{R}$, there exists $-a \in \mathbb{R}$, sch that $a + (-a) = 0$.
\item[Commutativity of multiplication] $a \cdot b = b \cdot a$.
\item[Associativity of multiplications] $a \cdot (b \dot c) = (a \cdot b) \cdot c$.
\item[Multiplicative identity] $1 \in \mathbb{R}$, such that $1 \cdot a = a$.
\item[Multiplicative inverse] for each $a \in \mathbb{R}$, there exists $a^{-1}\in \mathbb{R}$, such that $a \cdot a^{-1} = 1$.
\item[Distributivity] $a\cdot (b + c) = (a \cdot b) + (a \cdot c)$.
\end{description}
These are the necessary and sufficient properties of a field, hence $(\mathbb{R}, +, \cdot)$ is a field.
\end{exercise-homework-solution}
From here, the workbook content continues. There are going to be more exercises and solutions.
\section*{Appendix}
Here I want duplicates of the exercise and solutions from above.
\end{document}
可以在包选项中切换解决方案内容的显示。
更新 1:
进一步的调查显示,我应该能够使用thm-restate
来自的包来thmtools
重述定理类环境。但是,开箱即thm-restate
用效果不佳。请参阅下面的 MWE 和附加的屏幕截图:重述的环境未包装在 tcolorbox 中。tcolorbox
\documentclass[a4paper]{article}
\usepackage{thmtools}
\usepackage{thm-restate}
\usepackage[dvipsnames]{xcolor}
\usepackage[many]{tcolorbox}
\declaretheorem[name=Exercise, style=remark]{exercise}
\tcolorboxenvironment{exercise}{colframe=red!75!black}
\begin{document}
\noindent We start using the {\ttfamily exercise}-environment:
\begin{exercise}[restate=restatekey]
This is a demonstrational exercise.
\end{exercise}
\noindent and try to restate this:
\restatekey*
\noindent then follow it up with a {\ttfamily restatable}-environment
\begin{restatable}{exercise}{extest}\label{ex:test}
This is a restatable exercise.
\end{restatable}
\noindent In the following, there's the restated {\ttfamily restatable}-exercise:
\extest*
\end{document}
答案1
分步解决
解决缺失的tcolorbox
环境restatable
:
感谢这个仅稍微相关的问题,我能够让重述的环境与原始事件restatable
共享密钥:tcolorbox
\documentclass[a4paper]{article}
\usepackage{thmtools}
\usepackage{thm-restate}
\usepackage[dvipsnames]{xcolor}
\usepackage[many]{tcolorbox}
\makeatletter
\define@key{thmdef}{tcolorbox}[{}]{%
\thmt@trytwice{}{%
\RequirePackage{tcolorbox}%
\RequirePackage{thm-patch}%
\addtotheorempreheadhook[\thmt@envname]{%
\begin{tcolorbox}[#1]}%
\addtotheorempostfoothook[\thmt@envname]{\end{tcolorbox}}%
}%
}
\makeatother
\declaretheorem[name=Exercise,
style=remark,
tcolorbox={
colframe={red!75!black},
}]{exercise}
\begin{document}
\noindent A {\ttfamily restatable}-environment
\begin{restatable}{exercise}{extest}\label{ex:test}
This is a restatable exercise.
\end{restatable}
\noindent In the following, there's the restated {\ttfamily restatable}-exercise:
\extest*
\end{document}
但是,您不能使用以下形式来插入可重置环境:
\begin{exercise}[restate=restatekey]
This is a demonstrational exercise.
\end{exercise}
\restatekey*
将所有内容整合成最终的解决方案
我希望这对其他人有所帮助。我的大量注释的 MWE 代码
\begin{filecontents*}[overwrite]{testpackage.sty}
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{testpackage}[2021/08/25 LaTeX package for toggling solutions]
\RequirePackage{tcolorbox}
\RequirePackage{ifthen}
\RequirePackage{comment}
\RequirePackage{environ}
\RequirePackage{verbatim}
% process the options regarding solution visibility
\newcommand{\visibility}{visible}
\DeclareOption{hidesolutions}{\renewcommand{\visibility}{hidden}}
\DeclareOption{nosolutions}{\renewcommand{\visibility}{empty}}
% process all unknown options
\DeclareOption*{\PackageWarning{testpackage}{Unknown ‘\CurrentOption’}}
\ProcessOptions\relax
% this is the colorbox fix from first part of the solution
\define@key{thmdef}{tcolorbox}[]{%
\thmt@trytwice{}{%
\RequirePackage{tcolorbox}%
\RequirePackage{thm-patch}%
\addtotheorempreheadhook[\thmt@envname]{%
\begin{tcolorbox}[#1]}%
\addtotheorempostfoothook[\thmt@envname]{\end{tcolorbox}}%
}%
}
% define a list for storing the exercises
\def\exercises@list{}
% define a list for storing the exercises with their solutions
\def\exercisesandsolutions@list{}
% define a list for storing the exercises with their solutions set invivible
\def\exercisesandsolutionsinvisible@list{}
% define a list to store only the solutions
\def\solutions@list{}
\newcounter{exercise@cnt}
\newcounter{solution@cnt}
% define the exercise theorem environment (for inner use)
\declaretheorem[name={Exercise},numberwithin=section, tcolorbox={
colframe={red!75!black},
}]{thmexercise}
% define the solution theorem environment (for inner use)
\declaretheorem[name={Solution},numberwithin=section, tcolorbox={
colframe={black},
}]{thmsolution}
% define the invisible solution theorem environment (for inner use)
\declaretheorem[name={Solution},numberwithin=section, tcolorbox={
colframe={black},
invisible
}]{thmsolutionhidden}
% define the actual exercise environment
\newenvironment{exercise}[1][]{%
\stepcounter{exercise@cnt}%
% fully expand the counter representation
% before calling \restatable
\begingroup\edef\x{\endgroup
\noexpand\restatable[{#1}]{thmexercise}{exercise-\Alph{exercise@cnt}}%
}\x
}{\endrestatable
\xappto\exercises@list{%
\expandafter\noexpand\csname exercise-\Alph{exercise@cnt}\endcsname*%
}%
% append the exercise also to all other relevant lists
\xappto\exercisesandsolutions@list{%
\expandafter\noexpand\csname exercise-\Alph{exercise@cnt}\endcsname*%
}%
\xappto\exercisesandsolutionsinvisible@list{%
\expandafter\noexpand\csname exercise-\Alph{exercise@cnt}\endcsname*%
}%
}
% define the actual solution environment
\newenvironment{solution}[1][]{%
\stepcounter{solution@cnt}%
% fully expand the counter representation
% before calling \restatable
\def\x{}
\ifthenelse{\equal{\visibility}{visible}}{
\begingroup\edef\x{\endgroup
\noexpand\restatable[{#1}]{thmsolution}{solution-\Alph{solution@cnt}}%
}}{}
\ifthenelse{\equal{\visibility}{hidden}}{
\begingroup\edef\x{\endgroup
\noexpand\restatable[{#1}]{thmsolutionhidden}{solution-\Alph{solution@cnt}}%
}}{}
\ifthenelse{\equal{\visibility}{empty}}{
\setbox0\vbox\bgroup
}{
\x%
}
}{
\ifthenelse{{\equal{\visibility}{visible}}\OR{\equal{\visibility}{hidden}}}{
\endrestatable
\xappto\solutions@list{%
\expandafter\noexpand\csname solution-\Alph{solution@cnt}\endcsname*%
}%
% append the exercise also to all other relevant lists
\xappto\exercisesandsolutions@list{%
\expandafter\noexpand\csname solution-\Alph{solution@cnt}\endcsname*%
}%
}{
\egroup
}
}
% define shortcuts for displaying the lists
\newcommand{\allexercises}{\exercises@list}
\newcommand{\allsolutions}{\solutions@list}
\newcommand{\allexercisesandsolutions}{\exercisesandsolutions@list}
\end{filecontents*}
\documentclass{article}
\usepackage[a4paper, margin=1in]{geometry}
\usepackage{thmtools}
\usepackage{thm-restate}
\usepackage[dvipsnames]{xcolor}
\usepackage[many]{tcolorbox}
\usepackage[]{testpackage}
% \usepackage[hidesolutions]{testpackage}
% \usepackage[nosolutions]{testpackage}
\begin{document}
\section{Originals}
Some text.
\begin{exercise}[Title?]
This is the first recommendation.
\end{exercise}
We can of course have text between solution and exercise.
\begin{solution}[A named solution]
This is the solution after the first exercise.
\end{solution}
Some text.
\begin{exercise}
This is the second recommendation.
\end{exercise}
And even more text.
\begin{solution}
This is the solution after the second exercise.
\end{solution}
Some text.
\section{All Exercises}
\allexercises
\section{All Solutions}
\allsolutions
\section{All Exercises with Solutions}
\allexercisesandsolutions
\end{document}
产生以下输出(基于包选项):
我现在可以使用testpackage
选项| hidesolutions | emptysolutions
来产生不同的预期结果。