# How to present a vertical multiplication/addition

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?

• 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 youre right. Lyx tag is unnecessary. – gmunkhbaatarmn Feb 22 '11 at 10:10

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}
\end{document} • 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). – Loop Space 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
• @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
• Good answer. And I have a have one question. What about algebra expression? Example: 1*2x1*=? or, AB*CDE=? – gmunkhbaatarmn Feb 23 '11 at 5:38

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

• +1 for the explanation of the tabuar column specifications. – Seamus Feb 22 '11 at 17:36

I think the simplest (least complicated) solution is to use \phantom, as follows:

\documentclass{article}
\usepackage{amsmath}
\begin{document}
\begin{equation*}\begin{array}{c}
\phantom{\times99}384\\
\underline{\times\phantom{999}56}\\
\phantom{\times9}2304\\
\underline{\phantom\times1920\phantom9}\\
\phantom\times21504
\end{array}\end{equation*}
\end{document} Here, amsmath is needed for the equation* environment only; if you used equation you'd need no \usepackage at all. array allows multiple-line equations; \\ starts a new line. And \phantom{foo} prints a space the width of the characters foo. I've usually found 9 to be a good stand-in for any digit, but (e.g. if you have a few 1s) you may need to fiddle with it a bit.

• The Computer Modern font family (which is used by default in TeX and LaTeX) uses "tabular" numerals, i.e, they all have the same width. It thus doesn't matter for the case at hand if you write \phantom{1} or \phantom{8}. On the other hand, if one loadds a package that provides "proportional-spaced" numerals, this approach -- and virtually all others mentioned in the answers to the present posting -- aren't workable. – Mico Apr 6 '14 at 5:33
• If you use {r} instead of {c}, you can get rid of all phantoms except after 1920, where you want an invisible character to its right. – JDH Jul 23 '18 at 15:56

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 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{
\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
\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.

• Did you ever get the above completed? In particular, while it works well in N, I cannot use decimals. – schremmer Oct 18 '16 at 0:17
• @schremmer This was just a concept. To be honest I would either use LuaTeX or expl3 today. – Yiannis Lazarides Oct 18 '16 at 3:07
• In fact, I had missed the answer suggesting xlop and it is nice. So, I am all set. – schremmer Oct 19 '16 at 13:13

Simply use any array environment \begin{array} or \beign{align} or the most powerfull tool \begin{IEEEeqnarray} and put a \cline[i-j] in it where i & j are the coloumns numbers

\begin{IEEEeqnarray*}{crCl}
& 4 &\equiv& (1, 0, 4)\\
+& 9 &\equiv& (0, 1, 4)\\[-7pt]
\cline{1-4}
& 13 &\equiv& (1, 1, 3)
\end{IEEEeqnarray*}
`

produces the following 