如果我使用 Sebastian 的问题公式提供的代码用于运动环境。
\documentclass{article}
\usepackage[most]{tcolorbox}
\usepackage{lipsum}
\newtcolorbox[auto counter]{problem}[2][]{%
breakable,
blankest,
enhanced,
attach boxed title to top left={yshift=-\tcboxedtitleheight},
left=\tcboxedtitlewidth+1mm,
top=0pt,
colback=white,
fonttitle=\bfseries\sffamily,
coltitle={cyan!60!black},
colbacktitle=white,
colframe=white,
title=\thetcbcounter,
overlay unbroken and first={%
\node[fill=gray!30, inner sep=1pt, minimum width=\tcboxedtitlewidth,
outer sep=0pt, text=cyan!60!black, anchor=north] at(title.south)
{\foreach \i in {1,...,#2}{\if#2gt1{\,}\fi$\bullet$}};},#1}
\begin{document}
\lipsum[1]
\begin{problem}{1}
\lipsum[2]
\end{problem}
\begin{problem}{4}
\lipsum[3]
\end{problem}
\begin{problem}{2}
\lipsum[4]
\end{problem}
\end{document}
我们如何创建解决方案环境,以便我们在书的末尾提供练习的解决方案,其中每个练习都有章节,页面,练习数量以及相应的难度级别?
答案1
我建议您阅读exercise
包文档。
有以下套餐选项:
lastexercise
如果答案没有给出参考文献,那么答案应该参考最后的练习(见第 2.1 节)。
和
answerdelayed
保存答案,而不是排版。稍后可以使用命令将答案包含在文档中\shipoutAnswer
。
和Exercise
环境选项:
difficulty
练习的难度(数字)。稍后将通过计数器显示\ExerciseDifficulty
。
您可以从这个简单示例开始,然后进行自定义。如您所见,如果您Answer
在相应的后面立即写下您的环境Exercise
,则答案会自动加入到上一个练习中。
\documentclass[11pt]{book}
\usepackage[lastexercise,answerdelayed]{exercise}
\usepackage{lipsum}
\begin{document}
\chapter{Test 1}
\lipsum[1]
\section{Problems}
\begin{Exercise}[difficulty={1}]
My easy exercise.
\end{Exercise}
\begin{Answer}
My answer to my easy exercise.
\end{Answer}
\chapter{Test 2}
\lipsum[1]
\section{Problems}
\begin{Exercise}[difficulty={2}]
My difficult exercise.
\end{Exercise}
\begin{Answer}
My answer to my difficult exercise.
\end{Answer}
\chapter{Solutions}
\shipoutAnswer
\end{document}
答案2
这是针对问题 - 解决方案环境的 MWE,它结合了@Sebastiano 代码和@CarLaTeX 代码数学书:如何写练习和答案
\documentclass[11pt]{book}
\usepackage[most]{tcolorbox}
\usepackage{lipsum}
\usepackage{multicol}
\usepackage{ifthen}
\usepackage{xcolor}
\colorlet{lightcyan}{cyan!40!white}
\usepackage{chngcntr}
\usepackage{stackengine}
\usepackage{tasks}
\newboolean{firstanswerofthechapter}
\colorlet{lightcyan}{cyan!40!white}
\newtcolorbox[auto counter]{problem}[2][]{%
breakable,
blankest,
enhanced,
attach boxed title to top left={yshift=-\tcboxedtitleheight},
left=\tcboxedtitlewidth+2mm,
top=0pt,
colback=white,
fonttitle=\bfseries\sffamily,
coltitle={cyan!60!black},
colbacktitle=white,
colframe=white,
title=\thetcbcounter,
overlay unbroken and first={%
\node[fill=gray!30, inner sep=1pt,
minimum width=\tcboxedtitlewidth,
outer sep=0pt,
anchor=north] at (title.south) {%
\foreach \i in {1,...,4}{%
\ifnum\i>#2%
\,\textcolor{cyan!60!black}{$\bullet$}%
\else
\,\textcolor{red}{$\bullet$}%
\fi}};},
#1}
\newtcolorbox{exsolution}[2]{%
breakable,
blankest,
enhanced,
attach boxed title to top left={yshift=-\tcboxedtitleheight},
left=\tcboxedtitlewidth+2mm,
top=0pt,
colback=white,
fonttitle=\bfseries\sffamily,
coltitle={cyan!60!black},
colbacktitle=white,
colframe=white,
title=#1,
overlay unbroken and first={%
\node[fill=gray!30, inner sep=1pt,
minimum width=\tcboxedtitlewidth,
outer sep=0pt,
anchor=north] at (title.south) {%
\foreach \i in {1,...,4}{%
\ifnum\i>#2%
\,\textcolor{cyan!60!black}{$\bullet$}%
\else
\,\textcolor{red}{$\bullet$}%
\fi}};},
overlay unbroken and first={%
\node[fill=gray!30, inner sep=1pt,
minimum width=\tcboxedtitlewidth,
outer sep=0pt,
anchor=north] at (title.south) {%
\foreach \i in {1,...,4}{%
\ifnum\i>#2%
\,\textcolor{cyan!60!black}{$\bullet$}%
\else
\,\textcolor{red}{$\bullet$}%
\fi}};}, }
\newlength{\longestlabel}
\settowidth{\longestlabel}{\bfseries viii.}
\settasks{counter-format={tsk[r].}, label-format={\bfseries}, label-width=\longestlabel,
item-indent=0pt, label-offset=2pt, column-sep={10pt}}
\usepackage[lastexercise,answerdelayed]{exercise}
\renewcommand{\ExerciseName}{EXERCISES}
\renewcommand{\ExerciseHeader}{}
\renewcommand{\AnswerName}{Problems}
\renewcommand{\AnswerHeader}{\ifthenelse{\boolean{firstanswerofthechapter}}%
{\bigskip\noindent\textcolor{cyan}{\textbf{CHAPTER \thechapter}}\newline\newline%
\noindent\bfseries\emph{\textcolor{cyan}{\AnswerName\ {\thesection}, page %
\pageref{\AnswerRef}}}\smallskip}
{\noindent\bfseries\emph{\textcolor{cyan}{\AnswerName\ {\thesection} page \pageref{\AnswerRef}}}\smallskip}}
\setlength{\QuestionIndent}{16pt}
\begin{document}
\chapter{Test 1}
\lipsum[1]
\section{Problems}
\begin{Exercise}\label{EX-1.1}
\begin{problem}{1}
\lipsum[2]
\end{problem}
\end{Exercise}
\chapter{Test 2}
\lipsum[1]
\section{Problems}
\begin{Exercise}\label{EX-2.1}
\begin{problem}{4}
\lipsum[3]
\end{problem}
\begin{problem}{2}
\lipsum[4]
\end{problem}
\end{Exercise}
\setboolean{firstanswerofthechapter}{true}
\begin{Answer}[ref={EX-2.1}]
\begin{exsolution}{3}{2}
\lipsum[2]
\end{exsolution}
\end{Answer}
\chapter{Solutions}
\shipoutAnswer
\end{document}
然而,正如我们所见,这是一个相当笨拙的解决方案。在我看来,最实用的方法是立即为问题和相应的解决方案建立统一的环境。比如
\begin{prob}
\lipsum[2]
\begin{solution}
.....
\end{solution}
\end{prob}
因此,此解决方案(如果存在)将自动发送到包含练习解决方案的章节。不幸的是,我没有足够的 LaTeX 经验和知识来做到这一点。