1

I'm writing documents which only contain exercises and I would like to add some hints for some exercises.

What I want is that the hints are specified in the exercise itself but displayed at the end of document.

I've tried to use the \atEndDocument hook but it did not worked.

Here a short example of what I want to write

\documentclass[14pt,a4paper]{extarticle}    
\usepackage{pgfpages}
\pgfpagesuselayout{2 on 1}[a4paper,landscape,border shrink=7mm] 

\begin{document}
Exercise 1 : 
Solve the following equation $x^2+1=2$
\hint{Exercise 1 : Don't forget that two solutions exist}

Exercise 2 :
Derivate the following function $f:x \to \cos(x^2)$
\hint{Exercise 2 : The result is not $-\sin(x^2)$ }
\end{document}
  • 1
    Welcome to TeX.SX! Please make your code compilable (if possible), or at least complete it with \documentclass{...}, the required \usepackage's, \begin{document}, and \end{document}. That may seem tedious to you, but think of the extra work it represents for TeX.SX users willing to give you a hand. Help them help you: remove that one hurdle between you and a solution to your problem. – dexteritas Sep 12 '17 at 08:41
  • if you want help with an error make your example demonstrate the error, and say exactly what error message you got. – David Carlisle Sep 12 '17 at 08:44
  • 1
    You could adapt the code in the answers to https://tex.stackexchange.com/questions/33229/how-to-place-all-proofs-automatically-in-appendix – egreg Sep 12 '17 at 09:16
  • It appears that the pgf packages prevent from the use of the "\atBeginDocument" – M. Miguel-Brebion Sep 12 '17 at 15:24
  • @M.Miguel-Brebion No, they do not (perhaps you just mistyped it (the command starts with A not a ) If you had shown the error message that you got some could have debugged it. – David Carlisle Sep 12 '17 at 16:35

2 Answers2

3

A simple working example with the \AtEndDocument macro:

\documentclass{article}

\AtEndDocument{\vfill}
\newcommand{\hint}[1]{\AtEndDocument{\par #1}}
\begin{document}
Exercise 1 : 
Solve the following equation $x^2+1=2$
\hint{Exercise 1 : Don't forget that two solutions exists}

Exercise 2 :
Derivate the following function $f:x \to \cos(x^2)$
\hint{Exercise 2 : The result is not simply $-\sin(x^2)$ }

\end{document}
3

Just for fun: This solution saves the hints as macros for later expansion.

\documentclass{article}

\newcounter{nhint}
\newcommand{\hint}[1]% #1 = paragraph
{\stepcounter{nhint}\expandafter\gdef\csname hint\thenhint\endcsname{#1}}% global, just in case

\begin{document}
Exercise 1 : 
Solve the following equation $x^2+1=2$
\hint{Exercise 1 : Don't forget that two solutions exists}%

Exercise 2 :
Derivate the following function $f:x \to \cos(x^2)$
\hint{Exercise 2 : The result is not simply $-\sin(x^2)$ }%

{\section{Hints}}% turn off \@afterheading

\edef\total{\arabic{nhint}}% or \countdef\total 0 \total=\value{nhint}%
\setcounter{nhint}{0}%
\loop\ifnum \value{nhint}<\total
  \stepcounter{nhint}%
  \csname hint\thenhint\endcsname
  \par
\repeat

\end{document}

This solution stores the hints in a savebox.

\documentclass{article}

\newsavebox{\hints}
\setbox\hints=\vbox{}

\newcommand{\hint}[1]% #1 = paragraph
{\global\setbox\hints=\vbox{\unvbox\hints
  \parindent=0pt% set up formatting here
  \strut#1\strut\par}%
}

\begin{document}
Exercise 1 : 
Solve the following equation $x^2+1=2$
\hint{Exercise 1 : Don't forget that two solutions exists}

Exercise 2 :
Derivate the following function $f:x \to \cos(x^2)$
\hint{Exercise 2 : The result is not simply $-\sin(x^2)$ }

\section{Hints}

\unvbox\hints

\end{document}
John Kormylo
  • 79,712
  • 3
  • 50
  • 120