Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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

    \begin{questions}
    \printanswers
    \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:

Eqnarray?

Is there a workaround?

share|improve this question
    
Do you need each line numbered separately? –  Steven B. Segletes Apr 3 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 at 11:28
1  
Put also & after =,i.e, &=& –  ferahfeza Apr 3 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 at 11:36
3  
@RobertArbon: I need to add an important note: eqnarray is outdated! Please replace it by align! –  strpeter Apr 3 at 11:52

2 Answers 2

up vote 4 down vote accepted

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

enter image description here

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

enter image description here

or even add commentary:

\documentclass{exam}
\usepackage{tabstackengine}
\usepackage{xcolor}
\usepackage{graphicx}
\stackMath
\begin{document}
    \begin{questions}
    \printanswers
    \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}

enter image description here

share|improve this answer
    
Thanks very much! –  Robert Arbon Apr 3 at 11:40
1  
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 at 11:59
1  
Hi, thanks I just need two more reputation point to do that. –  Robert Arbon Apr 3 at 12:10
1  
Can't these be made with envs instead? Making these as macro args is not particularly userfriendly (IMO) –  daleif Apr 3 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 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}
    \printanswers
    \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.

share|improve this answer
    
This also have a problem =& does not add space after the =. If you want to align on the right, use ={}& instead. –  daleif Apr 3 at 12:12
1  
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 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 at 12:38

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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