# Using eqnarray (or similar) with exam class

I'm trying to use eqnarray in my solutions to questions using the exam class:

    \begin{questions}
\question What is $10 - 3$?
\begin{solution}
\begin{eqnarray}
10 - 3 &= 10 + (-3) \\
&= 7 + 3 + (-3 )\\
&= 7 + 0\\
&= 7
\end{eqnarray}
\end{solution}
\end{questions}


The result is not formatted properly:

Is there a workaround?

-
Do you need each line numbered separately? – Steven B. Segletes Apr 3 '14 at 11:26
Hi Steven, thank you, that was quick. No, I don't need them numbered in any particular way. Or indeed at all. – Robert Arbon Apr 3 '14 at 11:28
Put also & after =,i.e, &=& – ferahfeza Apr 3 '14 at 11:31
Oh my goodness, that was simple thanks. I feel a bit embarrassed now! Thanks once again for your prompt reply. – Robert Arbon Apr 3 '14 at 11:36
@RobertArbon: I need to add an important note: eqnarray is outdated! Please replace it by align! – strpeter Apr 3 '14 at 11:52

Here's one "or similar" way, if you didn't want the numberings. Also, it could be moved to any horizontal position. I centered it by surrounding it with \hfils:

\documentclass{exam}
\usepackage{tabstackengine}
\stackMath
\begin{document}
\begin{questions}
\question What is $10 - 3$?
\begin{solution}
\par\hfil\alignCenterstack{%
10 - 3 =& 10 + (-3) \\
=& 7 + 3 + (-3 )\\
=& 7 + 0\\
=& 7
}\hfil
\end{solution}
\end{questions}
\end{document}


One advantage of using this approach is that it puts the equation in a LaTeX "box", and thus there are limitless ways in which they can be moved around and manipulated on the page. Here, for example, you could lay out two parallel solution strategies:

\documentclass{exam}
\usepackage{tabstackengine}
\stackMath
\begin{document}
\begin{questions}
\question What is $10 - 3$?
\begin{solution}
\par\hfil\alignCenterstack{%
10 - 3 =& 10 + (-3) \\
=& 7 + 3 + (-3 )\\
=& 7 + 0\\
=& 7
}\hfil\alignCenterstack{%
10 - 3 =& 10 + (-3) \\
=& 10 + (7 - 10)\\
=& 10 +(- 10) + 7\\
=& 7
}\hfil
\end{solution}
\end{questions}
\end{document}


\documentclass{exam}
\usepackage{tabstackengine}
\usepackage{xcolor}
\usepackage{graphicx}
\stackMath
\begin{document}
\begin{questions}
\question What is $10 - 3$?
\begin{solution}
\par\hfil\alignCenterstack{%
10 - 3 =& 10 + (-3) \\
=& 7 + 3 + (-3 )\\
=& 7 + 0\\
=& 7
}\hfil\stackinset{r}{3.7ex}{t}{-3.2ex}%
{\color{red}\stackon{\scalebox{8}{$\times$}}{\mathrm{Don't~do~this!}}}%
{\alignCenterstack{%
10 - 3 =& 10 + (-3) \\
=& 10 + (7 - 10)\\
=& 10 +(- 10) + 7\\
=& 7
}}\hfil
\end{solution}
\end{questions}
\end{document}


-
Thanks very much! – Robert Arbon Apr 3 '14 at 11:40
Instead of a “Thank you” comment, you should give feedback clicking the 'up' arrow you can see above each answer's score. Once you’ve gathered 15 reputation points on this site, you’ll be able to upvote answers to contribute to this score. – Benedikt Bauer Apr 3 '14 at 11:59
Hi, thanks I just need two more reputation point to do that. – Robert Arbon Apr 3 '14 at 12:10
Can't these be made with envs instead? Making these as macro args is not particularly userfriendly (IMO) – daleif Apr 3 '14 at 12:11
@daleif There are already a number of environments for stacking math (which tend to occupy the full text width), but there were not, to my knowledge, the same breadth of macros, so I saw an opening to contribute. I don't find the syntax off-putting (one just slaps braces around the content); however, one conundrum I haven't yet cracked is getting each equation numbered. It's not easy since the result is a single LaTeX box. – Steven B. Segletes Apr 3 '14 at 12:17

The probably best approach is to use amsmath and its environment align. See the following MWE:

\documentclass{exam}
\usepackage{amsmath}
\begin{document}
\begin{questions}
\question What is $10 - 3$?
\begin{solution}
\begin{align}
10 - 3 &= 10 + (-3) \\
&= 7 + 3 + (-3 )\\
&= 7 + 0\\
&= 7
\end{align}
\end{solution}
\end{questions}
\end{document}


Remember that there is bad syntax allowed such as eqnarray. For more hints see l2tabu.

-
This also have a problem =& does not add space after the =. If you want to align on the right, use ={}& instead. – daleif Apr 3 '14 at 12:12
the instructions for align clearly say to put the & before the = (or other relation); after the relation is set, it's too late to determine what the proper spacing should be. – barbara beeton Apr 3 '14 at 12:15
@barbarabeeton: Thanks, I thought it would not really matter, as everything in front of the & is placed relatively right of the alignment. You are right! – strpeter Apr 3 '14 at 12:38