8

For some project, I'm trying to typeset something into approximately this:

 1234
  321
12345
    6
----- + 
13906

I've seen people do this using xlop for the addition of two values, which works really nicely, and array for more values, which is not ideal.

Is there any convenient package that can (something like) this for multiple values?

  • do you want something different than the layout you'd get from a simple array ? – David Carlisle Nov 06 '16 at 19:34
  • Not neccessarily, but if you look up the xlop package you'll see that its syntax for specifying the addition is rather consice, as opposed to the very verbose array. I was hoping a multi-value variant of xlop, or at least a less repetitive version of array existed. – Bert Peters Nov 06 '16 at 19:39
  • related questions http://tex.stackexchange.com/questions/45304/vertical-addition-and-multiplication-and-long-division and http://tex.stackexchange.com/questions/11702/how-to-present-a-vertical-multiplication-addition – samcarter_is_at_topanswers.xyz Nov 07 '16 at 13:50

3 Answers3

6

Here's a simple syntax:

\documentclass{article}

\usepackage{xparse}

\ExplSyntaxOn
\NewDocumentCommand{\addition}{O{c}m}
 {
  \seq_set_split:Nnn \l_bert_addition_seq { + } { #2 }
  \begin{array}[#1]{@{}r@{\;}l}
  \seq_use:Nn \l_bert_addition_seq { \\ } \\[-.5ex]
  \additionrule \\[-.5ex]
  \int_eval:n { #2 }
  \end{array}
 }
\seq_new:N \l_bert_addition_seq
\ExplSyntaxOff

\newcommand{\additionrule}{%
  \leaders\hrule height \dimexpr \fontdimen22\textfont2+0.2pt\relax
                 depth \dimexpr -\fontdimen22\textfont2+0.2pt\relax
  \hfill\hspace*{0pt} & +
}

\begin{document}

\[
\addition{1234+321+12345+6}
\]

\end{document}

The optional argument to \addition can be t, b or c and is passed to array.

enter image description here

egreg
  • 1,121,712
5

This answer does not provide additional spacing between digits, as is done with xlop, but otherwise provides multi-addend addition.

\documentclass{article}
\usepackage{xlop}
\usepackage{stringstrings,stackengine}
%%% \showsum based on http://tex.stackexchange.com/questions/219090/
%%%            writing-manual-summation-of-two-numbers/219113#219113
\newcounter{mysum}
\newcommand\showsum[1]{%
  \convertchar[q]{#1}{ }{+}%
  \setcounter{mysum}{\numexpr\thestring\relax}%
  \def\stackalignment{r}%
  \edef\tmp{\themysum}%
  {\stackunder{\underline{\ \Longstack{#1}}}{%
   \tmp}\raisebox{-\dp\strutbox}{\,+}}%
}
\begin{document}
\opadd{12}{12}

\showsum{1 2 3 4}  $\qquad$
\showsum{23 567 34 32}  $\qquad$
\showsum{1 3567 2334 3352 567}
\end{document}

enter image description here

  • This looks wonderful. Not as simple as a using a package, but very clear and looks beautiful. I will definitely be using this. Thank you. – Bert Peters Nov 06 '16 at 21:20
3

Since I can't comment on this (not enough reputation) I'll post this as an answer. Do you have to do it inside a math-environment? You could accomplish that in a tabular-environment easily I think.

EDIT: Because it was requested:

\documentclass{article}

\begin{document}
    \begin{tabular}{rr}
         &1234\\
        +&321\\
        +&12345\\
        +&6\\
        \hline
        &13906
    \end{tabular}
\end{document}

Please note that this doesn't calculate the results. Also please note that this puts a '+' in front of every row and not next to the \hline. For that use:

\documentclass{article}

\usepackage{multirow}

\begin{document}
    \begin{tabular}{rr}
         1234 & \\
          321 & \\
        12345 & \\
            6 & \multirow{2}{*}{+}\\
        \cline{1-1}
        13906 & \\
    \end{tabular}
\end{document}
Skillmon
  • 60,462
  • 4
    Please, provide an example how to do this ..., than this comment will become answer :-) – Zarko Nov 06 '16 at 20:46