Missing } inserted when using newenvironment

\begin{block}{Command} \newcolumntype{R}{>{\raggedleft\arraybackslash}X}
\begin{tabularx}{\textwidth}{*{2}{@{}l}@{}R}
1 & 2 & 3
\end{tabularx}
\end{block}


Since this is a bit cumbersome, I want to use \newenvironment to make things easier.

\documentclass[10pt]{beamer}

\usepackage{tabularx}

\newenvironment{Command}[1]
{\begin{block}{Command}%
\newcolumntype{R}{>{\raggedleft\arraybackslash}X}%
\begin{tabularx}{\textwidth}{*{#1}{@{}l}@{}R}%
}{  \end{tabularx}%
\end{block}}

\begin{document}

% THIS IS CORRECT
% \begin{block}{Command} \newcolumntype{R}{>{\raggedleft\arraybackslash}X}
%     \begin{tabularx}{\textwidth}{*{2}{@{}l}@{}R}
%         1 & 2 & 3
%     \end{tabularx}
% \end{block}

% THIS IS INCORRECT
\begin{Command}{2}
1 & 2 & 3
\end{Command}

\end{document}


This is what I've tried, but it doesn't work, and the error message is

! Missing } inserted.
<inserted text>
}
l.nn        \end{tabularx}


EDITED

According to the thread @TeXnician mentioned, I tried

\newenvironment{Command}[1]
{   \newcolumntype{R}{>{\raggedleft\arraybackslash}X}%
\tabularx{\textwidth}{#1}%
}{  \endtabularx}


and it does work. However, if \begin{block}\end{block} are added, the warning pops up:

! Undefined control sequence.
\endbeamercolorbox ->\ifdim \beamer@colbox@sep
=0pt\else \vskip \beamer@colb...
l.nn ^^I\end{block}

• What packages? What packages! This is why a complete MWE and not just code snippets are vital. – Steven B. Segletes Aug 21 '17 at 16:09
• You can't hide tabularx like this in an environment. See page 4 of the documentation. – Ulrike Fischer Aug 21 '17 at 16:14
• @StevenB.Segletes I use package beamer. – Kevin Dong Aug 21 '17 at 16:15
• @UlrikeFischer Thanks. The solution is in page 4. Use \tabularx{XX} and \endtabularx instead of \begin{tabularx}{XX} and \end{tabularx}. – Kevin Dong Aug 21 '17 at 16:17
• Possible duplicate of How to include tabularx/y in a new environment? – TeXnician Aug 21 '17 at 16:21

You can try with the environ package:

\documentclass[10pt]{beamer}
\usetheme{warsaw}
\usepackage{tabularx}
\newcolumntype{R}{>{\raggedleft\arraybackslash}X}
\usepackage{environ}

\NewEnviron{Command}[1]
{\begin{block}{command} %
\begin{tabularx}{\textwidth}{*{#1}{@{}l}@{}R}%
\BODY
\end{tabularx}
\end{block}
}

\begin{document}

\begin{frame}

\begin{Command}{2}
1 & 2 & 3 \\
abc & abc & abc
\end{Command}

\end{frame}

\end{document}


Update 3/2019:

It also works with the new xparse version and its new b-type argument:

\documentclass[10pt]{beamer}
\usetheme{warsaw}
\usepackage{tabularx}
\newcolumntype{R}{>{\raggedleft\arraybackslash}X}

\usepackage{xparse}
\NewDocumentEnvironment{Command}{m b}
{\begin{block}{command} %
\begin{tabularx}{\textwidth}{*{#1}{@{}l}@{}R}%
#2
\end{tabularx}
\end{block}
}

\begin{document}

\begin{frame}

\begin{Command}{2}
1 & 2 & 3 \\
abc & abc & abc
\end{Command}

\end{frame}

\end{document}