A possible solution using the titlesec
package to change the formatting for the sectional units and and explicit
option to gain access to the title for the sectional units using #1
(refer to the package documentation); the fncychap
package is no longer needed.
The modified file libro.cls
(the code contains some comments):
\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}
\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}
\RequirePackage[explicit]{titlesec}
% to store the chapter name
\newcommand\mychaptername{}
% to store the section name
\newcommand\mysectionname{}
% chapter formatting
\titleformat{\chapter}[display]
{}{}{0pt}
{\titlerule[0.5pt]\vspace{2ex}%
\filleft{\LARGE\bfseries\chaptertitlename\ \thechapter.\space%
\normalfont\LARGE#1}\gdef\mychaptername{#1}}
[{\vspace{1ex}\titlerule[0.5pt]}]
\titleformat{name=\chapter,numberless}[display]
{}{}{0pt}
{\filleft\normalfont\LARGE#1}
[{\vspace{1ex}\titlerule[0.5pt]}]
\titlespacing*{\chapter}
{0pt}{20pt}{40pt}
% change section formatting in chapters 2,3,...
\let\leo@chapter\chapter
\renewcommand{\chapter}{
\ifnum\value{chapter}>0
\changesectionanddefi
\let\chapter\leo@chapter
\fi
\leo@chapter
}
% format for sections in chapter 1
\titleformat{\section}
{\normalfont\bfseries}{\thesection}{0.5em}
{#1\gdef\mysectionname{#1}}%
% format for sections in chapters 2,3,...
\newcommand\changesectionanddefi{%
\titleformat{\section}
{\normalfont\Large}{\thesection}{0.5em}
{##1\gdef\mysectionname{##1}%
\@addtoreset{definicion}{section}%
\renewcommand{\thedefi}{\thesection.\arabic{definicion}}%
}%
}
\titlespacing*{\section}
{0pt}{3.5ex plus 1ex minus .2ex}{2.3ex plus .2ex}
\newcommand{\seccionDeEjercicios}{%
\newpage%
\kern 5mm
\hrule
\kern 3mm
\null \hfill {\large Ejercicios~\thesection.\space\mysectionname}\par
\kern 3mm
\hrule
\kern 5mm
\ifnum\value{section}=1 \relax
\Writetofile{solucionario}{\protect\paragraph*{\chaptername\space\thechapter.\space\mychaptername}}
\else\fi
\Writetofile{solucionario}{\protect\paragraph*{Ejercicios\space\thesection.
\space\mysectionname}}
}
\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}}
}
The test file, with the new code using titlesec
:
\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
\section{Propiedades de campo}
\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}
\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}
\section{Operaciones con otros objetos}
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}
\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}
\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}
\end{ejer}
\Closesolutionfile{solucionario}
\input{solucionario}
\end{document}
An image of the "Respuestas" ("Answers") section showing the desired result:
With the probsoln
package, problems can be defined either within the document or in an external file that can be loaded using a command such as \loadallproblems
or \loadrandomproblems
.
Here's a simple example with the problems defined in the document:
\documentclass{article}
\usepackage{probsoln}
\begin{defproblem}{prob1}% label
\begin{onlyproblem}% question
Calculate the following:%
\begin{textenum}
\item $7+2$
\item $9-9$
\item $5+5+5$
\end{textenum}
\end{onlyproblem}%
\begin{onlysolution}% solution
\begin{textenum}
\item $9$
\item $0$
\item $15$
\end{textenum}
\end{onlysolution}
\end{defproblem}
\begin{defproblem}{prob2}% label
\begin{onlyproblem}% question
Solve the following equations:
\begin{textenum}
\item $x+5=7$
\item $x-5=9$
\item $5x=20$
\end{textenum}
\end{onlyproblem}%
\begin{onlysolution}% solution
\begin{textenum}
\item $x=2$
\item $x=14$
\item $x=4$
\end{textenum}
\end{onlysolution}
\end{defproblem}
\begin{document}
\renewcommand{\labelenumii}{\theenumii)}
\begin{enumerate}
\foreachproblem{\item\thisproblem}
\end{enumerate}
\section*{Solutions}
\showanswers
\begin{enumerate}
\foreachsolution{\item\thisproblem}
\end{enumerate}
\end{document}
This produces
The formatting can be changed. For example, using the enumitem
package:
\documentclass{article}
\usepackage{probsoln}
\usepackage{enumitem}
\begin{defproblem}{prob1}% label
\begin{onlyproblem}% question
Calculate the following:%
\begin{textenum}
\item $7+2$
\item $9-9$
\item $5+5+5$
\end{textenum}
\end{onlyproblem}%
\begin{onlysolution}% solution
\begin{textenum}
\item $9$
\item $0$
\item $15$
\end{textenum}
\end{onlysolution}
\end{defproblem}
\begin{defproblem}{prob2}% label
\begin{onlyproblem}% question
Solve the following equations:
\begin{textenum}
\item $x+5=7$
\item $x-5=9$
\item $5x=20$
\end{textenum}
\end{onlyproblem}%
\begin{onlysolution}% solution
\begin{textenum}
\item $x=2$
\item $x=14$
\item $x=4$
\end{textenum}
\end{onlysolution}
\end{defproblem}
\newenvironment{ExerciseList}
{\begin{enumerate}[label={\emph{Exercise \arabic*}},%
ref={\arabic*},wide]
\renewcommand{\labelenumii}{\theenumii)}%
}
{\end{enumerate}}
\newcommand{\exercise}{\item\mbox{}\par}
\begin{document}
\begin{ExerciseList}
\foreachproblem{\exercise\thisproblem}
\end{ExerciseList}
\section*{Solutions}
\showanswers
\begin{ExerciseList}
\foreachsolution{\exercise\thisproblem}
\end{ExerciseList}
\end{document}
This produces:
Edit:
Here's an alternative where you can write the answer next to the question in the code:
\documentclass{article}
\usepackage{probsoln}
\usepackage{enumitem}
\newcommand{\QA}[2]{%
\begin{onlyproblem}#1\end{onlyproblem}%
\begin{onlysolution}#2\end{onlysolution}}
\begin{defproblem}{prob1}% label
\begin{onlyproblem}% question
Calculate the following:%
\end{onlyproblem}%
\begin{textenum}
\item \QA{$7+2$}{$9$}
\item \QA{$9-9$}{$0$}
\item \QA{$5+5+5$}{$15$}
\end{textenum}
\end{defproblem}
\begin{defproblem}{prob2}% label
\begin{onlyproblem}% question
Solve the following equations:
\end{onlyproblem}%
\begin{textenum}
\item \QA{$x+5=7$}{$x=2$}
\item \QA{$x-5=9$}{$x=14$}
\item \QA{$5x=20$}{$x=4$}
\end{textenum}
\end{defproblem}
\newenvironment{ExerciseList}
{\begin{enumerate}[label={\emph{Exercise \arabic*}},%
ref={\arabic*},wide]
\renewcommand{\labelenumii}{\theenumii)}%
}
{\end{enumerate}}
\newcommand{\exercise}{\item\mbox{}\par}
\begin{document}
\begin{ExerciseList}
\foreachproblem{\exercise\thisproblem}
\end{ExerciseList}
\section*{Solutions}
\showanswers
\begin{ExerciseList}
\foreachsolution{\exercise\thisproblem}
\end{ExerciseList}
\end{document}
The result is the same as the previous example.
Edit 2:
Here's a method that defines and displays the problem. The label is generated automatically:
\documentclass{article}
\usepackage{probsoln}
\usepackage{enumitem}
\newcommand{\QA}[2]{%
\begin{onlyproblem}#1\end{onlyproblem}%
\begin{onlysolution}#2\end{onlysolution}}
\newenvironment{ExerciseList}
{\begin{enumerate}[label={\emph{Exercise \arabic*}},%
ref={\arabic*},wide]
\renewcommand{\labelenumii}{\theenumii)}%
}
{\end{enumerate}}
\newcommand{\exercise}{\item\mbox{}\par}
\newcommand{\Exercise}[2]{\exercise
#1\par
\begin{defproblem}{prob\arabic{enumi}}%
\begin{textenum}%
#2%
\end{textenum}%
\end{defproblem}%
\useproblem{prob\arabic{enumi}}%
}
\begin{document}
\begin{ExerciseList}
\Exercise{Calculate the following:}%
{%
\item \QA{$7+2$}{$9$}
\item \QA{$9-9$}{$0$}
\item \QA{$5+5+5$}{$15$}
}%
\Exercise{Solve the following equations:}
{%
\item \QA{$x+5=7$}{$x=2$}
\item \QA{$x-5=9$}{$x=14$}
\item \QA{$5x=20$}{$x=4$}
}
\end{ExerciseList}
\section*{Solutions}
\showanswers
\begin{ExerciseList}
\foreachsolution{\exercise\thisproblem}
\end{ExerciseList}
\end{document}
Best Answer
I agree that the documentation of the
answers
package is a bit on the terse side. I don't find theexercise
documentation to be very difficult; it can do a lot of things, but is quite well documented, I think. I've never usedprobsoln
so I won't comment further on that.Since you ask about the
answers
package specifically, here is my attempt at explaining the package, and clarifying at least the simple example given in the documentation.First, the basic command
\Newassociation{xxx}{yyy}{zzz}
does the following things:{xxx}
to be used in your document for the solutions within the problems themselves. This environment will generally not be displayed unless the[nosolutionfiles]
option chosen when the package is loaded.{yyy}
which is the environment that will be used to display the solutions. These solutions are written to a separate file (or files) and then can be input into your document at a later place (e.g. at the end of a chapter, or at the end of the entire book.)zzz
. Either a single file is used (in which case it will bezzz.tex
) or multiple files are used, in which casezzz
acts as a kind of label that identifies the type of solution it is, and the file names are given explicitly.Because
xxx
,yyy
andzzz
are used as part of environment/macro names, they must only contain letters, since these are the only valid characters for multi-character macro names in TeX. As with other macro names, they are case-sensitive (Xxx
≠xxx
). Furthermore, you should make sure that they do not conflict with existing environments that you may have in your document.In the documentation, there are three sample documents (
ansexam{1|2|3}.tex
) which show how this works in practice. I've taken the simple example here (ansexam1.tex
), and annotated it with some comments. (On TeXLive distributions, these files can be found in/usr/local/texlive/2010/texmf-dist/doc/latex/answers/
).If you run this document, you will find that it has created a file
ans1.tex
which looks like the following:The
Solution
environment is set up by the\Newassociation
command. It has some basic formatting, which you can change as needed, either with hooks provided by the package, or by using\renewenvironment
after you have issued the\Newassociation
command.For example, there is a hook
\solutionstyle
which formats the label of each solution. This is initially set to\bfseries
, so the solution environment produces:1 First solution
2 Second solution
You could redefine it as follows:
And now your solutions would appear as:
Answer to Exercise 1 First solution
Answer to Exercise 2 Second solution
More complicated formatting is possible for any of the environments created using the
yyy
argument of the\Newassociation
command. There are two hooks,\preyyy
and\postyyy
(in this example, this would be\preSolution
and\postSolution
.) These must be created using\newcommand
by you, and will be prepended or appended to the output solution as needed. For example, if you wanted each solution to end with a bullet, you could use:Which would result in a right aligned bullet on its own line (ugly, but just an example.)
If you want your solutions to be more fancily formatted, you would use
\renewenvironment
to define the environment the way you like.