Tell me more ×
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.

How do I present vertical (columnar) multiplication and addition in LaTeX. Like this: http://mathworld.wolfram.com/LongMultiplication.html

    3 8 4
x     5 6
---------
  2 3 0 4
1 9 2 0
---------
2 1 5 0 4

I used the "array" environment. But i have right align problem. Any other best ideas?

share|improve this question
1  
Welcome to TeX.sx. I removed the LyX tag, since this question doesn't seem to have anything to do with LyX. If you think LyX is relevant to the question, please explain why by editing your question, and add the tag back in. – Seamus Feb 22 '11 at 9:52
I took the liberty to show an example of the output you'd like to receive. I hope I got it right -- feel free to revert the changes or clarify what output you'd like to get, if necessary. – Martin Tapankov Feb 22 '11 at 10:03
@Seamus Because I am using lyx in latex front end. And I am latex beginner. But I mean you`re right. Lyx tag is unnecessary. – gmunkhbaatarmn Feb 22 '11 at 10:10

4 Answers

up vote 27 down vote accepted

the xlop package does this sort of thing. It does warn that it uses "french conventions", but at least for multiplication it looks fine, to me.

disclaimer: i last did multiplication sums in school in the 1950s...

\documentclass{article}
\usepackage{xlop}
\begin{document}
\opmul{384}{56}\qquad
\end{document}

output image

share|improve this answer
Good answer. You can add links directly to your answers by clicking on the link icon. (I changed this for you.) – Alan Munn Feb 22 '11 at 12:48
Very nice! I especially like the fact that it does the multiplication for you (now if only there was a package that did matrix multiplication ... then there wouldn't be so many errors in my lectures). – Andrew Stacey Feb 22 '11 at 13:06
@Andrew: That should not be too hard. It may even be easier than the vertical multiplication. Or you can multiply them in octave and let octave output them in latex format. – Jan Hlavacek Feb 22 '11 at 17:26
@Jan: Do you know how to get octave to do that? I keep looking but haven't yet seen how to do it. – Andrew Stacey Feb 22 '11 at 19:12
4  
@Andrew: strrep(strrep(mat2str(A),",","&"),";","\\\\\n")(2:end-1) where A is your matrix. That will give you the body of your matrix, without the \begin{matrix} and \end{matrix}. – Jan Hlavacek Feb 22 '11 at 19:52
show 2 more comments

How about a simple tabular environment:

\documentclass{article}
\begin{document}
\begin{tabular}{cccc}
  & 1 & 2 & 3 \\
+ &   & 3 & 4 \\
\hline
  & 1 & 5 & 7 \\
\end{tabular}
\end{document}

enter image description here

If you need to change spacing, you can use the @ specifier which automatically puts arguments in braces as a space between columns (in this case a thin space \,).

\documentclass{article}
\begin{document}
\begin{tabular}{c@{\,}c@{\,}c@{\,}c}
  & 1 & 2 & 3 \\
+ &   & 3 & 4 \\
\hline
  & 1 & 5 & 7 \\
\end{tabular}
\end{document}

enter image description here

Some other spaces are a thick space \; and a medium space \:. It is also possible to avoid the onerous typing of repeating column types by using the *{}{}:

\begin{tabular}{c*{3}{@{\,}c}}

That produces c, and then 3 times @{\,}c, which combines to c@{\,}c@{\,}c@{\,}c.

share|improve this answer
1  
+1 for the explanation of the tabuar column specifications. – Seamus Feb 22 '11 at 17:36

Here is one way using Plain-commands:

\vbox{
  \openup2pt
  \def\trule{\noalign{\smallskip\hrule\smallskip}}
  \halign{&\tabskip1em$\mathstrut#$\cr
          &   & 3 & 8 & 4 \cr
    \times&   &   & 5 & 6 \cr
    \trule
          & 2 & 3 & 0 & 4 \cr
    1     & 9 & 2 & 0 \cr
    \trule
    2     & 1 & 5 & 0 & 4 \cr
  }
}
\bye

multipl

share|improve this answer

It is always preferable for a problem like this to let TeX do the calculations for you. Here is a draft solution and it does not use any tables. It still misses a small iteration macro to be completed, but I decided to post it, as it is easier to understand the code at this stage of development.

\documentclass{article}
\usepackage{fp,intcalc}
\begin{document}
  \def\multiplication#1#2{
  \def\answer{\FPmul\temp{#1}{#2}
   \parindent=0pt
   \FPround\temp{\temp}{0}\temp}
   \def\linea{#1}
   \def\lineb{$\times$\hfill#2}
   \def\linez##1##2##3{
   \intcalcMul{#1}{##2}##3}
   \def\Rule{\rule{1.5cm}{0.4pt}}
   \begin{minipage}[t]{1.5cm}
     \begin{flushright}
       \linea\\
       \lineb\\[-2pt]
       \Rule\\
       \def\Z{\phantom{0}}
       \linez{#1}{3}{}\\
       \linez{#1}{2}{\Z}\\[-8pt]
       \Rule
       \answer
     \end{flushright}
   \end{minipage}}
\multiplication{35670}{23}
\end{document}    

I have used both the fp package as well as the intCalc package to perform the calculations for demonstration purposes. It will be preferable to perform these calculations with the fp package in order to handle decimals correctly. One possibility also is to use a random function to set the what I presume are exercises randomly as well as produce the answers.

In many European countries the x operator would be typeset on the right.

share|improve this answer
I think there's a typo: the package should be intcalc and not incCalc. – Gonzalo Medina Sep 13 '11 at 20:31
@Gonzalo you right! I fixed. – Yiannis Lazarides Sep 14 '11 at 6:14

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.