4

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}}
\newcommand{\be}{\begin{equation}}
\newcommand{\ee}{\end{equation}}

\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}
Mico
  • 506,678
ivbc
  • 165
  • 6
    The best shortcut would be to configure your editor to type the full environment. – Bernard Jun 07 '17 at 20:43
  • 1
    Welcome to TeX.SE. – Mico Jun 07 '17 at 20:45
  • 6
    the amsmath documentation explicitly documents that you can not do this. – David Carlisle Jun 07 '17 at 20:54
  • What is the purpose of your "shortcut"? – Moriambar Jun 07 '17 at 21:04
  • 5
    PLEASE DO NOT DO THIS! Anyone reading your code (including you yourself in couple years time) will thank you for it! – yo' Jun 07 '17 at 22:10
  • @DavidCarlisle, would you mind supplying a link to the page that says that? I'm a noob at latex and I'm having difficulties finding it on my own. Thanks. – ivbc Jun 07 '17 at 23:29
  • @yo', thanks for the warning, I can totaly see where you are coming from. In this case I'll take my chances for it is obviously a mathematical enviroment wrapping around most equations. – ivbc Jun 07 '17 at 23:35
  • you will have it already texdoc technote will open the documentation then see section 6 "why can't I use abbreviations"... or see the file here but it is a bad idea to hide environment syntax even for environments where it works. – David Carlisle Jun 08 '17 at 06:51
  • 2
    @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 08 '17 at 06:53
  • @DavidCarlisle, I usualy play a lot with my enviroments changing Align to Gather and adding or removing *. It's annoying to have to hunt down the "\end"s and copy-paste the new enviroment. You seem to imply there is a better way to do it... whats it? – ivbc Jun 08 '17 at 14:51
  • @ivbc: Oh, but this is a completely different problem! It is possible to define a custom environment that you can redefine, once and for all, as, say, either gather or gather*; indeed, while \newenvironment*{bg}{\begin{gather}}{\end{gather}} does not compile, \newenvironment*{bg}{\gather}{\endgather} works as expected, and you can subsequently change the definition to \newenvironment*{bg}{\csname gather*\endcsname}{\endgather} (note that \csname endgather*\endcsname is the same as \endgather). Are you interested in an answer going in this directon? – GuM Jun 08 '17 at 15:07
  • @GustavoMezzetti, hi! I am interest yes. But would you mind adding some explanantion to why this would be better than, say, TH's method? – ivbc Jun 08 '17 at 15:49
  • it depends on your editor, auctex (emacs) has commands to change the name of the current enviornment, or add environments, so it would take less keystokes to add \begin{align} \end{align} than \ba got to end ... \ea – David Carlisle Jun 08 '17 at 16:05
  • @ivbc: The benefit is that a markup like, say, \begin{myg} ... \end{myg} integrates into LaTeX’s environment system, taking advantage of its name-checking facilities; thus, if you misspell the name myg in the \end declaration, you’ll get a meaningful error message. On the other hand, if you misspell an argument delimiter, (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 editor will supply it automatically.) – GuM Jun 08 '17 at 20:39

3 Answers3

5

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.)

enter image description here

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

%% OK to create LaTeX shortcut macros for "equation" env.
\newcommand{\be}{\begin{equation}}
\newcommand{\ee}{\end{equation}}
%% 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}
Mico
  • 506,678
  • 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 07 '17 at 23:33
  • 2
    @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 08 '17 at 02:38
3

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}
TH.
  • 62,639
2

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.)

GuM
  • 21,558