Exsheets-Paket: Verwenden Sie den Kapitelnamen in der Kapitel-Hook-Option

Exsheets-Paket: Verwenden Sie den Kapitelnamen in der Kapitel-Hook-Option

Ich setze mit dem exsheetsPaket ein Aufgabenbuch. Ich möchte, dass die Struktur der Lösungen die Struktur des Aufgabensatzes wiederholt. Chapter-hookund section-hookOptionen ermöglichen das Hinzufügen von benutzerdefiniertem Code zur Liste der Lösungen, wenn die Lösungen eines neuen Kapitels/Abschnitts gedruckt werden. Hier ist mein 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}

Das erstellte Inhaltsverzeichnis sieht wie folgt aus:Inhaltsverzeichnis

Ich möchte erreichen, dass anstelle 1 Solutions to Mechanicsvon current anstelle von usw. gilt. Für jede Hilfe bin ich dankbar.1 Solutions to the chapter 12 Solutions to Electricity2 Solutions to the chapter 2

Antwort1

Eine Lösung über Referenzen des Pakets nameref. Als Labelname wird die Kapitel-/Abschnittsnummer verwendet.

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

Ergebnis

Die Beschriftungseinstellungen können in \chapterFolgendes einbezogen werden \section:

  • Neuer Befehl \mychapter, der hinzufügt \chaplabel.
  • Speichern der alten Definition \chapter(z. B. über das Paket letltxmacro) und erneutes Definieren \chapterzum Ausführen der gespeicherten Version und Anhängen \chaplabel.
  • Abhängig von den benötigten Argumenttypen (Sternform, optionales Argument) sind die (Neu-)Definitionen mehr oder weniger einfach bis komplizierter.

Version, wobei \chaplabelund \seclabelim <after code>Argument von verwendet werden \titleformat, bereitgestellt vom Paket 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}

Der Trick, um die unendliche Rekursion zu vermeiden, wie in derKommentarist zu deaktivieren \chaplabelund \seclabelbevor die Kapitel und Abschnitte \printsolutionsverarbeitet werden.

Kleine Variation mit einem Schalter \ifwithlabels. Dann kann die Beschriftungseinstellung für nicht nummerierte Kapitel deaktiviert werden, zB:

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

verwandte Informationen