How can I keep equations left-aligned only if they do not exceed a certain width?

I would like all my equations to be left aligned by default, and to become centered as soon as 2*fleqn_indent + eq_width >= textwidth.

The following MWE might be more explicative:

\documentclass{scrbook}
\usepackage[fleqn]{amsmath}
\usepackage{showframe}
\usepackage{etoolbox}
\begin{document}
\noindent this should stay left-aligned
$a + b = c$
this should be centered like the one after it
$\sum_{i = 1}^{\infty} i = 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + 11 + 12 + + 13 + 14 + 15 + \dots$
{
\makeatletter
\setbool{@fleqn}{false}
\makeatother
$\sum_{i = 1}^{\infty} i = 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + 11 + 12 + + 13 + 14 + 15 + \dots$
}
and the same for all other math environments, for example \texttt{align}:
\begin{align*}
\sum_{i = 1}^{\infty} i &= 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + 11 + 12 +
+ 13 + 14 + 15 + 16 + \dots \\
&= -\frac{1}{12}
\end{align*}
{
\makeatletter
\setbool{@fleqn}{false}
\makeatother
\begin{align*}
\sum_{i = 1}^{\infty} i &= 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + 11 + 12
+ 13 + 14 + 15 + 16 + \dots \\
&= -\frac{1}{12}
\end{align*}
}
\end{document}


• you can't (using standard amsmath) change the value of the fleqn boolean mid document, you can change its value but the effects are unexpected, the package uses the option at package loading time and only fully defines the selected variants, so in some simple cases changing the boolean accidentally appears to work,but mostly it doesn't. – David Carlisle 2 days ago
• then what is the correct way to disable fleqn locally? – noibe 2 days ago
• it is not a supported feature. There is a package (I forget the name) that offers local flush left if you use centered by default, I'll see if I can find it. Bernard mentions it a few times on this site. Is switching to centred really useful? In almost all cases if it doesn't fit then it will still not fit centred in which case you will need to break the line, in whic case you can go back to flush left. so it is only a few equations right at teh edge case where it makes a difference – David Carlisle 2 days ago
• nccmath fleqn env, see tex.stackexchange.com/questions/369240/… – David Carlisle 2 days ago
• I use flush left by default so that package would be to little use to me. Like you already said, this isn't for really long equations. It's for those equations that still fit in one line but are long enough that it doesn't look good to keep them flushed left. – noibe 2 days ago