For some reason I want a macro \BeginControl such that \BeginControl{\environment} is equivalent to \begin{environment}. I have put together some code. It usually works but there is a problem with starred environments I'm trying to solve.
Consider the following code. The two instances of align* environment should behave the same, but it yields a wrong looking error ! LaTeX Error: \begin{align*} on input line 33 ended by \end{align*}. Where is the problem?
\documentclass{article}
\usepackage{amsmath}
\newcommand*{\cdef}{\newcommand*}
\cdef \Expanded [1]{%
\begingroup
\edef \x {%
\endgroup
#1%
}%
\x
}
\cdef \Apply [1]{%
\Expanded{\noexpand #1}%
}
\cdef \IgnoreNext [1]{%
% ignore next token
}
\cdef \MacroName [1]{%
\expandafter\IgnoreNext \string#1%
}
\cdef \BeginControl [1]{%
\Apply{\begin{\MacroName{#1}}}%
}
\begin{document}
\begin{align*}
A \\
B
\end{align*}
\BeginControl{\align*}
A \\
B
\end{align*}
\end{document}
Update: Thanks to the answers I found out that my macro \MacroName is incorrect since it expands to the macro name with catcode 12 instead of 11 due to \string. The following definition should work:
\let \Ex \expandafter
\cdef \ExEx {%
\Ex\Ex\Ex
}
\cdef \ExExArg [2]{%
\ExEx#1\ExEx{#2}%
}
\cdef \MacroName [1]{%
\ExExArg\scantokens{\Ex\IgnoreNext \string#1\noexpand}%
}
\BeginControl{\align}would produce the same error. – egreg Sep 06 '15 at 16:06\align. The macros work ok for theorems and itemize. If I remember I had also similar problem with frame environment in beamer. – user87690 Sep 06 '15 at 16:13itemizeeither. What's the purpose of having\BeginControl{\foo}instead of\begin{foo}? – egreg Sep 06 '15 at 16:26\cdef \EndCurrent {\Apply{\end{\@currenvir}}}. But with theorems and itemize, it seemed to work with no problem. – user87690 Sep 06 '15 at 16:59alignis special: it needs to see\end{align}explicitly, not buried in a macro. See, for instance, http://tex.stackexchange.com/questions/196238/newenvironment-does-not-work – egreg Sep 06 '15 at 17:54amsmathenvironment you're doomed, unless you redefine them all to look for\EndCurrentinstead of\end{<environment>}. – egreg Sep 06 '15 at 18:03