4

I have the following source code that generates random questions regarding conversions from Arabic to Roman Numerals. I would like the solutions to be printed on a new page after the exercises end. I have tried inserting the \printsolutions commmand inside and outside of the loop. Outside of the loop it prints the solution to the last exercise repeatedly. I'm guessing this is because this is the last number stored in memory after the loop ends. Here is my code:

\documentclass[12pt,leqno]{article}
\usepackage{amsmath, amsfonts, amssymb,amsthm}
\usepackage{forloop}
\usepackage[first=1, last=100]{lcg}
\newcommand{\random}{\rand\arabic{rand}}
\usepackage[auto-label=true]{exsheets}



\title{\textbf{Converting Arabic to Roman Numerals}}
\begin{document}
\maketitle

\newcounter{s}
\forloop[1]{s}{1}{\value{s}< 10}
{
\begin{question}
    Convert \random \ to a Roman Numeral
\end{question}
\begin{solution}
\MakeUppercase{\textbf{\roman{rand}}
}
\end{solution}
}
\printsolutions

\end{document}

2 Answers 2

2
+50

Here is a solution. Save the answer in a macro using the id of each question.

\documentclass[12pt,leqno]{article}
\usepackage{amsmath, amsfonts, amssymb,amsthm}
\usepackage{forloop}
\usepackage[first=1, last=100]{lcg}
\newcommand{\random}{\rand\arabic{rand}}
\usepackage[auto-label=true]{exsheets}


\title{\textbf{Converting Arabic to Roman Numerals}}
\begin{document}
\maketitle

\newcounter{s}
\forloop[1]{s}{1}{\value{s}< 10}
{
\begin{question}
    Convert \random\ to a Roman Numeral
    \expandafter\xdef\csname answer\CurrentQuestionID\endcsname{%
    \roman{rand}}
\end{question}
\begin{solution}
\MakeUppercase{\textbf{\csname answer\CurrentQuestionID\endcsname}}
\end{solution}
}

\newpage
\printsolutions

\end{document}
3

It's an expansion problem; with your code, just \roman{rand} is stored, not its current expansion.

\documentclass[twocolumn]{article}
\usepackage{amsmath, amsfonts, amssymb,amsthm}
\usepackage[first=1, last=100]{lcg}
\newcommand{\random}{\rand\arabic{rand}}
\usepackage[auto-label=true]{exsheets}

\usepackage{xparse}

\ExplSyntaxOn
\NewDocumentCommand{\replicate}{mm}% I prefer this to \forloop
 {
  \prg_replicate:nn { #1 } { #2 }
 }
\ExplSyntaxOff

\begin{document}

\title{Converting Arabic to Roman Numerals}
\author{1028}

\maketitle

\replicate{10}{%
  \begin{question}
    Convert \random \ to a Roman Numeral
  \end{question}
  \begingroup\edef\x{\endgroup
    \noexpand\begin{solution}
    \noexpand\textbf{\Roman{rand}}
    \noexpand\end{solution}
  }\x
}

\newpage
\printsolutions

\end{document}

I used twocolumn just for reducing the size of the output.

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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