0

I have the following scenario.

\documentclass[12pt,a4paper,addpoints]{exam}
\begin{document}
begin{flushleft}
\begin{questions}
\begin{parts}

\part[1] %Question 1
Write an algebraic expression for " a number plus 5".\lines{1}

\part[1] %Question 2
Write an algebraic expression for "a number minus 3".\lines{1}

Solutions

\textbf{[1]} $x+5$  % Solution to Question 1

\textbf{[2]} $x-3$  % Solution to Question 2

\end{parts}
\end{questions}
\end{flushleft}
\end{document}

How can I pair up each parts to their respective solution, eg 'Question 1 and Solution to Question 1' such that if I want to move (up or down) or delete \parts, so too will the solution move or delete.

So something like this

\documentclass[12pt,a4paper,addpoints]{exam}
\begin{document}
begin{flushleft}
\begin{questions}
\begin{parts}

\part[1] %Question 2        % I have just moved Question 2 above Quesiton 1
Write an algebraic expression for "a number minus 3".\lines{1}

\part[1] %Question 1
Write an algebraic expression for " a number plus 5".\lines{1}



Solutions

\textbf{[1]} $x-3$  % Solution to Question 2 <----------------- Solution to Question 2 has moved above it.

\textbf{[2]} $x+5$  % Solution to Question 1



