1

Consider the following MWE:

\documentclass[12pt]{book}
\usepackage{exsheets}
\SetupExSheets[solution]{print=true}

\begin{document} \begin{question} This is a question

\end{question}

\begin{solution}
This is an answer       
\end{solution}  

\end{document}

I'm trying to beside the solution environment, I have a hint for each question such that those hint printed according to 'chapter number.question number' in the chapter Hints. How it could possible to define such a command or environment? Consider the following MWE:

\documentclass[twoside,a4paper,cleardoublepage=empty,14pt]{book}
\usepackage{environ}
\usepackage{exsheets}
\SetupExSheets[question]{type=exam}
\SetupExSheets{counter-format=ch.qu[1]}
\RenewQuSolPair{question}[headings-format=\large\bfseries,name=Problem]{solution}
\usepackage{environ}

\makeatletter \NewEnviron{hint}{% \def@currentlabel{\BODY}\label{hint:\thequestion}%

}

\usepackage{pgffor}

\newcommand\printmyhints{%

\foreach \x in {1,...,\thequestion}{% 

    \noindent
    \thechapter.\x.~\ref{hint:\x}\par
}

}

\makeatother \begin{document}

\chapter{Analysis of Algorithms}

\begin{question}
    Which of the following answers is correct for the given recurrence relation?

\end{question}
\begin{hint}
    a hint is here!
\end{hint}
\begin{solution}
    this is a solution
\end{solution}
\chapter{answer Sheet}

    \printmyhints


\end{document}

The problem of above code is hints must has counter 1.1,1.2 but those get started from chapter 2.

tstt
  • 187
  • You can check this: https://tex.stackexchange.com/questions/673420/collecting-hints-to-exercises-with-xsim – Krishna Feb 04 '24 at 02:16

1 Answers1

2

The trick is to store the chapter and question number with the hint and to use a separate counter for the hints (labels).

I also gratuitously replaced \foreach with a TeX \loop. Just remember to loop over the hint counter.

BTW, this qualifies as the aux file approach.

\documentclass[twoside,a4paper,cleardoublepage=empty,14pt]{book}
\usepackage{environ}
\usepackage{exsheets}
\SetupExSheets[question]{type=exam}
\SetupExSheets{counter-format=ch.qu[1]}
\RenewQuSolPair{question}[headings-format=\large\bfseries,name=Problem]{solution}
\usepackage{environ}

\newcounter{hint}

\makeatletter \NewEnviron{hint}{\refstepcounter{hint}% \edef@currentlabel{\thechapter.\thequestion~\BODY}% \label{hint:\thehint}% } \newcommand\printmyhints{% you could use \foreach, but this is faster \bgroup% use local registers \count1=\value{hint}% a macro would also work \setcounter{hint}{0}% \loop\ifnum\value{hint}<\count1 \stepcounter{hint}% \noindent\ref{hint:\thehint}\par \repeat \egroup}

\makeatother \begin{document}

\chapter{Analysis of Algorithms}

\begin{question}
    Which of the following answers is correct for the given recurrence relation?

\end{question}
\begin{hint}
    a hint is here!
\end{hint}
\begin{solution}
    this is a solution
\end{solution}
\chapter{answer Sheet}

    \printmyhints


\end{document}


This shows a savebox approach. In order to allow multiline text, it uses \vbox instead of \hbox style saveboxes. udbox is a \vbox version of lrbox.

\documentclass[twoside,a4paper,cleardoublepage=empty,14pt]{book}
\usepackage{environ}
\usepackage{exsheets}
\SetupExSheets[question]{type=exam}
\SetupExSheets{counter-format=ch.qu[1]}
\RenewQuSolPair{question}[headings-format=\large\bfseries,name=Problem]{solution}
%\usepackage{showframe}

\makeatletter \def\udbox#1{% fragile \edef\reserved@a{% \endgroup \setbox#1\vbox{% \begingroup\aftergroup}% \def\noexpand@currenvir{@currenvir}% \def\noexpand@currenvline{\on@line}}% \reserved@a @endpefalse \color@setgroup \ignorespaces} \def\endudbox{\unskip\color@endgroup} \makeatother

\newsavebox{\hintbox}

\newenvironment{hint}{\begin{udbox}{\hintbox}% \unvbox\hintbox \noindent\thechapter.\thequestion\space}% {\end{udbox}% \global\setbox\hintbox=\copy\hintbox}

\newcommand\printmyhints{\usebox\hintbox}

\begin{document}

\chapter{Analysis of Algorithms}

\begin{question}
    Which of the following answers is correct for the given recurrence relation?

\end{question}
\begin{hint}
  Consider the following:
  %\hrule
  \begin{itemize}
    \item test
  \end{itemize}
\end{hint}
\begin{solution}
    this is a solution
\end{solution}
\chapter{answer Sheet}

    \printmyhints

\end{document}

John Kormylo
  • 79,712
  • 3
  • 50
  • 120
  • Could you look at this? https://tex.stackexchange.com/questions/700511/generating-bubble-answer-sheet – tstt Nov 06 '23 at 16:50
  • If I put for example some \item in the Hint environment, it doesn't work. How is it possible to resolve this? – tstt Nov 07 '23 at 17:37
  • Occasionally , I must write an itemize environment in hint environment but I get error. How I can resolve this? Thanks – tstt Nov 08 '23 at 06:30
  • I use environment solution for other purpose, so I shouldn't use that to print bubble in that. From this argument I decide to introduce environment correctchoice, Could you according to this observation modify correctchoice to accept \buuble command? Thank you – tstt Nov 08 '23 at 09:30
  • The problem seems to occur in the \edef stage. \protected@edef doesn't help. This may be inherent in the environ \BODY approach. – John Kormylo Nov 08 '23 at 12:38