# Difficulty in creating macro '\newcommand' for the beginning and end of frames in Beamer

I would like to create a Latex macro for my beamer presentations. I use beamer frequently, and want to make macros for the \begin{frame} and \end{frame} to require fewer keystrokes. At the beginning of the document I have:

\newcommand{\bfrm}{\begin{frame}}%start new frame
\newcommand{\efrm}{\end{frame}}%end new frame


Which compiles fine. I then test the first of the two macros for the beginning of the frame:

\bfrm{New frame testing}
blah blah
\end{frame}


Which produces the result I am looking for. I then test the second of the macros:

\bfrm{New frame testing}
blah blah
\efrm


Fails to compile, saying that there is a runaway argument. What is the reason for the failure?

• In general you should not make short cuts fir environments like this. Leave them as environments, it makes the code much much easier to read. If you are annoyed at the amount of typing, then look into what ever capabilities your editor has. Odds are you can even define a two letter shortcut that would insert the entire \begin/end{frane}  combo – daleif Aug 27 '16 at 18:35
• While this might work (but is not a good idea) with ordinary environments a frame is a very complex organism. Redefining it will lead to many problems, we already had plenty of such questions which demonstrated these problems. – user36296 Aug 27 '16 at 18:51
• To reiterate a previous comment, it's generally better to find a way for your editor to input the \begin{frame}/\endframe in a reduced number of key strokes - this need take no more effort but leaves documents more readable with less chance of breaking anything. A pretty comprehensive list of editors can be found at LaTeX Editors/IDEs – Dai Bowen Aug 27 '16 at 19:02
• Some environments collect everything from the \begin{XXX} up to the final \end{XXX} command so hiding' that \end{XXX} is a bad idea. Also, what Editor are you using? Most Editors used for writing and typesetting .tex documents have a way to quickly enter commands so you can save lots of wordy typing. – Herb Schulz Aug 27 '16 at 21:43
• @Vass With emacs, you can use AUCTeX and its interactive command LaTeX-environment (binded to C-c C-e) to insert automagically environments... – Paul Gaborit Aug 28 '16 at 6:00

You could do it by doing

\newcommand\bfrm[1][]{%
\if\relax\detokenize{#1}\relax
\def\startfrm{\begin{frame}}%
\else
\def\startfrm{\begin{frame}[#1]}%
\fi
\innerbfrm
}
\long\def\innerbfrm#1\efrm{\startfrm#1\end{frame}}


However, I see no advantage whatsoever in typing

\bfrm
The frame text
\efrm


over

\begin{frame}
The frame text
\end{frame}


I only see code obfuscation. The time used for typing in the full tags is negligible in comparison to the time necessary for writing the frame text.

Any good editor provides methods for easily typing in common parts; with Emacs, you just type

Ctrl+c , Ctrl+e , f , r , a , m , e , Enter

in the document you get

\begin{frame}

\end{frame}


with the cursor in between the tags. Ten keys, just the same as \bfrm\efrm, but you don't risk forgetting \efrm.

You have been doing probably many of us intuitively tried and failed similarly. Apart from the environment concept, which is a fairly intertwined TeX concept, this, I guarantee you in any other environment trial too, would cost you unnecessary time desparately debugging the reason, which is the same time you think you have saved by avoiding the actual syntax.

The main problem, if I may take a shortcut to avoid a lengthy and possibly above my paygrade discussion, is that the parser wants to see an explicit well-defined end point. If you like, you can think of the analogy

\def\mymacro#1!{}


would fail when we use it as follows

\def\myarg{a!!!}
\mymacro{\myarg}


even though there are plenty of exclamation marks to detect at least one. Well that's an expansion issue, I hear you say then can you guess what the following would give before you try ?

\expandafter\mymacro\expandafter{\myarg}


Very similar attempts are common to avoid the brace syntax using shortcuts and hacks trying to replace them with \bgroup,\egroup pairs etc. I feel for them.

In short TeX does not work as you think that would make sense (good or bad is not my argument). Let me reemphasize my point:

There is no shortcut in TeX that would save you time. In the long run the house always wins.

This doesn't mean I like TeX syntax. I'm just passing the torch. If you are really bothered by the pair version, then prefer \frame{....} to the environment syntax. But then you need to add \frametitle{...} (I think).

As Joseph Wright reminds, don't use \frame{...} either. Instead find a good IDE with autocomplete.

• Please don't use \frame{...}: it's there only because it was the original beamer syntax and is retained for backward compatibility. – Joseph Wright Aug 28 '16 at 6:48
• (+1) for the "In the long run the house always wins." – user36296 Aug 28 '16 at 11:05

I posted this answer using TeXstudio before realizing that the OP is using emacs. Nevertheless, I will make it informative for anyone else comes across it in the future.

1- Using Keyboard Shortcuts

Go to Macros > Edit Macros > + Add > Environment, then type %frame, which asks TeXstudio to create a \begin{frame} and \end{frame}. Of course, frame can be replaced by whatever environment you want to create.

The shortcut to this macro can be changed by going to Configure TeXstudio > Shortcuts

2- Defining a New Command

Referring to the Edit Macros window, under the Name field, there is a field called Abbreviation in which you define any arbitrary command such as \myframe.

Whenever you finish typing this command \myframe, TeXstudio will automatically create a frame environment. You can press enter, select it from the pop-up menu or press ctrl + space to complete the command and place the environment.

3- Defining a Trigger

The third field Trigger accepts a typical text which can be, for example, putframe. Whenever you complete typing putframe by inserting the letter e, TeXstudio swiftly replaces putframe with the frame environment.

There is also more advanced information in TeXstudio manual for the other macro types.

• The OP is using emacs`. – cfr Aug 28 '16 at 3:36
• @cfr I hope it would help somebody else :) – Diaa Aug 28 '16 at 6:33