[Tex/LaTex] Solutions in exam class

environmentsetoolboxexammath-mode

I'm trying to write up solutions to some exercises, using the exam class. In order to align equations correctly, I'm also using the aligned environment from amsmath.

To avoid too much repetitious typing, I've attempted to use etoolbox to start/end the aligned environment automatically within the solution – but on compiling, \LaTeX complains about a missing $.

The code below works, the commented out lines show what I'm trying to do. Any suggestions, either how to make this work, or a better method for achieving the same result?

\documentclass[answers]{exam}

\usepackage{xcolor} % Use colour!
\usepackage{titlesec} % Allow creation of new sectioning commands
\usepackage{amsmath} % Needed for splitting equations over multiple lines
\usepackage{ulem} % Underlining effects
\usepackage{etoolbox} % Tinkering with environments

\titleclass{\exercise}{straight}[\section]  % Create new 'section' command for exercises - this replaces \subsection
\newcounter{exercise}
\titleformat{\exercise}
  {\normalfont\large\bfseries}{}{1em}{Exercise \theexercise}
\titlespacing*{\exercise}{0pt}{3.25ex plus 1ex minus .2ex}{1.5ex plus .2ex}
\newcommand{\exerciseautorefname}{exercise}


\begin{document}

\unframedsolutions % Print solutions as plain text
\SolutionEmphasis{\color{blue}} % Print solutions in blue
\renewcommand{\thepartno}{\roman{partno}} % Format part numbers as lower-case roman numerals
\renewcommand\theexercise{\thesection\Alph{exercise}} % Print exercise numbers as 1A, etc...
\renewcommand{\solutiontitle}{\noindent} % Format solution - just print solution as entered
\newcommand{\ans}[1]{\uuline{#1}} % Double underline final answer

%\AtBeginEnvironment{solution}{$\begin{aligned}}
%\AtEndEnvironment{solution}{\end{aligned}$}

\section{Basic Algebra}
\exercise{}
\begin{questions}
    \question Simplify the following expressions by collecting like terms:

    \begin{parts}
        \part $8x + 3x + 4x - 6x$
        \begin{solution}
            $\begin{aligned}
                3p + 3 + 5p - 7 - 7p - 9 &= 3p + 5p - 7p + 3 - 7 - 9\\
                &= \ans{p - 13}\end{aligned}$
        \end{solution}

    \end{parts}

\end{questions}

\end{document}

Having done a bit more work on this, I find myself needing to use multiple alignment points in some places. The alignedat environment looks like what I need; I just can't get it to work in this context.

Code – main file

\documentclass[answers]{exam}

\addtolength{\textwidth}{1cm}
\addtolength{\hoffset}{-0.5cm}
\addtolength{\textheight}{1cm}
\addtolength{\voffset}{-0.5cm}

\usepackage{xcolor} % Use colour!
\usepackage{titlesec} % Allow creation of new sectioning commands
\usepackage{amsmath} % Needed for splitting equations over multiple lines
\usepackage{ulem} % Underlining effects
\usepackage{environ} % Tinkering with environments
\usepackage{gensymb} % Degree symbol
\usepackage[pdfstartview=FitH,bookmarks=true]{hyperref} 
\usepackage{bookmark} % PDF Bookmarks
\bookmarksetup{numbered} % Include numbers in PDF bookmarks

\titleformat{\section}{\normalfont\large\bfseries}{Chapter }{0em}{\thesection: }

\titleclass{\exercise}{straight}[\section]  % Create new 'section' command for exercises - this replaces \subsection
\newcounter{exercise}
\titleformat{\exercise}{\normalfont\large\bfseries}{Exercise }{0em}{\theexercise}
\titlespacing*{\exercise}{0pt}{3.25ex plus 1ex minus .2ex}{1.5ex plus .2ex}
\newcommand{\exerciseautorefname}{exercise}
\makeatletter
\providecommand*{\toclevel@exercise}{2} % Make sure exercises appear below sections in bookmarks
\makeatother

\pagestyle{headandfoot} % Let's have both
\header{\oddeven{Exercise \theexercise}{Chapter \thesection: \rightmark}}{Core 1}{\oddeven{Chapter \thesection: \rightmark}{Exercise \theexercise}}
\headrule
\footer{\oddeven{}{\thepage}}{}{\oddeven{\thepage}{}}
\footrule

\renewcommand\sectionmark[1]{\markright{#1}} % Set up \rightmark

\begin{document}

\unframedsolutions % Print solutions as plain text
\SolutionEmphasis{\color{blue}} % Print solutions in blue
\renewcommand{\thepartno}{\roman{partno}} % Format part numbers as lower-case roman numerals
\renewcommand\theexercise{\thesection\Alph{exercise}} % Print exercise numbers as 1A, etc...
\renewcommand{\solutiontitle}{\noindent} % Format solution - just print solution as entered
\newcommand{\ans}[1]{\uuline{#1}} % Double underline final answer

\NewEnviron{sol}{ % Align solutions sensibly
    \begin{solution}
        $\begin{alignedat}{2}
            \BODY
        \end{alignedat}$
    \end{solution}
}

\include{Chapter1}

\end{document}

Code – Chapter1.tex

\section{Basic Algebra}

\exercise{}
\begin{questions}
    \question Solve the following equations.
    \begin{parts}
        \part $5a - 32 = 68$
        \begin{sol}
            5a - 32 = 68 &\implies 5a &= 100\\
            &\implies \ans{a &= 20}
        \end{sol}
    \end{parts}
\end{questions}

Best Answer

Turns out the problem was with the alignment points in Chapter1.tex

I hadn't fully understood that the & (effectively) get used alternately, 'odd numbered' occurrences as alignment points (with the column to the left aligned to the right, and the column to the right aligned to the left), and 'even numbered' occurrences used to separate the groups of columns.

Hence if you were to use 5a - 32 = 68 &\implies 5a &= 100 (as above), this would get typeset with 5a - 32 = 68 aligned to the right, \implies set between the columns, then 5a aligned to the left, = between columns, and 68 aligned to the right. The correct usage is

\section{Basic Algebra}

\exercise{}
\begin{questions}
    \question Solve the following equations.
    \begin{parts}
        \part $5a - 32 = 68$
        \begin{sol}
            5a - 32 = 68 &\implies & 5a &= 100\\
            &\implies & \ans{a &= 20}
        \end{sol}
    \end{parts}
\end{questions}

Note the extra & following the &\implies, to start a new alignment group.

Related Question