Paquete de hojas sueltas: use el nombre del capítulo en la opción de gancho de capítulo

Paquete de hojas sueltas: use el nombre del capítulo en la opción de gancho de capítulo

Estoy escribiendo un libro de problemas con el exsheetspaquete. Quiero que la estructura de soluciones repita la estructura de los problemas planteados. Chapter-hooky section-hooklas opciones permiten agregar código personalizado a la lista de soluciones cuando se imprimen las soluciones de un nuevo capítulo/sección. Aquí está mi 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}

El índice de contenidos producido es el siguiente:toc

Lo que me gustaría lograr es tener 1 Solutions to Mechanicsen lugar de actual 1 Solutions to the chapter 1, 2 Solutions to Electricityen lugar de 2 Solutions to the chapter 2y así sucesivamente. Se agradece cualquier ayuda.

Respuesta1

Una solución a través de referencias del paquete nameref. El número de capítulo/sección se utiliza como nombre de etiqueta.

\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

La configuración de la etiqueta se puede incluir en \chaptery \section:

  • Nuevo comando \mychapterque agrega \chaplabel.
  • Guardar la definición anterior de \chapter(por ejemplo, a través del paquete letltxmacro) y redefinirla \chapterpara ejecutar la versión guardada y los anexos \chaplabel.
  • Dependiendo de los tipos de argumentos necesarios (forma de estrella, argumento opcional), las (re)definiciones son más o menos fáciles o más complicadas.

Versión, donde \chaplabely \seclabelse utilizan en el <after code>argumento de \titleformat, proporcionada por el paquete 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}

El truco para evitar la recursividad infinita como en elcomentarioes deshabilitar \chaplabely antes de que se procesen \seclabellos capítulos y secciones de .\printsolutions

Ligera variación con un interruptor \ifwithlabels. Luego, la configuración de la etiqueta se puede desactivar para capítulos sin numerar, por ejemplo:

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

información relacionada