\newcommand / \newenvironment - 可选参数
我正在尝试自己的命令和环境,现在面临这些问题:
- 如何创建命令
\foo{parameter}[可选]
或名为\begin{bar}{参数}[可选]
? - 如何创建命令
\foo[Optional_1]...[Optional_n]{parameter}
我尝试过
\newcommand{\foo}[3][][]{#1#2#3} - failed
\newcommand{\foo}[3][2][][]{#1#2#3} - failed
有人知道一些提示吗?多谢。
I'm experimenting with my own commands and environments and now I'm facing those problems:
- How to create command
\foo{parameter}[optional]
or environment called\begin{bar}{parameter}[optional]
? - How to create command
\foo[optional_1]...[optional_n]{parameter}
I've tried
\newcommand{\foo}[3][][]{#1#2#3} - failed
\newcommand{\foo}[3][2][][]{#1#2#3} - failed
Does anyone know some hint? Thanks a lot.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您不能简单地创建
\foo{parameter}[可选]
命令;但是,您可以使用创建
\foo[可选]{parameter}
命令如果你将其称为
\foo{given}
,它将产生Mandatory:给定,可选:default
;如果你将其称为\foo[bonus]{given}
,它将产生强制:给定,可选:奖励
。这可能就是您应该这样做的方式——这与您的 LaTeX 代码的其余部分一起看起来会更好。创建具有可选参数的新环境的方法与类似
其中
#1
又是可选参数;这又被写为\begin{env}[opt]{req}...\end{env}
。如果您确实想要其他形式的命令,请参阅我答案的末尾。TeX FAQ 有一个关于使用多个命令编写命令的答案可选参数。如何做到这一点有两种选择。基本思想是定义一个带有可选参数的命令,然后运行另一个带有可选参数的命令,等等; twoopt 包对此进行了封装。
如果您确实想要像
\reversed{mandatory}[可选]
这样的命令,您可以这样做。首先,定义一个命令,该命令接受必需的参数,将其存储在宏中,然后将其转发到另一个命令。第二个命令采用可选参数,并使用定义的命令和可选参数。将所有这些放在一起,我们得到您可以使用
\reversed{mandatory}[可选]
或仅使用\reversed{mandatory}
,一切都应该正常。You can't create a
\foo{parameter}[optional]
command simply; you can, however, create a\foo[optional]{parameter}
command withIf you call it as
\foo{given}
, it will produceMandatory: given, optional: default
; if you call it as\foo[bonus]{given}
, it will produceMandatory: given, optional: bonus
. This is probably how you should do it—that will look better with the rest of your LaTeX code. Creating a new environment with optional parameters is done similarly withwhere
#1
is again the optional argument; this is again written as\begin{env}[opt]{req}...\end{env}
. If you really want a command in the other form, see the end of my answer.The TeX FAQ has an answer about writing commands with more than one optional argument. There are two options to how to do it. The underlying idea is to define a command which takes an optional argument, and then runs another command which itself takes an optional argument, etc.; the twoopt package encapsulates this.
If you really want a command like
\reversed{mandatory}[optional]
, you can do it like so. First, you define a command which takes a required argument, stores it in a macro, and then forward it onto another command. This second command takes an optional argument, and uses the defined command and the optional argument. Putting this all together, we getYou can then use
\reversed{mandatory}[optional]
or just\reversed{mandatory}
, and everything should work.使用 xparse 包(LaTeX3 开发工作的一部分):
xparse 中的可选参数与 \newcommand 中的可选参数略有不同。您可以检测是否给出了一个:
您会发现这是通过使用小写“o”来实现的,而大写“O”则需要一个默认值(我通过包含一个空组将其设为空) )。
Using the xparse package (part of the LaTeX3 development efforts):
Optional arguments are a bit different in xparse to with \newcommand. You can detect whether one is given or not:
You'll see that this works by using a lower case 'o', whereas the upper case 'O' then requires a default value (which I've made empty by including an empty group).
还要考虑 xargs 包。以下是其文档中的示例。
以通常的方式设置它,
然后如果您定义
(这意味着如果未指定,则使用“1”作为第一个参数,并使用“n”作为第三个参数)。然后
生成(无下标)
(x1, ..., xn)
并
生成(再次,无下标,y 替换强制参数)
(y0, ..., yn)
Consider also the xargs package. The following is an example from its documentation.
Set it up in the usual way,
and then if you define
(which means to use "1" for the first argument, if it is not specified, and to use "n" for the third). Then
yields (sans subscripts)
(x1, . . . , xn)
and
yields (again, sans subscripts, and y replaces the mandatory parameter)
(y0, ..., yn)
我知道已经有全面的答案,但在某些情况下,我想针对不同的情况给出不同的定义。对于这个问题,有一个仍然非常基本但简单的解决方案。我把它写下来,以防其他人需要。
结果如下所示,可选参数的不同定义:
I know there are already comprehensive answers, but is some cases, I want to give different definitions for different situations. There is a still very basic yet simple solution for this. I write it down in case any other need it.
And the result is shown below, different definitions for optional parameter(s):
特洛伊丹尼尔的解决方案对我来说非常有效。此自定义“示例”环境允许命名您的示例或不使用可选参数。
Troy Daniel's solution worked perfectly for me. This custom 'example' environment allows to name your examples or not using an optional parameter.