Summing numbers like paper calculation

A no-package approach for three term or higher sums as well.

\documentclass{article}

\newcommand{\showsum}[2][c]{%
  $\edef\originalplusmathcode{\the\mathcode`+}%
   \begingroup\lccode`~=`+ \lowercase{\endgroup\def~}{\\&}%
   \mathcode`+ "8000
  \begin{array}[#1]{@{}r@{\;}r@{}}
   \mathchar\originalplusmathcode& #2 \\
  \hline
  & \the\numexpr#2\relax
  \end{array}%
  $%
 }

\begin{document}

X\quad % to show the baseline
\showsum{12345 + 6543}\quad
\showsum{521725 + 256814}\quad
\showsum{523057 + 6743}\quad
\showsum[t]{57208+6207}\quad
\showsum[b]{57208+6207+12095}\quad
X

\end{document}

enter image description here

The X are here to indicate baseline. (plagiarized from @egreg)


Variant display:

\documentclass{article}

\newcommand{\showsum}[2][c]{%
  $\edef\originalplusmathcode{\the\mathcode`+}%
   \begingroup\lccode`~=`+ \lowercase{\endgroup\def~}{\\\mathchar\originalplusmathcode&}%
   \mathcode`+ "8000
  \begin{array}[#1]{@{}r@{\;}r@{}}
   & #2 \\
  \hline
  =& \the\numexpr#2\relax
  \end{array}%
  $%
 }

\begin{document}

X\quad % to show the baseline
\showsum{12345 + 6543}\quad
\showsum{521725 + 256814}\quad
\showsum{523057 + 6743}\quad
\showsum[t]{57208+6207+77777}\quad
\showsum[b]{57208+6207+12095+33333}\quad
X

\end{document}

enter image description here


We can also sum negative integers:

\documentclass{article}

\newcommand{\showsum}[2][c]{%
  $\edef\originalplusmathcode{\the\mathcode`+}%
   \begingroup\lccode`~=`+ \lowercase{\endgroup\def~}{\\\mathchar\originalplusmathcode&}%
   \edef\originalminusmathcode{\the\mathcode`-}%
   \begingroup\lccode`~=`- \lowercase{\endgroup\def~}{\\\mathchar\originalminusmathcode&}%
   \mathcode`+ "8000
   \mathcode`- "8000
  \begin{array}[#1]{@{}r@{\;}r@{}}
   & #2 \\
  \hline
  =& \the\numexpr#2\relax
  \end{array}%
  $%
 }

\begin{document}

X\quad % to show the baseline
\showsum{12345 - 6543}\quad
\showsum{521725 + 256814}\quad
\showsum{523057 - 6743}\quad
\showsum[t]{57208-6207+77777}\quad
\showsum[b]{57208-6207+12095-33333}\quad
X

\end{document}

enter image description here

(there was a missing % after the final $ in all three \showsum, fixed now but images not updated)


The code below defines a macro, \Summation, that accepts a comma separated list of integers such as

  \Summation{12345, 6543}
  \Summation{521725, 256814}
  \Summation{523057, 6743}
  \Summation{57208,6207}
  \Summation[b]{57208,6207,12095}

The macro then adds the integers in a table, as in the OP. The commands above give the output:

enter image description here

There is an optional first argument that becomes the positioning optional argument in the tabular environment (by default, t is used). I haven't really checked, but it is likely ro break with large integers.

All of the integers are printed using the \num command from the siunitx package, so their formatting can be customised using siunitx. for example, by adding

\sisetup{group-separator={,},group-four-digits}

the numbers will have a comma separating the thousands, millions, ... etc. so that the output becomes

enter image description here

The code is an exercise in using LaTeX3:

\documentclass{article}
\usepackage{xparse}
\usepackage{siunitx}

\ExplSyntaxOn
\clist_new:N \l_int_clist
\int_new:N \g_total_int
\tl_new:N \g_summation_tl
\NewDocumentCommand\Summation {O{t} m}{
  \clist_set:Nn \l_int_clist {#2}
  \int_zero:N \g_total_int
  \tl_clear:N \g_summation_tl
  \clist_map_inline:Nn \l_int_clist {
    \int_gadd:Nn \g_total_int {##1}
    \tl_gput_right:No \g_summation_tl {& \num{##1}\\}
   }
   \begin{tabular}[#1]{r@{\space}r}
     + \tl_use:N \g_summation_tl \cline{2-2}
     &\num{\int_use:N \g_total_int}
  \end{tabular}
}
\ExplSyntaxOff

\begin{document}

  \Summation{12345, 6543}
  \Summation{521725, 256814}
  \Summation{523057, 6743}
  \Summation{57208,6207}
  \Summation[b]{57208,6207,12095}

\end{document}

As noted in Latex3 inline mapping produces extra row in tabular, it is necessary to construct the table as a token list because otherwise \hrule will complain.


Let TeX do the calculations

\documentclass{article}
\usepackage{xparse}

\ExplSyntaxOn
\NewDocumentCommand{\showsum}{O{c}m}
 {
  \ensuremath
   {
    \simeon_showsum:nn { #1 } { #2 }
   }
 }

\seq_new:N \l__simeon_showsum_seq

\cs_new_protected:Nn \simeon_showsum:nn
 {
  \seq_set_split:Nnn \l__simeon_showsum_seq { + } { #2 }
  \begin{array}[#1]{@{}r@{\;}r@{}}
  + & \seq_use:Nn \l__simeon_showsum_seq { \\ & } \\
  \hline
  & \int_eval:n { #2 }
  \end{array}
 }
\ExplSyntaxOff

\begin{document}

X\quad % to show the baseline
\showsum{12345 + 6543}\quad
\showsum{521725 + 256814}\quad
\showsum{523057 + 6743}\quad
\showsum[t]{57208+6207}\quad
\showsum[b]{57208+6207+12095}\quad
X

\end{document}

enter image description here

Tags:

Macros

Tables