我想以编程方式创建一个新\NewDocumentEnvironment
环境,使用一个简单的命令来创建新的环境(有点像如何使用\newtheorem
创建一种新类型的环境)。
但是,我不确定该怎么做。我应该使用某种类型的expandafter
, \csname
... 吗?
平均能量损失
\documentclass[]{article}
\usepackage{xparse}
%% Environment generic but too verbose to use. (I need the +b)
\NewDocumentEnvironment{myenv}{O{}m+b}{
Foo (#1) Bar (#2) Baz (#3) Bise}{}
%% Create a command to create new environment to fix the options of the verbose env.
\NewDocumentCommand\newEnv{O{}mm}{
\NewDocumentEnvironment{#1}{O{}+b}{
\begin{myenv}[##1]{#2}
##3
\end{myenv}
}{}
}
%% Use it to create a new environment
\newEnv[dolphin]{myDolpinEnv}{I am a dolphin.}
\begin{document}
\begin{myenv}[I'm optional]{I'm mandatory}
I'm inside.
\end{myenv}
\begin{myDolpinEnv}[blue dolphin]
Psiooouu!
\end{myDolpinEnv}
\end{document}
答案1
好的,我找到了解决方案,我在原来的环境中犯了一些拼写错误。
\documentclass[]{article}
\usepackage{xparse}
%% Environment generic but too verbose to use. (I need the +b)
\NewDocumentEnvironment{myenv}{O{}m+b}{
Foo (#1) Bar (#2) Baz (#3) Bise}{}
%% Create a command to create new environment to fix the options of the verbose env.
\NewDocumentCommand\newEnv{O{}mm}{
\NewDocumentEnvironment{#2}{O{}+b}{
\begin{myenv}[#1,##1]{#3}
##2
\end{myenv}
}{}
}
%% Use it to create a new environment
\newEnv[dolphin]{myDolpinEnv}{I am a dolphin.}
\begin{document}
\begin{myenv}[I'm optional]{I'm mandatory}
I'm inside.
\end{myenv}
\begin{myDolpinEnv}[blue dolphin]
Psiooouu!
\end{myDolpinEnv}
\end{document}