I'm writing a book and for it I have created a little class file.
Each section in the book have (sub-)section of exercises. At the end of all the chapters I want to put a chapter*
of answers to some of the exercises. Since the exercises are numbered by section (i.e. the exercise's counter reset whenever the section step), I want to group the answers according to the section (and the chapter) of the exercise they answer to. What I want is something like this:
Here is the relevant code of the class file.
The commented lines are my unsuccessful attempts to achieve what I want. I have leaved almost all the packages to see if there some compatibility problem.
\NeedsTeXFormat{LaTeX2e}[2009/09/24]
\ProvidesClass{libro}
\LoadClassWithOptions{book}
\RequirePackage[pdftex]{graphicx}
\RequirePackage[latin1]{inputenc}
\RequirePackage[spanish]{babel}
\RequirePackage{amsmath,amsfonts,amssymb,amsthm}
\RequirePackage{array}
\RequirePackage{fancyhdr}
\RequirePackage{afterpage}
\RequirePackage{thmtools}
\RequirePackage{multicol}
\RequirePackage{color}
\RequirePackage{import}
\RequirePackage[font=small]{caption}
\RequirePackage{float}
\RequirePackage{subfig}
\RequirePackage{ifthen}
\RequirePackage{calc}
\RequirePackage{remreset}
\RequirePackage{pb-diagram}
\RequirePackage[hidelinks]{hyperref}
\RequirePackage[capitalise,noabbrev,spanish]{cleveref}
\RequirePackage{answers}
\newcounter{cuentaejercicios}[section]
\renewcommand{\thecuentaejercicios}{\Alph{cuentaejercicios}}
\declaretheoremstyle[%
spaceabove=1em,%
spacebelow=1em,%
headfont=\normalfont,%
postheadspace=1em,%
headpunct={.}]{estilodeejercicio}
\declaretheorem[
name={},
style=estilodeejercicio,
refname={Ejercicio},
numberlike=cuentaejercicios
]{ejer}
\renewcommand\paragraph{\@startsection
{paragraph}{3}{0cm}
{6pt}
{1pt}
{\bfseries}}
%\let\c@hapter\chapter
%\renewcommand{\chapter}{
% \c@hapter
% \Writetofile{solucionario}{\protect\paragraph*{Capítulo \Roman{\thechapter}.\space
% \leftmark}}
%}
\newcommand{\seccionDeEjercicios}{%
\newpage%
\kern 5mm
\hrule
\kern 3mm
\null \hfill {\large Ejercicios~\thesection.~\rightmark}\par
\kern 3mm
\hrule
\kern 5mm
\Writetofile{solucionario}{\protect\paragraph*{Ejercicios\space\thesection.
\space\rightmark}}
}
\newcommand{\Z}{\ensuremath{\mathbb{Z}}}
\newcommand{\R}{\ensuremath{\mathbb{R}}}
\newcommand{\N}{\ensuremath{\mathbb{N}}}
\newcommand{\Q}{\ensuremath{\mathbb{Q}}}
\newcommand{\nin}{\not \in}
\newcommand{\ds}[1]{\displaystyle{#1}}
\AtBeginDocument{
\addtocontents{toc}{\protect\markboth{\small\textsl Contenidos}{\small\textsl
Contenidos}}
\pagestyle{fancy}
\fancyhf{}
\renewcommand\headrulewidth{0pt}
\renewcommand{\chaptermark}[1]{\markboth{#1}{}}
\renewcommand{\sectionmark}[1]{\markright{#1}{}}
\fancyhead[LO]{\small\textsl{\leftmark}}
\fancyhead[RE]{\small\textsl{\rightmark}}
\fancyhead[LE,RO]{\thepage}
\Opensolutionfile{solucionario}
\Newassociation{respuesta}{Res}{solucionario}
\renewcommand{\Res}[1]{\textbf{Ejercicio #1.}}
\Writetofile{solucionario}{\protect\chapter*{Respuestas}}
% \begin{Filesave}{solucionario}
% \protect\chapter*{Respuestas}
% \protect\fancyhf{}
% \protect\fancyhead[LO]{\small\textsl{Soluciones a los ejercicios}}
% \protect\fancyhead[RE]{\small\textsl{Soluciones a los ejercicios}}
% \protect\fancyhead[LE,RO]{\thepage}
% \end{Filesave}
}
The \seccionDeEjercicios
command was provided by this answer.
Here is an example of my document:
\documentclass[12pt,letterpaper]{libro}
\usepackage[
% showframe,
headheight=14.5pt,text={14cm,21cm},centering
]{geometry}
\usepackage{enumerate}
\usepackage{cases}
\parindent=0cm
\begin{document}
\chapter{Números Reales}
bla bla bla blabla blabla blabla blabla blabla blabla blabla blabla blabla blabla blabla
bla bla bla blabla blabla blabla blabla blabla blabla blabla blabla blabla blabla blabla
bla bla bla blabla blabla blabla blabla blabla blabla blabla blabla blabla blabla blabla
bla bla bla blabla blabla blabla blabla blabla blabla blabla blabla blabla blabla blabla
\chapter{Álgebra}
\section{Operaciones con polinomios}
bla bla bla blabla blabla blabla blabla blabla blabla blabla blabla blabla blabla blabla
bla bla bla blabla blabla blabla blabla blabla blabla blabla blabla blabla blabla blabla
bla bla bla blabla blabla blabla blabla blabla blabla blabla blabla blabla blabla blabla
bla bla bla blabla blabla blabla blabla blabla blabla blabla blabla blabla blabla blabla
\seccionDeEjercicios
\begin{ejer}
Efectúe las operaciones indicadas. Exprese el resultado como un
polinomio reducido.
\begin{multicols}{2}
\begin{enumerate}
\item $\left( x+\dfrac{1}{2}\right) ^{2}$
\item $\left( 3-2x\right) ^{2}$
\item $\left( x-\dfrac{2}{3}\right) ^{3}$
\item $\left( 2-x\right) ^{3}$
\item $\left( 2x^{2}-\dfrac{1}{2}+x^{2}\right) ^{2}$
\item $\left( -x-y\right) ^{2}$
\item $\left( -x-y\right) ^{3}$
\item $\left( -x-2y-4x\right) ^{3}$
\item $-\left( 2x^{3}-3t^{3}\right) ^{2}$
\item $\left(-2+x^{2}-3c-x^{2}\right) ^{2}$
\item $\left( \dfrac{x+2u^{2}}{3}\right) ^{2}$
\item $-\left( \dfrac{x^{2}-u^{2}}{2}\right) ^{2}$
\item $\left( \left( x-r\right)^{2}-r^{2}\right) ^{2}$
\item $3\left( x-2t\right) ^{4}-3\left(2t-x\right) ^{4}$
\end{enumerate}
\end{multicols}
\begin{respuesta}
\begin{multicols}{2}
\begin{enumerate}
\item $x^{2}+x+\dfrac{1}{4}$
\item $4x^{2}-12x+9$
\item $ x^{3}-2x^{2}+\dfrac{4}{3}x-\dfrac{8}{27}$
\item $-x^{3}+6x^{2}-12x+8$
\item $ 9x^{4}-3x^{2}+\dfrac{1}{4}$
\item $ x^{2}+2xy+y^{2}$
\item $-x^{3}-3x^{2}y-3xy^{2}-y^{3}$
\item $-125x^{3}-150x^{2}y-60xy^{2}-8y^{3}$
\item $-9t^{6}+12t^{3}x^{3}-4x^{6}$
\item $9c^{2}+12c+4$
\item $\dfrac{4}{9}u^{4}+\dfrac{4}{9}u^{2}x+\dfrac{1}{9}x^{2}$
\item $-\dfrac{1}{4}u^{4}+\dfrac{1}{2}u^{2}x^{2}-\dfrac{1}{4}x^{4}$
\item $4r^{2}x^{2}-4rx^{3}+x^{4}$
\end{enumerate}
\end{multicols}
\end{respuesta}
\end{ejer}
\begin{ejer}
Determine el cociente y el residuo en cada caso.
\begin{enumerate}
\item $\left( 2x^{3}-x^{2}+2x-1\right) \div \left( x-2\right) $
\item $\left( 4x^{3}+2x^{2}-3x-5\right) \div \left( 2x-1\right) $
\item $\left( x^{8}+x^{6}+x^{4}-x^{2}+x\right) \div \left( x-3\right) $
\item $\left( x^{4}-x^{3}-x^{2}+x^{3}+1\right) \div \left( -x+1\right) $
\item $\left( x^{3}+x^{6}+2x^{5}-\dfrac{x^{2}}{2}+x\right) \div \left(x-1\right) $
\end{enumerate}
\end{ejer}
\newpage
\begin{ejer}
Efectúe las operaciones indicadas. Exprese el resultado como un
polinomio reducido.
\begin{enumerate}
\item $-(7x^{2}+3x-4)+(8-x^{2}+5x^{3})-(2x^{3}+3x^{2}-5+8x)$
\item $\left( 11x^{3}-x+7\right) -\left( 2x-4x^{3}-6\right) +\left(7x^{2}-9x+4\right) $
\item $\left( x^{2}+2xy+4y^{2}\right) \left( x-2y\right) $
\item $5a^{3}b^{2}\left( ab^{2}-b+4a\right) -(8a^{3}b^{3}-10a^{4}b^{4})$
\item $(3x^{2}-8x+7)(x^{3}+x)+(2x^{3}-6x+2)$
\item $\dfrac{-36x^{2}y^{2}+24x^{2}y^{2}}{-12x^{2}y^{2}}$
\item $\dfrac{2a^{4}b^{6}c-4a^{5}b^{3}c^{2}+5a^{3}b^{3}c^{3}}{2a^{2}b^{3}c}$
\item $19x^{2}-10x^{3}+x^{5}-14x+6)\div (x^{2}+1-2x)$
\item $(2x^{3}-31x+35-x^{2})\div (2x-7)$
\item $(2-x^{5})\div (x^{2}+1)$
\item $(3x-5x^{2}+9+x^{3})\div (3-x)$
\item $(2x+3)^{2}-(2x-3)^{2}$
\item $(3x+1)(x+4)-(x+2)^{2}$
\item $(7x^{2}-6)(x+1)+(3x-4)^{3}$
\item $-5(a-b^{2})^{2}+(3a+b)^{3}$
\item $(2x^{2}-3)(3x^{2}-5)-(5x^{2}+6)^{2}$
\item $3x(x-2)+(3x-1)(x+5)-(3-x)^{3}$
\item $\left( 7a+1\right) \left( 7a-1\right) -\left( 4a+1\right)^{3}+2a^{2}\left(
a-\dfrac{1}{4}\right) ^{2}$
\end{enumerate}
\begin{respuesta}
\begin{multicols}{2}
\begin{enumerate}
\item[1.] $3x^{3}-11x^{2}-11x+17$
\item[3.] $x^{3}-8y^{3}$
\item[5.] $3x^{5}-8x^{4}+12x^{3}-8x^{2}+x+2$
\item[7.] $-2a^{3}c+a^{3}b^{3}+\frac{5}{2}ac^{2}$
\item[9.] $x^{2}+3x+5$
\item[11.] $-x^{2}+2x+3$
\item[13.] $2x^{2}+9x$
\item[17.] $x^{3}-3x^{2}+35x-32$
\end{enumerate}
\end{multicols}
\end{respuesta}
\end{ejer}
\Closesolutionfile{solucionario}
\input{solucionario}
\end{document}
Edit: Here the relevant code of my class, with chapters based on fncychap
(as requested). Some code to differentiate the style of the chapters, was provided by egreg in this.
\NeedsTeXFormat{LaTeX2e}[2009/09/24]
\ProvidesClass{libro}
\LoadClassWithOptions{book}
\RequirePackage[pdftex]{graphicx}
\RequirePackage[latin1]{inputenc}
\RequirePackage[spanish]{babel}
\RequirePackage{amsmath,amsfonts,amssymb,amsthm}
\RequirePackage{array}
\RequirePackage{fancyhdr}
\RequirePackage[Sonny]{fncychap}
\RequirePackage{afterpage}
\RequirePackage{thmtools}
\RequirePackage{multicol}
\RequirePackage{color}
\RequirePackage{import}
\RequirePackage[font=small]{caption}
\RequirePackage{float}
\RequirePackage{subfig}
\RequirePackage{ifthen}
\RequirePackage{calc}
\RequirePackage{remreset}
\RequirePackage{pb-diagram}
\RequirePackage[hidelinks]{hyperref}
\RequirePackage[capitalise,noabbrev,spanish]{cleveref}
\RequirePackage{answers}
% Chapters
\ChNameVar{\bf\LARGE}
\ChNumVar{\LARGE}
\ChTitleVar{\LARGE\normalfont}
\ChRuleWidth{0.5pt}
\ChNameAsIs
\ChTitleAsIs
\renewcommand{\DOCH}{}
\renewcommand{\DOTI}[1]{%
\vspace*{-0.9cm}
\CTV\raggedleft\mghrulefill{\RW}\par\nobreak
\vskip 2\p@
\CNV\FmN{\@chapapp}\space\CNoV\Roman{chapter}.\space\CTV\FmTi{#1}\par\nobreak
\vspace*{-11\p@}
\mghrulefill{\RW}\par\nobreak
\vspace*{40\p@}}
\renewcommand{\DOTIS}[1]{%
\vspace*{-9mm}
\CTV\raggedleft
\CTV\FmTi{#1}\par\nobreak
\vspace*{-11\p@}
\mghrulefill{\RW}\par\nobreak
\vspace*{40\p@}}
\renewcommand\@seccntformat[1]{
{\csname the#1\endcsname}.\hspace{0.5em}}
\renewcommand\section{\@startsection
{section}{1}{0mm}
{6pt}
{1pt}
{\bfseries}}
\renewcommand\paragraph{\@startsection
{paragraph}{3}{0cm}
{6pt}
{1pt}
{\bfseries}}
\newcounter{cuentaEjercicios}[section]
\renewcommand{\thecuentaEjercicios}{\Alph{cuentaEjercicios}}
\declaretheoremstyle[%
spaceabove=1em,%
spacebelow=1em,%
headfont=\normalfont,%
postheadspace=1em,%
headpunct={.}]{estiloDeEjercicio}
\declaretheorem[
name={},
style=estiloDeEjercicio,
refname={Ejercicio},
numberlike=cuentaEjercicios
]{ejer}
\newcounter{definicion}
\declaretheoremstyle[%
spaceabove=6pt,%
spacebelow=6pt,%
headfont=\normalfont\bf,%
notefont=\normalfont\bf,
notebraces={{}{}},
bodyfont=\itshape,
headpunct={.}]{estiloDeDefinicion}
\declaretheorem[
name={Definici\'on},
numberlike=definicion,
style=estiloDeDefinicion]{defi}
\let\c@hapter\chapter
\renewcommand{\chapter}{
\ifnum\value{chapter}>0
\cambiarseccionydefi
\let\chapter\c@hapter
\fi
\c@hapter
}
\def\cambiarseccionydefi{%
\renewcommand\section{\@startsection{section}{1}{0mm}{6pt}{1pt}{\Large}}%
\@addtoreset{definicion}{section}%
\renewcommand{\thedefi}{\thesection.\arabic{definicion}}%
}
\newcommand{\seccionDeEjercicios}{%
\newpage%
\kern 5mm
\hrule
\kern 3mm
\null \hfill {\large Ejercicios~\thesection.~\rightmark}\par
\kern 3mm
\hrule
\kern 5mm
}
\newcommand{\Z}{\ensuremath{\mathbb{Z}}}
\newcommand{\R}{\ensuremath{\mathbb{R}}}
\newcommand{\N}{\ensuremath{\mathbb{N}}}
\newcommand{\Q}{\ensuremath{\mathbb{Q}}}
\newcommand{\nin}{\not \in}
\newcommand{\ds}[1]{\displaystyle{#1}}
\fancypagestyle{respuestas}{%
\fancyhf{}%
\fancyhead[LO]{\small\slshape Soluciones a los ejercicios}%
\fancyhead[RE]{\small\slshape Soluciones a los ejercicios}%
\fancyhead[LE,RO]{\thepage}}
\AtBeginDocument{
\addtocontents{toc}{\protect\markboth{\small\textsl Contenidos}{\small\textsl
Contenidos}}
\pagestyle{fancy}
\fancyhf{}
\renewcommand\headrulewidth{0pt}
\renewcommand{\chaptermark}[1]{\markboth{#1}{}}
\renewcommand{\sectionmark}[1]{\markright{#1}{}}
\fancyhead[LO]{\small\textsl{\leftmark}}
\fancyhead[RE]{\small\textsl{\rightmark}}
\fancyhead[LE,RO]{\thepage}
\Opensolutionfile{solucionario}
\Newassociation{respuesta}{Res}{solucionario}
\renewcommand{\Res}[1]{\textbf{Ejercicio #1.}}
\Writetofile{solucionario}{\protect\chapter*{Respuestas}}
\Writetofile{solucionario}{\protect\pagestyle{respuestas}}
}
Best Answer
A possible solution using the
titlesec
package to change the formatting for the sectional units and andexplicit
option to gain access to the title for the sectional units using#1
(refer to the package documentation); thefncychap
package is no longer needed.The modified file
libro.cls
(the code contains some comments):The test file, with the new code using
titlesec
:An image of the "Respuestas" ("Answers") section showing the desired result: