In many CAS-systemscomputer algebra systems, one can sum a sequence of numbers using the syntax
sum(a(k), k, kStart, kEnd) for a sequence a(k) (of real and integer values) with the variable k.
What's the best and fastest way to do this with LaTeX?
For example, when you want to calculate thousands of values for a table.
I've developed a MWE using foreach and fpeval.
\documentclass[margin=5pt, varwidth]{standalone}
\usepackage{amsmath}
\usepackage{tikz}
\newcommand\Sum[4]{%
\def\mysum{0}%
\foreach #2 in {#3,...,#4}{%
\def\psum{\fpeval{(#1)}}%
\xdef\mysum{\fpeval{\mysum+\psum}}%
}\mysum}
\begin{document}
Test 1: $\displaystyle\sum\limits_{k=1}^{100} k = \Sum{\k}{\k}{1}{100}$
\newcommand\binomial[2]{\fpeval{fact(#1)/(fact(#2)*fact(#1-#2))}}
Test 2: $\displaystyle\sum\limits_{k=0}^{5} \dbinom{5}{k}
= \Sum{ \binomial{5}{\k} }{\k}{0}{5} =2^5 = \fpeval{2^5}$
\end{document}
