# how to make shortcuts for the start and end of a “gather” environment

I am using sharelatex and found this weird behavior. On the code below, the first two equations don't compile, while the last two do.

Since these ways don't work, can anyone tell me another way to make a shortcut to the gather environment?

\documentclass{article}
\usepackage{amsmath}

\def\bg{\begin{gather}}
\def\eg{\end{gather}}
\newcommand{\bgat}{\begin{gather}}
\newcommand{\egat}{\end{gather}}

\begin{document}\noindent

try \#1
\bgat
2 + 2 = 4\\
\frac{\sqrt{y}}{x^2} = a
\egat

try \#2
\bg
2 + 2 = 4\\
\frac{\sqrt{y}}{x^2} = a
\eg

try \#3
\be
2 + 2 = 4
\ee

try \#4
\begin{gather}
2 + 2 = 4\\
\frac{\sqrt{y}}{x^2} = a
\end{gather}

\end{document}

• The best shortcut would be to configure your editor to type the full environment. – Bernard Jun 7 '17 at 20:43
• Welcome to TeX.SE. – Mico Jun 7 '17 at 20:45
• the amsmath documentation explicitly documents that you can not do this. – David Carlisle Jun 7 '17 at 20:54
• PLEASE DO NOT DO THIS! Anyone reading your code (including you yourself in couple years time) will thank you for it! – yo' Jun 7 '17 at 22:10
• @ivbc it may be obvious to you but it makes it far less obvious to other readers, and it makes it less obvious to your editor, for example I can select an environment or change from align to align* or gather or whatever with a few keystrokes as the editor knows the environment syntax, if you change to \bg, \eg it can not select the environment or syntax highlight the contents as mathematics or do anything useful. – David Carlisle Jun 8 '17 at 6:53

You can use a macro with a delimited argument to do what you want. But as Bernard says, it's probably better to just configure your editor to insert the full environment.

I didn't look too deeply into it, but I suspect that \begin{gather} is looking for \end{gather} explicitly. Since it's being hidden inside your \eg, it doesn't find it.

Using a macro with a delimited argument looks something like this:

\documentclass{article}
\usepackage{amsmath}

\def\bg#1\eg{%
\begin{gather}%
#1%
\end{gather}%
}

\begin{document}

\bg
2+2 = 4\\
\frac{\sqrt{y}}{x^2} = a
\eg

\end{document}


The multi-line display-math environments, such as gather, of the amsmath package are set up to look for explicit, hard-coded termination strings, e.g., \end{gather}, to determine when the end of an environment has been reached. The reason your attempts #1 and #2 cannot work is that LaTeX only "sees" \eg and \egat while it's scanning ahead to locate the end of the gather environment. However, during this scanning-ahead phase, LaTeX does not expand any macros and hence doesn't get a chance to "realize" that these macros will expand to \end{gather}.

Hence, while it's OK to create LaTeX macros to act as abbreviations for \begin{gather} and for the single-line environment equation, a macro-based approach will fail for \egat and \eg. If you are free to use LuaLaTeX, though, you could take a pre-processor approach, as is done in the following example. (In case you're curious about the details: The example code sets up a Lua function which operates at a very early stage of processing. The function scans all input lines and replaces "on the fly" all instances of \eg that occur at the very end of a line with \end{gather}. Note that the instance of \eg in the string \verb+\eg+ does not get operated on by the Lua function.)

% !TeX program = lualatex
\documentclass{article}
\usepackage{amsmath,luacode}

%% OK to create LaTeX shortcut macros for "equation" env.
%% OK to create LaTeX macro to abbreviate "\begin{gather}"
\newcommand{\bg}{\begin{gather}}

%% Preprocessor approach to handle abbrev. for "\end{gather}"
\begin{luacode}
function do_gather ( s )
return s:gsub ( "\\eg\$" , "\\end{gather}" )
end
luatexbase.add_to_callback ( "process_input_buffer", do_gather , "do_gather" )
\end{luacode}

\begin{document}

\noindent
\verb+gather+ environment: \verb+\bg+ and \verb+\eg+
\bg
2 + 2 = 4\\
\frac{\sqrt{y}}{x^2} = a
\eg
\verb+equation+ environment: \verb+\be+ and \verb+\ee+
\be
2 + 2 = 4
\ee
\end{document}

• Thank you for the answer Mico! I chose TH's because he's was simpler and didnt require LuaTex. But yours works too! thank you!!! – ivbc Jun 7 '17 at 23:33
• @ivbc -- just remember that if you do this, never try to submit the file in that condition to a journal for publication. since most journals will not be using lua(la)tex for at least a few years (it's not yet sufficiently stable for their needs) this will fail, and it will no doubt be sent back to you for repairs. – barbara beeton Jun 8 '17 at 2:38

If the problem is to define a custom environment that one can redefine, once and for all, as, say, either gather or gather*, as the OP noted in one of her/his comments (switching from gather to align, or vice versa, seems much less sensible, of course!), it should be noted that this—I repeat, for an environmentis possible, thanks to the code that Michael J. Downes wrote some fifteen/twenty years ago and that, after having being integrated into the amsmath package, formed the base of the environs package. Look at the following code for a working example:

% My standard header for TeX.SX answers:
\documentclass[a4paper]{article} % To avoid confusion, let us explicitly
% declare the paper format.

\usepackage[T1]{fontenc}         % Not always necessary, but recommended.
% End of standard header.  What follows pertains to the problem at hand.

\usepackage{amsmath}

\newenvironment*{mygather}{\gather}{\endgather}
% \newenvironment*{mygather}{\csname gather*\endcsname}{\endgather}
\newenvironment*{myalign}{\align}{\endalign}
% \newenvironment*{myalign}{\csname align*\endcsname}{\endalign}

\begin{document}

A customized \texttt{gather} environment:
\begin{mygather}
1+1+1+1 = 4\\
\frac{\sqrt{y}}{x^2} = a
\end{mygather}

A customized \texttt{align} environment:
\begin{myalign}
1+1+1+1 &= 4\\
\frac{\sqrt{y}}{x^2} &= a
\end{myalign}

\end{document}


You can comment out the two lines that say

\newenvironment*{mygather}{\gather}{\endgather}


and

\newenvironment*{myalign}{\align}{\endalign}


and uncomment in their place those that read, respectively,

% \newenvironment*{mygather}{\csname gather*\endcsname}{\endgather}


and

% \newenvironment*{myalign}{\csname align*\endcsname}{\endalign}


and check that everything works as expected.

The following points are worth remarking:

1. It is not necessary to say, for example,

\newenvironment*{myalign}
{\csname align*\endcsname}
{\csname endalign*\endcsname}


because \csname endalign*\endcsname and \endalign, as well as \csname endgather*\endcsname and \endgather, are perfect synonyms.

2. It would probably seem more obvious to write something like

\newenvironment*{mygather}{\begin{gather}}{\end{gather}}


but this does not work.

3. The benefit of a markup like, say,

\begin{mygather} ... \end{mygather}


over

\bgather ... \egather


is that the former integrates into LaTeX’s environment system, taking advantage of its name-checking facilities; thus, if you misspell the name mygather in the \end declaration, you’ll get a meaningful error message. On the other hand, if you misspell an argument delimiter like \egather, (La)TeX will swallow up the rest of your file and then, very likely, halt with some mysterious error message. (Of course, we are assuming that it is much more unlikely to misspell the \end keyword itself, also because many editors will supply it automatically.)