I found a partial answer to my problem so I update the question, and leave the unmodified original post here below for reference.
The only problem left is how to pass the actual value of a command to a macro at the moment of creation of the macro, instead that having it evaluated when the macro is called. Made lots of attemptes with \edef
,\expandafter
... no way!
This question is a follow-up of Exercises in lecture notes without predetermined placement
There, the problem was to write exercises anywhere in the text (which is very useful fo the author if part of the logic of an argument is deferred to exercises), and decide afterwards where to place them (e.g. at the end of the section).
The answer provided there is really good, simple and effective. It is almost what I need, since in addition I would like to write also the answers, and have them all published e.g. at the end of the book. For this reason, I tried to modify the code in this way:
\documentclass{book}
\usepackage{amsthm}
\theoremstyle{remark}
\newtheorem*{SolInternal}{}
\newtheorem{ExInternal}{Exercise}[section]
\makeatletter
\let\@solutions\@empty%
\let\@exercises\@empty%
\newcommand\exercise[3][]{%
\g@addto@macro\@exercises{%
\begin{ExInternal}[#1]%
\label{exercise:\theExInternal}%
#2%
\end{ExInternal}%
}%
\global\edef\ExerciseLabel{\theExInternal}%
\g@addto@macro\@solutions{%
\begin{SolInternal}[\ExerciseLabel]%
#3%
\end{SolInternal}%
}%
}
\newcommand\exerciseshere{%
\subsection*{Exercises}
\@exercises%
\global\let\@exercises\@empty%
}
\newcommand\solutionshere{%
\chapter*{Solutions of the Exercises}
\@solutions%
\global\let\@solutions\@empty%
}
\makeatother
\begin{document}
\chapter{This is a chapter}
\section{This is a section}
You mat try exercise \ref{myExercise}
\exercise{\label{myExercise}Show that \(2+2=4\).}{\(2+2=1+1+1+1=4\).}
\exercise{Show that \(4+4=8\)}{Apply exercise \ref{myExercise}}
And there is more text around.
\exerciseshere % creates the exercises of the section
\section{This is another section}
Here we have fun.
\exercise{Prove that \(1+1=10\).}{Use the binary system.}
\exerciseshere
\solutionshere % creates the solutions of all the book
\end{document}
This works fine, it creates automatically unique labels for the exercises; but I cannot find a way for passing them to the corresponding solutions. All solutions get the same label, that of the last exercise, because they are evaluated at the moment, the macro is called. I tried with edef, but apparently it does not what I would like...
Original Post:
There, the problem was to write exercises anywhere in the text (which is very useful fo the author if part of the logic of an argument is deferred to exercises), and decide afterwards where to place them (e.g. at the end of the section).
The answer provided there is really good, simple and effective. It is almost what I need, since in addition I would like to write also the answers, and have them all published e.g. at the end of the book. For this reason, I tried to modify the code and merge it with the package answers
. More precisely, I added a \begin{sol}#3\end{sol}
in the definition of the macro, and raised to 3 the number of arguments. It did not work, it resulted in a Runaway Error ! File ended while scanning use of \next
. The temporary file is created, but is messed up. Any solution?
The code I tried:
\documentclass{article}
\usepackage{amsthm,answers}
\Newassociation{sol}{Solution}{ans}
\theoremstyle{remark}
\newtheorem{ExInternal}{Exercise}[section]
\makeatletter
\let\@exercises\@empty%
\newcommand\exercise[3][]{%
\g@addto@macro\@exercises{%
\begin{ExInternal}[#1]%
#2%
\begin{sol}%
#3%
\end{sol}%
\end{ExInternal}%
}%
}
\newcommand\exerciseshere{%
\subsection*{Exercises}
\@exercises%
\global\let\@exercises\@empty%
}
\makeatother
\begin{document}
\Opensolutionfile{ans}[ans1]
\section{Prime Numbers}
A \emph{prime number} is a positive integer other than $1$ that is only divisible by $1$ and itself.
\exercise[Euclid's Theorem]{\label{ex:euclid}Show that there are infinitely many prime numbers.}{\(n!+1\) is prime for every \(n\).}
\exerciseshere
\Closesolutionfile{ans}
\section{Solutions}
\input{ans1}
\end{document}
And here is the temporary file I got:
$ cat ans1.tex
\begin{Solution}{1.1}
n!+1\) is prime for every \(n\).\end {sol}\end {ExInternal}\global \let \@exercises \@empty
\Closesolutionfile{ans}
\section{Solutions}
\input{ans1}
\end{document}
$