使用新环境时缺少 } 插入

使用新环境时缺少 } 插入
\begin{block}{Command} \newcolumntype{R}{>{\raggedleft\arraybackslash}X}
    \begin{tabularx}{\textwidth}{*{2}{@{}l}@{}R}
        1 & 2 & 3
    \end{tabularx}
\end{block}

因为这有点麻烦,所以我想用它\newenvironment来让事情变得更容易。

\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}

这是我尝试过的,但是没有用,错误信息是

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

已编辑

根据@TeXnician提到的帖子,我尝试过

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

并且确实有效。但是,如果\begin{block}\end{block}添加,则会弹出警告:

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

答案1

您可以尝试使用 environment 包:

\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}

enter image description here

更新3/2019:

它还适用于新版本 xparser 及其新的b-type 参数:

\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}

相关内容