Exsheets パッケージ: chapter-hook オプションで章名を使用する

Exsheets パッケージ: chapter-hook オプションで章名を使用する

私はパッケージを使って問題集をタイプセットしています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現在の の代わりに1 Solutions to the chapter 12 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>\titleformattitlesec

\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}
[...]

関連情報