Centered left aligned `gather` environment

amsmathmath-mode

I know that if you pass fleqn as an option, all amsmath's environments becomes left aligned, including gather, however, along with that, they become flushleft.

Actually, what I'm trying to create is a left aligned gather environment that would be horizontally centered relatively to the page. fleqn is not what I need, because I want to keep the default behavior of environments, but I found a way to temporarily turn this option on. The question is about how I can center my leftceq environment without using additional packages as etoolbox?

There is a dirty workaround with align environment that makes me put & at the beginning of each line that's drastically inconvenient with lots of math equations.

enter image description here

\documentclass{article}
\usepackage{mathtools}
\mathtoolsset{showonlyrefs=true}
\usepackage{showframe}

\makeatletter
\newenvironment{leftceq}{
\@fleqntrue%
\gather%
}{%
\endgather
}
\makeatother


\begin{document}
\verb|my environment|
\begin{leftceq}
y_1 = C_1e^{-4x}+C_2e^{-5x}+C_3xe^{-5x}\\
y_2 = C_1e^{6x}+C_2e^{-9x}\cos x+C_3e^{-9x}\sin x
\end{leftceq}\\

\verb|desired result, faked with align environment|
\begin{align}
&y_1 = C_1e^{-4x}+C_2e^{-5x}+C_3xe^{-5x}\\
&y_2 = C_1e^{6x}+C_2e^{-9x}\cos x+C_3e^{-9x}\sin x
\end{align}\\

\verb|default gather|
\begin{gather}
y_1 = C_1e^{-4x}+C_2e^{-5x}+C_3xe^{-5x}\\
y_2 = C_1e^{6x}+C_2e^{-9x}\cos x+C_3e^{-9x}\sin x
\end{gather}

\end{document}

Best Answer

Use the “fake” but making it automatic.

\documentclass{article}
\usepackage{amsmath}

\ExplSyntaxOn

\NewDocumentEnvironment{leftceq}{b}
 {
  \antshar_leftceq:nn { } { #1 }
 }
 {}
\NewDocumentEnvironment{leftceq*}{b}
 {
  \antshar_leftceq:nn { * } { #1 }
 }
 {}

\seq_new:N \l__antshar_leftceq_seq

\cs_new_protected:Nn \antshar_leftceq:nn
 {
  \seq_set_split:Nnn \l__antshar_leftceq_seq { \\ } { #2 }
  \begin{align#1}
  & \seq_use:Nn \l__antshar_leftceq_seq { \\ & }
  \end{align#1}
 }

\ExplSyntaxOff

\begin{document}

Numbered
\begin{leftceq}
y_1 = C_1e^{-4x}+C_2e^{-5x}+C_3xe^{-5x}\\
y_2 = C_1e^{6x}+C_2e^{-9x}\cos x+C_3e^{-9x}\sin x
\end{leftceq}

Unnumbered
\begin{leftceq*}
y_1 = C_1e^{-4x}+C_2e^{-5x}+C_3xe^{-5x}\\
y_2 = C_1e^{6x}+C_2e^{-9x}\cos x+C_3e^{-9x}\sin x
\end{leftceq*}

\end{document}

This splits the environment's content at \\ and places & in front of every line when passing the lines to align.

enter image description here

Bad version asked in comments

\ExplSyntaxOn
\NewDocumentEnvironment{leftceq}{b}
 {
  \seq_set_split:Nnn \l_tmpa_seq { \\ } { #1 }
  \begin{align*} & \seq_use:Nn \l_tmpa_seq { \\ & } \end{align*}
 }{}
\ExplSyntaxOff

I cannot recommend doing this, because if it turns out that the numbered version is necessary, one has to duplicate the code. And separating the user interface from the internal code is a plus, besides being the recommended way.


If you want to support nested alignments such as cases, I'm afraid you have to do it the hard way.

This is a fast (and quite likely it can be improved) modification of the amsmath code.

\documentclass{article}
\usepackage{amsmath,showframe}

\makeatletter

\newenvironment{leftceq}{%
  \start@leftceq\@ne\st@rredtrue\m@ne
}{\endalign}

\def\start@leftceq#1#2#3{%
    \let\xatlevel@#1% always \z@, \@ne, or \tw@
    \maxfields@#3\relax
    \ifnum\maxfields@>\m@ne
        \checkat@true
        \ifnum\xatlevel@=\tw@
            \xxat@true
        \fi
        \multiply\maxfields@\tw@
    \else
        \checkat@false
    \fi
    \ifingather@
        \iffalse{\fi\ifnum0=`}\fi
        \DN@{\vcenter\bgroup\savealignstate@\leftceq@#2}%
    \else
        \ifmmode
          \if@display
             \DN@{\align@recover}%
          \else
            \nomath@env
            \DN@{\@namedef{end\@currenvir}{}\@gobble}%
          \fi
        \else
            $$%
            \let\split\insplit@
            \DN@{\leftceq@#2}%
        \fi
    \fi
    \collect@body\next@
}
\def\leftceq@#1#2{%
    \inalign@true \intertext@ \Let@ \chardef\dspbrk@context\z@
    \ifingather@\else\displ@y@\fi
    \let\math@cr@@@\math@cr@@@align
    \ifxxat@\else \let\tag\tag@in@align \fi
    \let\label\label@in@display
    #1% set st@r
    \ifst@rred\else \global\@eqnswtrue \fi
    \measure@{#2}%
    \global\row@\z@
    \tabskip\eqnshift@
    \halign\bgroup
        \span\leftceq@preamble\crcr
        #2%
}
\def\leftceq@preamble{%
   &\setboxz@h{\@lign$\m@th\displaystyle{{}##}$}%
    \ifmeasuring@\savefieldlength@\fi
    \set@field
    \hfil
    \tabskip\alignsep@
}

\makeatletter

\begin{document}

\begin{leftceq}
y_1 = C_1e^{-4x}+C_2e^{-5x}+C_3xe^{-5x}\\[2ex]
y_2 = C_1e^{6x}+C_2e^{-9x}\cos x+C_3e^{-9x}\sin x \\[4ex]
\begin{cases} a & x \\ b & y \end{cases} \\
y_1 = C_1e^{-4x}+C_2e^{-5x}+C_3xe^{-5x}\\
y_2 = C_1e^{6x}+C_2e^{-9x}\cos x+C_3e^{-9x}\sin x
\end{leftceq}

\end{document}

enter image description here