2

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}
4
  • 1
    Why do you call the middle result "faked"? Why do you call it a "dirty workaround"? There's absolutely nothing improper, fake, or dirty about using the align machinery in that manner.
    – Mico
    Nov 13, 2021 at 10:11
  • @Mico as you might know, if you don't put any & inside align, everything becomes right aligned. For me it's really weird, why it's not left aligned by default, so I have to create such an environment by myself. Also, I mentioned in the post why I don't like align approach, that's why I called it a 'facked solution'. I want to achieve such a result without spamming & on each and every line.
    – antshar
    Nov 13, 2021 at 10:16
  • @antsher - You ask, "why [is align] not left aligned by default"? I believe that the default (viz., right-alignment if no & alignment point is provided) is is actually entirely reasonable at it greatly simplifies entering equations that actually conform to the expected use of align, which is to employ one or more & alignment points (usually, but not necessarily, placed before = symbols): the material up to that alignment point is right-aligned, and everything after that point left-aligned. That is exactly what most users would want to happen. Nothing fake about it.
    – Mico
    Nov 13, 2021 at 10:41
  • This isn't a good example to illustrate why you don't like the align environment. Since both y_1 and y_2 have the same width, the usual use of align with & preceding the = sign will have the exact same result. With an example where these left-of-= elements don't have the same width, and you don't want the = signs aligned, it would be easier to understand your problem. Nov 13, 2021 at 19:31

2 Answers 2

3

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

13
  • I guess, this solution requires xparse package. But, actually, I don't mind it, because it's already included in my main project. Thank you for the solution, but I'd to ask two things: 1) I prefer to have minimalistic, compact code (I mean a simpler solution, rather that removing every space character to fit in one line), so is it possible to put everything together inside NewDocumentEnvironment? 2) Is it possible to tweak my attempt so that it will work as well?
    – antshar
    Nov 13, 2021 at 10:30
  • 2
    @antshar If you need to load xparse, then you're running a version of LaTeX prior to 2020-10-01. I'm not sure what you mean about putting everything inside \NewDocumentEnvironment. It can be done, but why? Separating user interface from internal code is a plus. Your attempt cannot work, because with fleqn there is no centering at all.
    – egreg
    Nov 13, 2021 at 10:35
  • I meant how I can make all the replacement's inside brackets: \NewDocumentEnvironment{leftceq}{b}{<\\ replaces with \\& here>}{}. Current solution spreads beyond environment definition.
    – antshar
    Nov 13, 2021 at 10:47
  • @antshar Sorry, but I can't understand. With your approach you'd need code duplication for leftceq and leftceq*. Why would you? The way I used is actually the recommended way. The code will most likely sit in some external file that you \input in the preamble (or a personal package).
    – egreg
    Nov 13, 2021 at 10:49
  • Actually, I don't need a starred version of the environment. if you don't mind, this way would be the best for my needs.
    – antshar
    Nov 13, 2021 at 10:54
0

After long researches and collecting information bit by bit, found the solution.

They key is to turn fleqn for gathered instead of gather and then put it inside \[ \] that will make it centered. However, unfortunately, a direct replacement of \gather ... \endgather with \gathered ... \endgathered doesn't work, because gathered doesn't seem to support option fleqn. Luckily, I found this egreg's answer that points out how change it the way it begins to work.

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

\makeatletter
\renewenvironment{gathered}[1][c]{%
    \RIfM@\else
        \nonmatherr@{\begin{gathered}}%
    \fi
    \null\,%
    \if #1t\vtop \else \if#1b\vbox \else \vcenter \fi\fi \bgroup
        \Let@ \chardef\dspbrk@context\@ne \restore@math@cr
        \spread@equation
        \ialign\bgroup
            %%% In the original there is just \hfil
            \if@fleqn\else\hfil\fi\strut@$\m@th\displaystyle##$\hfil
            \crcr
}{%
  \endaligned
}


\newenvironment{leftceq}{
\[\bgroup%
\@fleqntrue%
\begin{gathered}%
}{%
\end{gathered}\hskip1.5pt%
\egroup\]%
}

\makeatother

\begin{document}

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

\verb|gathered in mathmode|
\[
\begin{gathered}
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{gathered}
\]

\end{document}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.