2

I have the following issue with some recursive calculus.

I just write the function \recsum that recursively computes the sum of an unspecified amount of integers.

It works when I call the function by itself. But when I try to store the results, it doesn't work anymore. Error:

! Incomplete \iffalse; all text was ignored after line 22. \fi

I try printing \expandafter\meaning\mysum{12345}, but it only makes me even more bewildered...

Here is the code I wrote:

\documentclass{article}

\usepackage{tikz}

\begin{document}

\def\recsum#1#2#3\endsum{%
  \pgfmathsetmacro{\ble}{#1+#2}%
  \def\dum{#3}%
  \ifx\dum\empty%
     \ble%
  \else%
     \recsum{\ble}#3\endsum%
  \fi%
}

\def\mysum#1{\recsum{0}#1\endsum}

-- \mysum{14283} --

% % This doesn't work:
% \edef\ble{\mysum{14283}}

\end{document}

What's happening here?

  • 1
    You could make the macro \mysum have an optional argument that, if present, receives the result at the end of the calculation. More or less as the xstring package does. – Phelype Oleinik May 18 '18 at 12:57
  • 1
    your macro does not work via expansion (it makes internal definitions) so it can not be used in \edef – David Carlisle May 18 '18 at 13:02
  • Including the name of the receiving variable in \mysum and \recsum seems to work fine. I'll try to see later if it solves all my problems (the real function is a bit more complicated)! Thanks! :D – Vinzza May 18 '18 at 13:25
  • @PhelypeOleinik, could make an answer of your comment, so I could accept it (and erase mine)? (By the way, sorry for the answer that was supposed to be yours!) – Vinzza May 21 '18 at 9:16
  • @Vinzza No need for apologies :) Sometimes, people prefer to comment because it's more of a suggestion than an answer (in this case I suggested what you could do, I didn't show how). Here we encourage high quality, complete answers, so everything else is better fit as a comment. Of course, it's the best practice you wait some time (or ask the commenter) to see if they want to post an answer to avoid "stealing". With practice you'll get the hang of how the site goes and know what to do. Welcome to TeX.SX! (BTW, egreg's answer actually does what you were asking, and not just works-around.) – Phelype Oleinik May 21 '18 at 11:20
3

In fully expandable fashion (and no decimal part in the result):

\documentclass{article}
\usepackage{xparse}

\ExplSyntaxOn
\NewExpandableDocumentCommand{\listsum}{m}
 {
  \int_eval:n { 0 \tl_map_function:nN { #1 } \vinzza_sum:n }
 }
\cs_new:Nn \vinzza_sum:n { + #1 }
\ExplSyntaxOff

\begin{document}

\listsum{14282}

\listsum{123456789{10}}

\edef\result{\listsum{57}}
\texttt{\meaning\result}

\end{document}

enter image description here

You can even leave spaces, so

\listsum{1 4 2 8 2}

would produce 17; however, multidigit numbers should still be braced, so you can do

\listsum{1 2 3 4 5 6 7 8 9 {10}}

On the other hand, with no extra package

\documentclass{article}

\newcommand{\dosum}[1]{\the\numexpr#1\relax}

\begin{document}

\dosum{1+2+3+4+5}

\end{document}

would easily print 15 (any number of summands is allowed, so long as the final sum is less than 231). This is fully expandable as well so you can do

\edef\result{\dosum{1+2+3+4+5}}

to store the result in a macro.

2

The suggestion of @phelype works perfectly!

You could make the macro \mysum have an optional argument that, if present, receives the result at the end of the calculation. More or less as the xstring package does.

Thanks again!

The working code:

\def\recsum#1#2#3\endsum{%
  \pgfmathsetmacro{#1}{#1+#2}%
  \def\dum{#3}%
  \ifx\dum\empty\else%
  \recsum{#1}#3\endsum%
  \fi%
}
\def\mysum#1#2{\def#1{0}\recsum{#1}#2\endsum}

\mysum\results{14282}
--\results--
  • It's bad custom to steal an answer like that. You should ask if @PhelypeOleinik could make an answer of this and then accept that! – Andreas Storvik Strauman May 18 '18 at 18:29
  • I'm sorry if I haven't done the proper thing here... :/ This is my first question on StackExchange and I just wanted to close my question. I'll do as you suggest! Thank you for putting me in the right path! :) – Vinzza May 21 '18 at 9:09
  • It’s okay. I’m just informing you. It’s nice that you ask questions, but you might want to take the tour :) – Andreas Storvik Strauman May 21 '18 at 9:11
  • 1
    To avoid one of the definitions you could another is-it-empty test. +1 anyway :) – Phelype Oleinik May 21 '18 at 11:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.