2

I guess I want to define a new environment, but perhaps it already exists and I didn't know what to search for.

The environment I want should leave the equations center aligned if no & is present and should keep the = signs aligned if present.

\documentclass{article}
\begin{document}

\begin{customEnv}%behave like gather
   a=1,\\
   b=1111111111111111111111111111.
\end{customEnv}

\begin{customEnv}%behave like align
   a&=1,\\
   b&=1111111111111111111111111111.
\end{customEnv}

\end{document}

PS: Bonus points if we can use some parameter to change the environment's behavior when without &, so that it defaults to center, left or right depending on this parameter!!!

PPS: If possible try to use only the standar packages available.

0

2 Answers 2

3

Are you sure you gain something? I'm not. For instance, you give up being able to nest align or aligned in gather.

\documentclass{article}
\usepackage{amsmath}
\usepackage{xparse,environ}

\ExplSyntaxOn
\NewEnviron{customEnv}
 {
  \tl_if_in:NnTF \BODY { & }
   {
    \begin{align}\BODY\end{align}
   }
   {
    \begin{gather}\BODY\end{gather}
   }
 }
\ExplSyntaxOff

\begin{document}

\begin{customEnv}%behave like gather
   a=1,\\
   b=1111111111111111111111111111.
\end{customEnv}

\begin{customEnv}%behave like align
   a&=1,\\
   b&=1111111111111111111111111111.
\end{customEnv}

\end{document}

enter image description here

1
  • I am a new user and so far have not had the need to nest align with gather. Do you see that often?
    – ivbc
    Commented Jun 19, 2017 at 17:42
1

Here, I use TABstacks. I use the \readTABstack feature to absorb the data and analyze the input. The \TABcells feature to tell me how many columns there are in row 1. If 1, I use a \Centerstack, else an \alignCenterstack.

However, I must add the disclaimer that a TABstack will not number each equation uniquely.

\documentclass{article}
\usepackage{tabstackengine}
\newcommand\mystack[2][c]{%
  \readTABstack{#2}%
  \ifnum\TABcells{1}=1\relax%
    \Centerstack[#1]{#2}%
  \else%
    \alignCenterstack{#2}%
  \fi%
}
\stackMath
\begin{document}
\[\mystack[l]{%behave like gather
   W=1,\\
   b=1111111111111111111111111111.
}\]
\[\mystack[c]{%behave like gather
   W=1,\\
   b=1111111111111111111111111111.
}\]
\[\mystack[r]{%behave like gather
   W=1,\\
   b=1111111111111111111111111111.
}\]
\[\mystack{%behave like align
   W=&1,\\
   b=&1111111111111111111111111111.
}\]
\end{document}

In the above MWE, three separately aligned (left, center, right) non-tabbed stacks are shown, followed by the input using aligning tabs.

enter image description here

2
  • your result is exactly what I want, but unfortunately the package is not recognized on my PC nor on sharelatex. It could cause some problems when collaborating. I will update the question.
    – ivbc
    Commented Jun 19, 2017 at 17:46
  • @ivbc ctan.org/pkg/tabstackengine Commented Jun 19, 2017 at 17:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .