TransWikia.com

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

TeX - LaTeX Asked on December 31, 2020

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}

enter image description here

One Answer

Maybe this solution works.

enter image description here

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]{%
  setboxz@=vbox{
    begin{align*}
    #2
    ifmeasuring@elsegloballetgot@maxcolwdmaxcolumn@widthsfi
    end{align*}
  }%
  begingroup
  defor{+}edefx{endgroupglobal#1=dimexprgot@maxcolwdrelax}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.6linewidth}

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}

Correct answer by Alan Xiang on December 31, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP