我正在用exsheets
包排版一本問題書。我希望解決方案的結構能夠重複問題集的結構。Chapter-hook
和section-hook
選項允許在列印新章節/部分的解決方案時將自訂程式碼新增至解決方案清單。這是我的 MWE:
\documentclass{book}
\makeatletter
\@addtoreset{chapter}{part} % Reset \chapter numbering after each \part
\makeatother
\usepackage{exsheets}
\SetupExSheets{
chapter-hook = \chapter{Solutions to the chapter \thechapter},
section-hook = \section{Solutions to the section \thesection},
}
\begin{document}
\tableofcontents
\part{Problems}
\chapter{Mechanics}
\section{Kinematics}
\begin{question}
A kinematics problem.
\begin{solution}
Solution to the kinematics problem.
\end{solution}
\end{question}
\section{Dynamics}
\begin{question}
A dynamics problem.
\begin{solution}
Solution to the dynamics problem.
\end{solution}
\end{question}
\chapter{Electricity}
\section{Electrostatics}
\begin{question}
An electrostatics problem.
\begin{solution}
Solution to the electrostatics problem.
\end{solution}
\end{question}
\section{Electrodynamics}
\begin{question}
An electrodynamics problem.
\begin{solution}
Solution to the electrodynamics problem.
\end{solution}
\end{question}
\part{Solutions}
\printsolutions
\end{document}
我想要實現的是取代1 Solutions to Mechanics
current 1 Solutions to the chapter 1
,2 Solutions to Electricity
而不是2 Solutions to the chapter 2
等等。任何幫助表示讚賞。
答案1
透過包的引用解決方案nameref
。章/節編號用作標籤名稱。
\documentclass{book}
\makeatletter
\@addtoreset{chapter}{part} % Reset \chapter numbering after each \part
\makeatother
\usepackage{exsheets}
\usepackage{nameref}
\SetupExSheets{
chapter-hook = \chapter{Solutions to \nameref{CHAP:\thechapter}},
section-hook = \section{Solutions to \nameref{SEC:\thesection}},
}
\newcommand*{\chaplabel}{\label{CHAP:\thechapter}}
\newcommand*{\seclabel}{\label{SEC:\thesection}}
\begin{document}
\tableofcontents
\part{Problems}
\chapter{Mechanics}\chaplabel
\section{Kinematics}\seclabel
\begin{question}
A kinematics problem.
\begin{solution}
Solution to the kinematics problem.
\end{solution}
\end{question}
\section{Dynamics}\seclabel
\begin{question}
A dynamics problem.
\begin{solution}
Solution to the dynamics problem.
\end{solution}
\end{question}
\chapter{Electricity}\chaplabel
\section{Electrostatics}\seclabel
\begin{question}
An electrostatics problem.
\begin{solution}
Solution to the electrostatics problem.
\end{solution}
\end{question}
\section{Electrodynamics}\seclabel
\begin{question}
An electrodynamics problem.
\begin{solution}
Solution to the electrodynamics problem.
\end{solution}
\end{question}
\part{Solutions}
\printsolutions
\end{document}
標籤設定可以包含在\chapter
和中\section
:
\mychapter
新增的新命令\chaplabel
。- 儲存舊定義
\chapter
(例如透過套件letltxmacro
)並重新定義\chapter
以執行已儲存的版本並附加\chaplabel
。 - 根據所需的參數類型(星形形式、可選參數),(重新)定義或多或少容易或更複雜。
版本,其中\chaplabel
和\seclabel
被用在<after code>
的參數中\titleformat
,由 package 提供titlesec
:
\documentclass{book}
\makeatletter
\@addtoreset{chapter}{part} % Reset \chapter numbering after each \part
\makeatother
\usepackage{exsheets}
\usepackage{nameref}
\SetupExSheets{
chapter-hook = \chapter{Solutions to \nameref{CHAP:\thechapter}},
section-hook = \section{Solutions to \nameref{SEC:\thesection}},
}
\newcommand*{\chaplabel}{\label{CHAP:\thechapter}}
\newcommand*{\seclabel}{\label{SEC:\thesection}}
\usepackage{titlesec}
\titleformat{\chapter}{\normalfont\LARGE\bfseries}{\thechapter}{1em}%
{}[\chaplabel]
\titleformat{\section}{\normalfont\Large\bfseries}{\thesection}{1em}%
{}[\seclabel]
\usepackage{etoolbox}
\pretocmd\printsolutions{%
\let\chaplabel\relax
\let\seclabel\relax
}{}{\errmessage{Patching \noexpand\printsolutions failed.}}
\begin{document}
\tableofcontents
\part{Problems}
\chapter{Mechanics}
\section{Kinematics}
\begin{question}
A kinematics problem.
\begin{solution}
Solution to the kinematics problem.
\end{solution}
\end{question}
\section{Dynamics}
\begin{question}
A dynamics problem.
\begin{solution}
Solution to the dynamics problem.
\end{solution}
\end{question}
\chapter{Electricity}
\section{Electrostatics}
\begin{question}
An electrostatics problem.
\begin{solution}
Solution to the electrostatics problem.
\end{solution}
\end{question}
\section{Electrodynamics}
\begin{question}
An electrodynamics problem.
\begin{solution}
Solution to the electrodynamics problem.
\end{solution}
\end{question}
\part{Solutions}
\printsolutions
\end{document}
避免無限遞歸的技巧評論是在處理 的章節之前禁用\chaplabel
和。\seclabel
\printsolutions
與開關略有不同\ifwithlabels
。然後可以對未編號的章節停用標籤設置,例如:
[...]
\newif\ifwithlabels
\newcommand*{\chaplabel}{\ifwithlabels\label{CHAP:\thechapter}\fi}
\newcommand*{\seclabel}{\ifwithlabels\label{SEC:\thesection}\fi}
\usepackage{titlesec}
\titleformat{\chapter}{\normalfont\LARGE\bfseries}{\thechapter}{1em}%
{}[\chaplabel]
\titleformat{\section}{\normalfont\Large\bfseries}{\thesection}{1em}%
{}[\seclabel]
\usepackage{etoolbox}
\pretocmd\printsolutions{%
\withlabelsfalse
}{}{\errmessage{Patching \noexpand\printsolutions failed.}}
\begin{document}
\tableofcontents
\withlabelstrue
\part{Problems}
[...]