3

How would you write a manual summation of numbers in TeX? That is, something like this(not necessarily identical):

                                                 

Bernard
  • 271,350
  • Welcome to TeX.SX! This are four numbers. Right? Why don't they have a plus on each line? Please provide some Minimal code, which shows your documentclass, your loaded math-packages and at least the numbers typed out. We do not want to set up the problem for you, as we do not know, how you are working and where you are getting the actual problem. Thank you. – LaRiFaRi Dec 22 '14 at 14:22
  • Welcome to TeX.SX! Please help us to help you and add a minimal working example (MWE) that illustrates your problem. It will be much easier for us to reproduce your situation and find out what the issue is when we see compilable code, starting with \documentclass{...} and ending with \end{document}. –  Dec 22 '14 at 14:22
  • @ChristianHupfer , LaRiFaRi Thank you for your welcome! I didn't post any code because I didn't have a clear idea on how to do this, I am getting no errors here :) – chubakueno Dec 22 '14 at 14:31

3 Answers3

3

Just for fun, I offer this automated alternative. The \convertchar turns the space separated input into a + separated input, to which \numexpr can be applied. I use right-aligned stacks to compose the result.

\documentclass{article}
\usepackage{stringstrings,stackengine}
\newcounter{mysum}
\newcommand\showsum[1]{%
  \convertchar[q]{#1}{ }{+}%
  \setcounter{mysum}{\numexpr\thestring\relax}%
  \def\stackalignment{r}%
  {\ttfamily\stackunder{\underline{+\Longstack{#1}}}{\themysum}}%
}
\begin{document}
\showsum{1 2 3 4}  $\qquad$
\showsum{23 567 34 32}  $\qquad$
\showsum{1 3567 2334 3352 567}
\end{document}

enter image description here

  • I searched for the $10$ near the $1,2,3,4$. There was no $10$. I started to read the code. And then I loled because of the ingenuity here! Really interesting. – chubakueno Dec 24 '14 at 21:19
  • @chubakueno Thanks. Indeed, the result is calculated from the inputs. – Steven B. Segletes Dec 25 '14 at 01:19
  • @chubakueno See my latest answer at http://tex.stackexchange.com/questions/213357/addition-with-3-elements-using-xlop/219509#219509, which provides an output format identical to your original question. – Steven B. Segletes Dec 26 '14 at 05:57
2

Leave the computation to TeX. ;-)

\documentclass{article}
\usepackage{xparse,array,booktabs}

\ExplSyntaxOn
\NewDocumentCommand{\computesum}{sO{}m}
 {
  \chubakueno_computesum:n { #3 }
  \IfBooleanTF{#1}
   {
    \ensuremath { \int_to_arabic:n { \l_chubakueno_sum_int } }
   }
   {
    \group_begin:
    \chubakueno_typesetsum:n { #2 }
    \group_end:
   }
 }

\int_new:N \l_chubakueno_sum_int
\seq_new:N \l_chubakueno_summands_seq
\seq_new:N \l_chubakueno_output_summands_seq
\seq_new:N \l__chubakueno_temp_seq
\tl_new:N \l_chubakueno_total_tl
\tl_new:N \l__chubakueno_temp_tl
\cs_generate_variant:Nn \int_set:Nn { Nx }

\keys_define:nn { chubakueno/sum }
 {
  spacing .dim_set:N = \l_chubakueno_spacing_dim,
  align   .tl_set:N  = \l_chubakueno_align_tl,
  align   .initial:n = c,
 }

\cs_new_protected:Npn \chubakueno_computesum:n #1
 {
  \int_set:Nn \l_chubakueno_sum_int { #1 }
  \seq_set_split:Nnn \l_chubakueno_summands_seq { + } { #1 }
 }

\cs_new_protected:Npn \chubakueno_typesetsum:n #1
 {
  \keys_set:nn { chubakueno/sum } { #1 }
  \dim_compare:nTF { \l_chubakueno_spacing_dim = 0pt }
   {% do nothing if the space is zero
    \seq_set_eq:NN \l_chubakueno_output_summands_seq  \l_chubakueno_summands_seq
    \tl_set:NV \l_chubakueno_total_tl \l_chubakueno_sum_int
   }
   {% else add the interline space
    \seq_map_inline:Nn \l_chubakueno_summands_seq
     {
      \seq_set_split:Nnn \l__chubakueno_temp_seq { } { ##1 }
      % insert the interdigits space
      \__chubakueno_insert_space:N \l__chubakueno_temp_tl
      \seq_put_right:NV \l_chubakueno_output_summands_seq \l__chubakueno_temp_tl
     }
    \seq_set_split:NnV \l__chubakueno_temp_seq { } \l_chubakueno_sum_int
    \__chubakueno_insert_space:N \l_chubakueno_total_tl
   }
  \begin{tabular} [\l_chubakueno_align_tl] { >{$}r<{$} @{$\;$} >{$}r<{$} }
  & \seq_use:Nnnn \l_chubakueno_output_summands_seq { \\ + & } { \\ & } { \\ + & }
  \\
  \midrule
  & \tl_use:N \l_chubakueno_total_tl
  \end{tabular}
 }

\cs_new_protected:Npn \__chubakueno_insert_space:N #1
 {
  \tl_set:Nx #1
   {
    \seq_use:Nn \l__chubakueno_temp_seq { \skip_horizontal:n { \l_chubakueno_spacing_dim } }
   }
 }

\ExplSyntaxOff

\begin{document}

Just the sum: \computesum*{345+6543+345}

The unspaced sum: \computesum{345+6543+345}

Spaced sum: \computesum[spacing=.1em,align=b]{640+231+100+91+1003}

Top aligned: \computesum[align=t,spacing=.2em]{333 + 333 + 333 + 2}

\end{document}

The first task is to compute the sum, storing it into an integer variable. The summands are also stored in a sequence, for later processing. If just \computesum* is called, only the sum is printed.

With the normal call, some possible options are evaluated: spacing is for interdigit space (default zero), align is for the overall alignment of the tabular, it can be c (default), t or b. Spaces around + in the mandatory argument are ignored.

Then the table is built by saving the summands with the interdigit space in a new sequence that's used in a tabular. The last row of the tabular is similarly built from the total.

enter image description here

egreg
  • 1,121,712
1

One possible solution could be a simple table.

\begin{table}[h]
\begin{tabular}{r}
1234  \\
345   \\
+6543 \\
345   \\ \hline
8467 
\end{tabular}
\end{table}

Edit: Results in following: enter image description here

jj01
  • 34
  • 1
    I recommend to use more positioning options that just h. – Stefan Kottwitz Dec 22 '14 at 14:33
  • Of course, you are right, but this should also just illustrate one possible solution. The positioning depends on the position of the equation in the text. – jj01 Dec 22 '14 at 14:35
  • Thank you for the solution, just a plus question: how would you add a blank space after a number? For example, to align those numbers as a multiplication. – chubakueno Dec 22 '14 at 14:40
  • Not quite sure what you mean with a blank space after a number for a multiplication, do you have an example for that? – jj01 Dec 22 '14 at 14:44
  • @jj01 Something like this . Thanks in advance. – chubakueno Dec 22 '14 at 14:59
  • @jjo1: I would write use an array environment and write {}+6543 rather than +6543  to ensure proper spacing around +. – Bernard Dec 22 '14 at 17:31
  • If you want to write the "." behind a number you simply write it there. – jj01 Dec 23 '14 at 06:45
  • But the other solution provided might better fit your purpose, did not know about these features, great to know ;). – jj01 Dec 23 '14 at 06:46
  • @jj01 I managed to do it using the tab separators & . Thank you anyway! – chubakueno Dec 24 '14 at 21:25