I have been defining a mathematic formula using NewDocumentCommand. This works well, but I do manage to get line breaks.
A minimal working example can be found below. Part of the construction is borrowed from parse variable length comma separated list into command (Peter Grill's answer).
\documentclass{article}
\usepackage{amsmath}
\usepackage{pgffor}
\usepackage{xparse}
\NewDocumentCommand{\formulaEx}{m}{
\gdef\separator{}
\gdef\formula{}
\foreach \x in {1,...,#1} {%
\xdef\formula{\formula \separator \frac{1}{(1+\x)^{\x}} }
\gdef\separator{ + }
}
\formula
\gdef\formula{}
}
\begin{document}
\begin{align*}
P &= \formulaEx{10}
\end{align*}
\end{document}
This does not generate any line break. Replacing + by + \\ or + \newline yields an error. I also tried to use the package breqn but it is ineffective (no error but no line break).
I have tried a number of workarounds (such as adapting the solution proposed here tex capacity exceeded when using breqn package with a separate macro), but all of them were useless.
Many thanks for your help.



align*and go with$P = \formulaEx{10}$– Steven B. Segletes Nov 06 '17 at 17:40\xdefwith content containing latex commands. – David Carlisle Nov 06 '17 at 18:26edefinstead ofxdefbut the problem remains. I guess this is related to expansion and I am not 100% clear (to say the least) with expansion. – isanco Nov 06 '17 at 20:14