当方程式不超过一定宽度时,如何才能保持方程式左对齐?

当方程式不超过一定宽度时,如何才能保持方程式左对齐?

我希望我的所有方程式默认都左对齐,并且尽快居中2*fleqn_indent + eq_width >= textwidth

以下 MWE 可能更具解释力:

\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}

在此处输入图片描述

答案1

也许这个解决方案有效。

在此处输入图片描述

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{expl3}
\usepackage{xparse}
\usepackage{nccmath}
\usepackage{datetime2}

% macro to get the length of equation
% https://tex.stackexchange.com/questions/59925/how-to-get-width-of-equation
\makeatletter
\newcommand{\settowidthofalign}[2]{%
  \setbox\z@=\vbox{
    \begin{align*}
    #2
    \ifmeasuring@\else\global\let\got@maxcolwd\maxcolumn@widths\fi
    \end{align*}
  }%
  \begingroup
  \def\or{+}\edef\x{\endgroup\global#1=\dimexpr\got@maxcolwd\relax}\x
}
\makeatother

\ExplSyntaxOn

% stores the width of equation
\dim_new:N \g__eqn_width

% the threshold between left equation-aligned and centered equation
\dim_new:N \g_eqn_align_threshold_dim
\dim_gset:Nn \g_eqn_align_threshold_dim {0.6\linewidth}

\DeclareDocumentEnvironment{smartalign*}{+b}{}{
    \settowidthofalign{\g__eqn_width}{#1}
    \dim_compare:nNnTF {\g__eqn_width} < {\g_eqn_align_threshold_dim} {
        \begin{fleqn}[2em]
            \begin{align*}
                #1
            \end{align*}
        \end{fleqn}
    } {
        \begin{align*}
            #1
        \end{align*}
    }
}

\DeclareDocumentEnvironment{smartalign}{+b}{}{
    \settowidthofalign{\g__eqn_width}{#1}
    \dim_compare:nNnTF {\g__eqn_width} < {\g_eqn_align_threshold_dim} {
        \begin{fleqn}[2em]
            \begin{align}
                #1
            \end{align}
        \end{fleqn}
    } {
        \begin{align}
            #1
        \end{align}
    }
}

\ExplSyntaxOff

\begin{document}


\noindent Example 1: 
\begin{smartalign*}
\sum_{i = 1}^{\infty} i = 1 + 2 + 3 + 4 + \dots
\end{smartalign*}

\noindent Example 2:
\begin{smartalign*}
\sum_{i = 1}^{\infty} i = 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + \dots
\end{smartalign*}

\noindent Example 3:
\begin{smartalign*}
\sum_{i = 1}^{\infty} i &= 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + 11 + 12
                               + 13  + \dots \\
                            &= -\frac{1}{12}
\end{smartalign*}

\noindent Example 4:
\begin{smartalign}
\sum_{i = 1}^{\infty} i &= 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + \dots \\
                            &= -\frac{1}{12}
\end{smartalign}

\noindent Example 5:
\begin{smartalign}
\sum_{i = 1}^{\infty} i &= 1 + 2 + 3 + 4 + 5 + \dots \\
                            &= -\frac{1}{12}
\end{smartalign}


\end{document}

相关内容