Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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}
$ 
share|improve this question
    
Welcome to TeX.sx! –  Kurt Feb 6 '13 at 16:32
add comment

1 Answer

Thanks to the answers of Joseph Wright to question When to use \edef, \noexpand, and \expandafter? and of David Carlisle to passing current content of commands to a macro I eventually came up with a fully satisfactory (for me) code:

          \documentclass{article}
          \usepackage{amsthm} % used for `exercise' and `solution' environments
          \usepackage{lipsum}

          %*******************************************************************
          %  set of macros for exercises                                     *
          %  (seek for ^^ to find end)                                       *
          %vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv


          \makeatother\theoremstyle{remark}
          \newtheorem*{SolInternal}{}
          \newtheorem{ExInternal}{Exercise}[section]

          \makeatletter
          \let\@solutions\@empty%
          \let\@exercises\@empty% 

          \newcommand\exercise[3][]{% 
            % use:
            % \exercise['optional label']{'text of exercise'}{'text os solution'}
            % to be placed everywhere in the text, does not produce output
            % 
            \g@addto@macro\@exercises{%
              \begin{ExInternal}[#1]%
                \label{exercise:\theExInternal}%
                #2%
              \end{ExInternal}%
              \wlog{Created exercise \theExInternal}%
              \edef\ExerciseLabel{[exercise:\theExInternal]}%    
              \expandafter\addsolution\ExerciseLabel{#3}%    
            }%
          }


          \newcommand\exerciseshere{%
            %
            % places all the exercises created after the last call of \exerciseshere
            %
              \subsection*{Exercises}% change this to whatever environment you like
              \@exercises%
              \global\let\@exercises\@empty%
          }

          \newcommand\solutionshere{%
          %
          % places all the solutions of the exercises from last call of \solutions
          %
              \section{Solutions of the Exercises}% change this to whatever environment you like
              \@solutions%
              \global\let\@solutions\@empty% uncomment this 
          }

          \def\addsolution[#1]#2{%
          %
          % helper macro automatically called by \exercise
          %
              \g@addto@macro\@solutions{% 
                \begin{SolInternal}[{\bfseries \ref{#1}}, Page \pageref{#1}]%
                  \wlog{Added solution to #1}
                  #2%
                \end{SolInternal}%
              }%
          }


          \makeatother

          %^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
          %  end of macros for exercises                                     *
          %  (seek for vv to find beginning)                                 *
          %*******************************************************************

          \begin{document}
          \section{First Section}
          \lipsum[1]
          \exercise{compute 1+1.}{The answer is 2.}
          \lipsum[2]
          \exercise{\label{base3}compute 2+2.}{The answer is 4.}
          \exerciseshere
          \section{Second Section}
          \exercise{{In which base the solution of exercise \ref{base3} is 11?}{In base 3.}
          \lipsum[3]
          \exerciseshere
          \appendix
          \solutionshere
          \end{document}

Note that it is possible to label the exercises, since it is not forbidden for an environment to have more than one label. The result is:

pdf output of above code

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.