\end{parts}
\end{questions}
\end{flushleft}
\end{document}
6
  • usually solutions (using \begin{solution} ... \end{solution}) are entered with the questions (share counter) and are usually hidden ([answers] or \printanswers). See pages 54 through 80 of the manual. Commented Apr 27, 2020 at 15:24
  • @John Kormylo But it doesn't do what I want. I want the questions and solutions on two separate pages. Once I remove the question, i want the solution to be removed too.
    – Alan Jones
    Commented Apr 27, 2020 at 20:03
  • Sorry. I thought that would be a standard option, but evidently not (I don' use it myself). You might use the endnotes package. Commented Apr 27, 2020 at 23:34
  • All good, I am trying to find a solution where I can pair two pieces of information. I think the post below is on the right track but I don't understand the logic behind it.
    – Alan Jones
    Commented Apr 28, 2020 at 8:29
  • It is loosely based on how footnotes are created. Commented Apr 28, 2020 at 13:51

1 Answer 1

1

It occurred to me that one could use a savebox to collect solutions and preserve the formatting. However the formatting was not appropriate for a separate list.

\documentclass[12pt,a4paper,addpoints,answers]{exam}
\usepackage{showframe}% MWE only
\usepackage{blindtext}% MWE only

\newsavebox{\mybox}

\newcommand{\mysolution}[1]{\global\setbox\mybox=\vbox{\unvbox\mybox\medskip
  \begin{solution}#1\end{solution}}}


\begin{document}
\begin{questions}
\question
Write an algebraic expression for "a number minus 3".
\mysolution{$x-3$}

\question
Write an algebraic expression for " a number plus 5".
\mysolution{\blindtext}
\end{questions}

\begin{flushleft}
Solutions

\unvbox\mybox% box is now empty
\end{flushleft}

\end{document}

My next attempt tried to emulate the question formatting.

\documentclass[12pt,a4paper,addpoints,answers]{exam}
\usepackage{showframe}% MWE only
\usepackage{blindtext}% MWE only

\newsavebox{\mybox}

\newcommand{\mysolution}[1]{\global\setbox\mybox=\vbox{\unvbox\mybox\medskip
  \makebox[\labelwidth][r]{\questionlabel}\hskip\labelsep
  \parbox[t]{\linewidth}{#1}\par}}


\begin{document}
\begin{questions}
\question
Write an algebraic expression for "a number minus 3".
\mysolution{$x-3$}

\question
Write an algebraic expression for " a number plus 5".
\mysolution{\blindtext}
\end{questions}

\begin{flushleft}
Solutions

\unvbox\mybox% box is now empty
\end{flushleft}

\end{document}

This version automatically prints the solution at the end of each layer. I only implemented questions and parts, but extending it is straightforward.

\documentclass[12pt,a4paper,addpoints,answers]{exam}
\usepackage{xpatch}
\usepackage{showframe}% MWE only
\usepackage{blindtext}% MWE only

\newsavebox{\mybox}

\makeatletter
\newcommand{\mysolution}[1]{\setbox\mybox=\vbox{\unvbox\mybox\medskip
  \leftskip=\@totalleftmargin
  \noindent\llap{\makebox[\labelwidth][r]{\mylabel}\hskip\labelsep}%
  #1\par}}

\newcommand{\printmybox}{\ifprintanswers
  \ifvoid\mybox
  \else
    \uplevel{\textbf{Solutions}}\par
    \unvbox\mybox
  \fi
\fi}

\newcommand{\emptybox}{\setbox\mybox=\copy\voidb@x}
\makeatother

% modify environments
\xapptocmd{\questions}{\emptybox
  \let\mylabel=\questionlabel}{}{appto questions failed}
\xpretocmd{\endquestions}{\printmybox}{}{preto endquestions failed}

\xapptocmd{\parts}{\emptybox
  \let\mylabel=\partlabel}{}{appto parts failed}
\xpretocmd{\endparts}{\printmybox}{}{preto endparts failed}

\begin{document}
\begin{questions}
\question
Write an algebraic expression for "a number minus 3".
\mysolution{$x-3$}

\question
Write an algebraic expression for " a number plus 5".
\mysolution{\blindtext}

\question
\begin{parts}
\part
Write an algebraic expression for "a number minus 3".
\mysolution{$x-3$}

\part
Write an algebraic expression for " a number plus 5".
\mysolution{\blindtext}
\end{parts}
\end{questions}
\end{document}

This version reproduces the question labels in their entirety. The purpose of the \prelabel was to add 3. in front of (a).

\documentclass[12pt,a4paper,addpoints,answers]{exam}
\usepackage{xpatch}
\usepackage{showframe}% MWE only
\usepackage{blindtext}% MWE only

\newsavebox{\mybox}
\newsavebox{\prelabel}% previous label

\makeatletter
\newcommand{\mysolution}[1]{\global\setbox\mybox=\vbox{\unvbox\mybox\medskip
  \leftskip=\@totalleftmargin
  \noindent\llap{\box\prelabel\makebox[\labelwidth][r]{\mylabel}\hskip\labelsep}%
  #1\par}}
\makeatother

\newcommand{\printmybox}{\ifprintanswers
  \ifvoid\mybox
  \else
    \fullwidth{\textbf{Solutions}}\par
    \unvbox\mybox
  \fi
\fi}

\newcommand{\newprelabel}{\setbox\prelabel=\hbox{% store current level label
  \box\prelabel\makebox[\labelwidth][r]{\mylabel}\hskip\labelsep}}

% modify environments
\xapptocmd{\questions}{\let\mylabel=\questionlabel}{}{appto questions failed}

\xpretocmd{\parts}{\newprelabel}{}{preto parts failed}
\xapptocmd{\parts}{\let\mylabel=\partlabel}{}{appto parts failed}

\begin{document}
\begin{questions}
\question
Write an algebraic expression for ``a number minus 3''.
\mysolution{$x-3$}

\question
Write a paragraph in Latin.
\mysolution{\blindtext}

\question
\begin{parts}
\part
Write an algebraic expression for ``a number plus 5''.
\mysolution{$x+5$}

\part
Write a paragraph in Latin.
\mysolution{\blindtext}
\end{parts}

\printmybox
\end{questions}
\end{document}
12
  • Is it doable under exam class?
    – Alan Jones
    Commented Apr 28, 2020 at 7:54
  • @AlanJones At first glimpse I thought about a mechanism like the table of contents, i.e., writing an external text file containing entries of pattern \entry{<number of question>}{<number of part>}{<number of subpart>}{<number of subsubpart>}{Text that forms the answer} and a command which evaluates these entries within another questions-environment and hereby by means of the numbers "decides" whether to insert/end another parts/subparts/subsuparts-environment and whether solutions to some answers were omitted. But it seems you can have only one questions-environment per document. Commented Apr 28, 2020 at 8:54
  • @AlanJones So I think it is doable under the exam-class, but will be a lot of work as patching some of the things of the exam-class is required while the documentation of the implementation of the exam-class in my opinion is a nightmare. Commented Apr 28, 2020 at 8:56
  • Hmmm, maybe I have to look at other packages that can pair up two blocks of information.
    – Alan Jones
    Commented Apr 28, 2020 at 9:16
  • @AlanJones Besides this in the exam-class the naming of internal data-structures which only exist during the LaTeX-run/during compilation, e.g., the naming of internal referencing labels, is not strictly separated from the printing of numbering in the resulting output-file. This can cause problems like weird messages about multiply defined labels when having more than one question-environment. Commented Apr 28, 2020 at 9:17

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .