# How to avoid a spurious space when combining frame, centering and another environment?

I want to create a special environment in beamer, which combines several environments, one of which is center. However, when using my newly created environment, I get an additional space in comparison to using the commands manually.

Here is a minimal example:

\documentclass[20pt]{beamer}

\newenvironment{myenv}
{\begin{frame}[fragile,environment=myenv]\begin{center}\begin{tabular}{c}}
{\end{tabular}\end{center}\end{frame}}

\begin{document}

\begin{myenv}%
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx%
\end{myenv}

\begin{frame}\begin{center}\begin{tabular}{c}%
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx%
\end{tabular}\end{center}\end{frame}

\end{document}


In the second frame, the text is centred as intended; in the first frame it is shifted to the left as if it contained an additional space at the end.

The same happens with some environments other than tabular but not if I do not nest another environment inside center. Using \ignorespacesafterend does not help.

As stated in the Beamer manual v3.33 at the end of page 62, when trying to use a frame environment inside \newenvironment, the ending must end with \end{frame} and not contain any other \end command, since the actual mechanics are somewhat sensitive. So in order to add additional environments, the manual suggests to define a separate new command to contain it all.

Here is a modified version based on your example.

\documentclass[20pt]{beamer}

\newenvironment{myenv}
{\begin{frame}\startmyenv}
{\stopmyenv\end{frame}}

\newcommand\startmyenv{\begin{center}\begin{tabular}{c}}
\newcommand\stopmyenv{\end{tabular}\end{center}}

\begin{document}

\begin{myenv}
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
\end{myenv}

\begin{frame}\begin{center}\begin{tabular}{c}
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
\end{tabular}\end{center}\end{frame}

\end{document}


The output of the above example should render two identical frames without spurious spaces.

As I just found out, using fragile=singleslide (instead of just fragile) as an argument of frame solves this particular issue too. However, the accepted answer is likely to fix other issues as well and arguably should be the preferred way to do it.