如何将问题和解决方案结合在一起

如何将问题和解决方案结合在一起

我有以下情况。

\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}

我如何将每个部分与它们各自的解决方案配对,例如“问题 1 和问题 1 的解决方案”,这样如果我想移动(向上或向下)或删除 \parts,解决方案也会移动或删除。

就像这样

\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}

答案1

我想到可以使用保存框来收集解决方案并保留格式。但是这种格式不适合单独列出。

\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}

我的下一次尝试试图模拟问题的格式。

\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}

此版本会自动在每一层末尾打印解决方案。我只实现了问题和部分,但扩展它很简单。

\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}

此版本完整重现了问题标签。 的目的是在 前面\prelabel添加。3.(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}

相关内容