4

Here is my previous thread.

I want to make the same calculations with number with decimal separator. Summing 2, 3 numbers or more.

This is my current code. I want to make 57208 - 57,208; 6207 - 6,207 and more..

\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[b]{57208+6207+12095}\quad
X

\end{document}

What I need. Also with 2 numbers only. \showsum[b]{57,208+6,207+12,095}\quad enter image description here

EDIT: May question was wrong formulated. I want to sum numbers like that: 6.7+13.12+2.501=22.321 (with comma, not with dot in visualizing). And enter it in function in the same way.

Simeon Simeonov
  • 819
  • 5
  • 11

1 Answers1

4

The following is specific to writing a sum and uses siunitx's \num to print the numbers with their thousand separators:

enter image description here

\documentclass{article}

\usepackage{siunitx,xfp}

\newcommand{\addsum}[1]{\edef\sumtotal{\inteval{\sumtotal+#1}}}
\newcommand{\stacksum}[1]{\edef\stackedsum{\stackedsum \tnc \tcf{#1} \tnl}}

\NewDocumentCommand{\showsum}{ O{c} >{ \SplitList { + } } m }{%
  $\def\stackedsum{}
  \let\tnl\relax\let\tnc\relax\let\tcf\relax
  \ProcessList{#2}{\stacksum}
  \def\tnl{\\}\let\tnc&\def\tcf{\num}
  \sisetup{
    group-separator={,},
    group-minimum-digits=4}
  \begin{array}[#1]{@{} r @{} r @{}}
    +\;{}\stackedsum
    \hline
    & \def\sumtotal{0}%
    \ProcessList{#2}{\addsum}\num{\sumtotal}
  \end{array}$%
}

\begin{document}

X\quad % to show the baseline

\showsum[b]{57208+6207+12095}\quad
X

\end{document}
Werner
  • 603,163
  • May question was wrong formulated. I want to sum numbers like that: 6.7+13.12+2.501=22.321 (with comma, not with dot in visualizing). And enter it in function in the same way. – Simeon Simeonov Dec 24 '18 at 13:30
  • @SimeonSimeonov: For that you can use \fpeval (instead of \inteval as in my example). If you add output-decimal-marker={,} to the setting of \sisetup, you'll get a , in the output. – Werner Dec 24 '18 at 16:23
  • Okay I changed it and it works. https://pastebin.com/uPWt3ABw But how to make how to make to work with subtraction? – Simeon Simeonov Dec 24 '18 at 23:51
  • 1
    @SimeonSimeonov: What would you want the input to look like? \showsub[.]{X-Y-Z-...}? Or are you ultimately interested in something more generic: \showsum[.]{X-Y+Z+A-B-C+D...}? – Werner Dec 26 '18 at 07:16
  • How to place the + in the middle in front of 6,207 instead of 57,208. I learned that it's best to be in the center always for better readability. – Simeon Simeonov Apr 13 '19 at 23:06
  • 1
    @SimeonSimeonov: By "in the middle", do you mean the second element? Or do you mean the middle element of the list? – Werner Apr 14 '19 at 04:47
  • again you are making the right question. https://imgur.com/a/GgRCaxG . I mean the middle element of the list. – Simeon Simeonov Apr 14 '19 at 10:50