我想定义一种使用符号将参数传递给环境的便捷方法key=value
。我不知道如何在文档中找到它。
举个例子:
\newenvironment{foobar}[3]{
...
}
\begin{foobar}[foo=1,bar=2,baz=3]
text1
\end{foobar}
\begin{foobar}[bar=10]
text2
\end{foobar}
结果可能是:
begin_foobar
you said that foo equals 1, bar equals 2 and baz equals 3
text1
end_foobar
begin_foobar
you said that bar equals 10
text2
end_foobar
(如果您对 LaTeX 管道方面的优秀文档有任何建议,我会采纳)
答案1
下面是一个你可以使用的示例keyval
包裹:
\documentclass{article}
\usepackage{environ}
\usepackage{keyval}
\makeatletter
\define@key{myenv@keys}{foo}{\def\myenv@foo{#1}}%
\define@key{myenv@keys}{bar}{\def\myenv@bar{#1}}%
\newenvironment{myenv}[1][]{%
\setkeys{myenv@keys}{foo=4,bar=4,#1}
Foo=\myenv@foo\par
Bar=\myenv@bar\par
Before\par
}{
After
}
\makeatother
\begin{document}
\begin{myenv}[foo=42]
Inside
\end{myenv}
\begin{myenv}[bar=42]
Inside
\end{myenv}
\end{document}
答案2
和expl3
:
\documentclass{article}
\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentEnvironment{foobar}{O{}}
{
\par
\nowox_foobar_init:n { #1 }
}
{
\par
}
\keys_define:nn { nowox/foobar }
{
foo .tl_set:N = \l_nowox_foobar_foo_tl,
bar .tl_set:N = \l_nowox_foobar_bar_tl,
baz .tl_set:N = \l_nowox_foobar_baz_tl,
}
\seq_new:N \l__nowox_foobar_args_seq
\cs_new_protected:Nn \__nowox_foobar_arg_add:nn
{
\tl_if_empty:nF { #2 }
{
\seq_put_right:Nn \l__nowox_foobar_args_seq
{
#1~equals~#2
}
}
}
\cs_generate_variant:Nn \__nowox_foobar_arg_add:nn { nV }
\cs_new_protected:Nn \nowox_foobar_init:n
{
\keys_set:nn { nowox/foobar } { #1 }
\__nowox_foobar_arg_add:nV {foo} \l_nowox_foobar_foo_tl
\__nowox_foobar_arg_add:nV {bar} \l_nowox_foobar_bar_tl
\__nowox_foobar_arg_add:nV {baz} \l_nowox_foobar_baz_tl
\seq_if_empty:NF \l__nowox_foobar_args_seq
{
you~said~that~\seq_use:Nnnn \l__nowox_foobar_args_seq { ~and~ } { ,~ } { ~and~ } \par
}
}
\ExplSyntaxOff
\begin{document}
\begin{foobar}[foo=1,bar=2,baz=3]
text1
\end{foobar}
\begin{foobar}[bar=10]
text2
\end{foobar}
\begin{foobar}
text3
\end{foobar}
\end{document}
这些键是本地设置的,因此它们一开始不会改变其空状态。非空值被添加到一个序列中,该序列可以在条目之间“使用”不同的文本(两个条目之间用逗号分隔,两个条目之间用“and”分隔,如果超过两个条目,最后两个条目之间用“and”分隔)。
答案3
根据@nowox 的回答,我添加了允许在环境中使用键值的宏定义。
\documentclass{article}
\usepackage{keyval}
\makeatletter
\define@key{myenv@keys}{foo}{\def\myenv@foo{#1}}%
\define@key{myenv@keys}{bar}{\def\myenv@bar{#1}}%
\newenvironment{myenv}[1][]{%
% \makeatletter
\setkeys{myenv@keys}{foo=default foo,% Set default value for foo
bar=default bar,% Set default value for bar
#1}% Set values given in environment argument.
\def\foo{\myenv@foo}% Define \foo as the value given.
\def\bar{\myenv@bar}% Probably a bad idea to redefine \bar, but done for sake of demo.
In environment preamble: foo=``\foo'', bar=``\bar.''\par
}{
\par\bigskip
}
\makeatother
\begin{document}
\makeatother
\begin{myenv}[foo=hello]
% Within the environment, \foo and \bar can used.
In environment (1): \verb|\foo|=``\foo'', \verb|\bar|=``\bar.''
\end{myenv}
\begin{myenv}[bar=goodbye]
In environment (2): \verb|\foo|=``\foo'', \verb|\bar|=``\bar.''
\end{myenv}
Outside the \verb|myenv| environment, \verb|\foo| is undefined
and \verb|\bar| retains its original definition meaning,
e.g., \verb|\bar{A}=|$\bar{A}$.
\end{document}