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

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.


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.


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.

enter image description here

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

enter image description here


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.

enter image description here

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.

enter image description here


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.