Pacote Exsheets: use o nome do capítulo na opção Chapter-Hook

Pacote Exsheets: use o nome do capítulo na opção Chapter-Hook

Estou compondo um livro de problemas com o exsheetspacote. Quero que a estrutura das soluções repita a estrutura dos problemas definidos. Chapter-hooke section-hookas opções permitem adicionar código personalizado à lista de soluções quando as soluções de um novo capítulo/seção são impressas. Aqui está meu 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}

O índice produzido é o seguinte:toc

O que eu gostaria de alcançar é ter 1 Solutions to Mechanicsem vez de current 1 Solutions to the chapter 1, 2 Solutions to Electricityem vez de 2 Solutions to the chapter 2e assim por diante. Qualquer ajuda é apreciada.

Responder1

Uma solução através de referências de package nameref. O número do capítulo/seção é usado como nome do rótulo.

\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}

Resultado

As configurações do rótulo podem ser incluídas em \chaptere \section:

  • Novo comando \mychapterque adiciona \chaplabel.
  • Salvando a definição antiga de \chapter(por exemplo, via package letltxmacro) e redefinindo \chapterpara executar a versão salva e acrescenta \chaplabel.
  • Dependendo dos tipos de argumentos necessários (forma de estrela, argumento opcional), as (re)definições são mais ou menos fáceis ou mais complicadas.

Versão, onde \chaplabele \seclabelsão usados ​​no <after code>argumento de \titleformat, fornecidos por 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}

O truque para evitar a recursão infinita como noComenteé desabilitar \chaplabele \seclabelantes que os capítulos e seções \printsolutionssejam processados.

Pequena variação com um interruptor \ifwithlabels. Em seguida, a configuração do rótulo pode ser desativada para capítulos não numerados, por exemplo:

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

informação relacionada