Пакет 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 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(например, через package letltxmacro) и переопределение \chapterдля выполнения сохраненной версии и добавления \chaplabel.
  • В зависимости от необходимых типов аргументов (форма звезды, необязательный аргумент) (пере)определения можно более или менее легко усложнить.

Версия, где \chaplabelи \seclabelиспользуются в <after code>аргументе \titleformat, предоставленная пакетом 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}
[...]

Связанный контент