Skip to content

Commit f9f5f9b

Browse files
committed
deferring problems to end (of chapter) now easier
1 parent 390f8b4 commit f9f5f9b

1 file changed

Lines changed: 70 additions & 21 deletions

File tree

sty/open-logic-defer.sty

Lines changed: 70 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,45 +11,94 @@
1111
\RequirePackage{etoolbox}
1212
\RequirePackage{answers}
1313

14+
% write problems to \oljobname.prb
15+
1416
\def\solutionextension{prb}
1517

1618
\makeatletter
1719

18-
% save definition of prob environment as probd
19-
\let\probd\prob
20-
\let\endprobd\endprob
21-
\let\c@probd\c@prob
22-
\makeatother
20+
% Undefine the prob and probtag environments
2321

2422
\csundef{prob}
2523
\csundef{endprob}
2624
\csundef{probtag}
2725
\csundef{endprobtag}
2826

27+
% Instead, make them into environments which write probdeferred and
28+
% probdeferredtag environments to the problem file using answers.sty
29+
2930
\Newassociation{prob}{probdeferred}{\jobname}
3031
\Newassociation{probtag}{probdeferredtag}{\jobname}
3132

32-
% Redefine probdeferred as a theorem environment
33-
%\csundef{probdeferred}
34-
%\csundef{endprobdeferred}
35-
%\csundef{c@probdeferred}
33+
% probdeferred and probdeferredtag are just wrappers for theorem
34+
% environments which do the actual typesetting. answers.sty writes
35+
% the number of the problem into the .prb file, but we throw them away
36+
% and just count them separately. It also throws away the tags. This
37+
% is a bug; actually probtag should deal with the tags but this
38+
% requires hacking answers.sty code.
3639

3740
\renewenvironment{probdeferred}[1]{\begin{probd}}{\end{probd}}
3841
\renewenvironment{probdeferredtag}[2]{\begin{probd}}{\end{probd}}
3942

40-
%\declaretheorem[
41-
% style=definition,
42-
% name=Problem,
43-
% sibling=thm]{probdeferred}
44-
% answers writes current label as argument
45-
%\apptocmd{\probdeferred}{\@gobble}{}{}
43+
% the probd environment does the actual typesetting.
4644

47-
% at the end of a chapter, read the solutionfile
48-
\appto\OLEndChapterHook{%
49-
\Closesolutionfile{\jobname}
50-
\Readsolutionfile{\jobname}}
45+
\declaretheorem[
46+
style=definition, numbered=yes,
47+
name={Problem}]{probd}
48+
49+
% Print the chapter number with the problem number
5150

52-
% at the end of a \chapter command, open the solution file
51+
\def\theprobd{\probch.\arabic{probd}}
52+
53+
% make sure the .prb file is open
54+
\Opensolutionfile{\jobname}
55+
56+
% - `\problemsperchapter`: execute this in the preamble to print the
57+
% - problem file at the end of every chapter
58+
59+
\newcommand{\problemsperchapter}{%
60+
% after each chapter command, open the .prb file and write a section
61+
% header into it
5362
\renewcommand\memendofchapterhook{%
63+
\Closesolutionfile{\jobname}
5464
\Opensolutionfile{\jobname}
55-
\Writetofile{\jobname}{\protect\section*{Problems}}}
65+
\Writetofile{\jobname}{
66+
\protect\section*{Problems}
67+
\protect\def\protect\probch{\thechapter}
68+
\protect\setcounter{probd}{0}}}
69+
% at the end of every chapter, close the .prb file and print it
70+
\appto\OLEndChapterHook{%
71+
\Closesolutionfile{\jobname}
72+
\def\probch{\thechapter}
73+
\Readsolutionfile{\jobname}}}
74+
75+
% - `\collectproblems`: execute this in the preamble to arrange for
76+
% the printing of problems explicitly, e.g., at the end of the book
77+
78+
\newcommand{\collectproblems}{%
79+
% at the end of every \chapter command, write a section header into
80+
% the .prb file and reset counters
81+
\renewcommand\memendofchapterhook{%
82+
\Writetofile{\jobname}{
83+
\protect\section*{Problems for Chapter~\thechapter}
84+
\protect\def\protect\probch{\thechapter}
85+
\protect\setcounter{probd}{0}}}}
86+
87+
% \collectproblems is the default, so we execute it right away
88+
89+
\collectproblems
90+
91+
% - `\stopproblems`: execute at the end of the mainmatter, to stop
92+
% chapter commands from writing to the .prb file
93+
94+
\newcommand{\stopproblems}{%
95+
\renewcommand\memendofchapterhook{}}
96+
97+
% - `\printproblems`: closes problem file and prints problems.
98+
99+
\newcommand{\printproblems}{%
100+
\Closesolutionfile{\jobname}
101+
\Readsolutionfile{\jobname}}
102+
103+
\makeatother
104+

0 commit comments

Comments
 (0)