\newcommand / \newenvironment - 可选参数

发布于 2024-09-03 03:23:26 字数 352 浏览 2 评论 0原文

我正在尝试自己的命令和环境,现在面临这些问题:

  1. 如何创建命令 \foo{parameter}[可选] 或名为 \begin{bar}{参数}[可选]
  2. 如何创建命令 \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:

  1. How to create command \foo{parameter}[optional] or environment called \begin{bar}{parameter}[optional]?
  2. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

幻想少年梦 2024-09-10 03:23:26
  1. 您不能简单地创建 \foo{parameter}[可选] 命令;但是,您可以使用

    创建 \foo[可选]{parameter} 命令

    \newcommand{\foo}[2][默认]{强制:#2;可选:#1}
    

    如果你将其称为\foo{given},它将产生Mandatory:给定,可选:default;如果你将其称为\foo[bonus]{given},它将产生强制:给定,可选:奖励。这可能就是您应该这样做的方式——这与您的 LaTeX 代码的其余部分一起看起来会更好。创建具有可选参数的新环境的方法与

    类似

    \newenvironment{env}[2][def]{(#1,#2)\begingroup}{\endgroup}
    

    其中 #1 又是可选参数;这又被写为 \begin{env}[opt]{req}...\end{env}。如果您确实想要其他形式的命令,请参阅我答案的末尾。

  2. TeX FAQ 有一个关于使用多个命令编写命令的答案可选参数。如何做到这一点有两种选择。基本思想是定义一个带有可选参数的命令,然后运行另一个带有可选参数的命令,等等; twoopt 包对此进行了封装。


如果您确实想要像\reversed{mandatory}[可选]这样的命令,您可以这样做。首先,定义一个命令,该命令接受必需的参数,将其存储在宏中,然后将其转发到另一个命令。第二个命令采用可选参数,并使用定义的命令和可选参数。将所有这些放在一起,我们得到

\makeatletter
\newcommand{\reversed}[1]{\def\reversed@required{#1}\reversed@opt}
\newcommand{\reversed@opt}[1][def]{Required: \reversed@required; optional: #1}
\makeatother

您可以使用 \reversed{mandatory}[可选] 或仅使用 \reversed{mandatory},一切都应该正常。

  1. You can't create a \foo{parameter}[optional] command simply; you can, however, create a \foo[optional]{parameter} command with

    \newcommand{\foo}[2][default]{Mandatory: #2; optional: #1}
    

    If you call it as \foo{given}, it will produce Mandatory: given, optional: default; if you call it as \foo[bonus]{given}, it will produce Mandatory: 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 with

    \newenvironment{env}[2][def]{(#1,#2)\begingroup}{\endgroup}
    

    where #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.

  2. 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 get

\makeatletter
\newcommand{\reversed}[1]{\def\reversed@required{#1}\reversed@opt}
\newcommand{\reversed@opt}[1][def]{Required: \reversed@required; optional: #1}
\makeatother

You can then use \reversed{mandatory}[optional] or just \reversed{mandatory}, and everything should work.

烟织青萝梦 2024-09-10 03:23:26

使用 xparse 包(LaTeX3 开发工作的一部分):

\usepackage{xparse}
\NewDocumentCommand\foo{O{}O{}m}{%
  % Code with optional #1 and #2 with empty defaults
}
\NewDocumentCommand\foo{mO{}}{%
  % Code with optional #2 with empty default
}
\NewDocumentEnvironment{foo}{O{}}{%
% Start code with optional #1
}{%
%  End code with optional #1
}

xparse 中的可选参数与 \newcommand 中的可选参数略有不同。您可以检测是否给出了一个:

\NewDocumentCommand\foo{mo}{%
  \IfNoValueTF{#2}
   {Code without #2}
   {Code with #2}%
}

您会发现这是通过使用小写“o”来实现的,而大写“O”则需要一个默认值(我通过包含一个空组将其设为空) )。

Using the xparse package (part of the LaTeX3 development efforts):

\usepackage{xparse}
\NewDocumentCommand\foo{O{}O{}m}{%
  % Code with optional #1 and #2 with empty defaults
}
\NewDocumentCommand\foo{mO{}}{%
  % Code with optional #2 with empty default
}
\NewDocumentEnvironment{foo}{O{}}{%
% Start code with optional #1
}{%
%  End code with optional #1
}

Optional arguments are a bit different in xparse to with \newcommand. You can detect whether one is given or not:

\NewDocumentCommand\foo{mo}{%
  \IfNoValueTF{#2}
   {Code without #2}
   {Code with #2}%
}

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).

ら栖息 2024-09-10 03:23:26

还要考虑 xargs 包。以下是其文档中的示例。

以通常的方式设置它,

\usepackage{xargs}

然后如果您定义

\newcommandx*\coord[3][1=1, 3=n]{(#2_{#1},\ldots,#2_{#3})}

(这意味着如果未指定,则使用“1”作为第一个参数,并使用“n”作为第三个参数)。然后

$\coord{x}$

生成(无下标)

(x1, ..., xn)

$\coord[0]{y}$

生成(再次,无下标,y 替换强制参数)

(y0, ..., yn)

Consider also the xargs package. The following is an example from its documentation.

Set it up in the usual way,

\usepackage{xargs}

and then if you define

\newcommandx*\coord[3][1=1, 3=n]{(#2_{#1},\ldots,#2_{#3})}

(which means to use "1" for the first argument, if it is not specified, and to use "n" for the third). Then

$\coord{x}$

yields (sans subscripts)

(x1, . . . , xn)

and

$\coord[0]{y}$

yields (again, sans subscripts, and y replaces the mandatory parameter)

(y0, ..., yn)

我一向站在原地 2024-09-10 03:23:26

我知道已经有全面的答案,但在某些情况下,我想针对不同的情况给出不同的定义。对于这个问题,有一个仍然非常基本但简单的解决方案。我把它写下来,以防其他人需要。

% ----------------------------------
%! TEX program = XeLaTeX
% !TeX encoding = UTF-8
% Author:       Troy_Daniel 
% Email:        [email protected]
% ----------------------------------
\documentclass{article}
\usepackage{xcolor}

\newcommand{\Caption}[3]{%
    \textcolor[rgb]{0.36, 0.72, 0.80}{\Large #1}
    {\def\tmp{#3}
    \ifx\tmp\empty                                  % the third parameter is not provieded
        \textcolor[rgb]{0.96, 0.66, 0.35}{\small#2}
    \else                                           % non-empty third parameter
        \textcolor[rgb]{0.96, 0.66, 0.35}{\small[#3]#2}
    \fi}}
\begin{document}

\Caption{First}{Second}{}

\Caption{First}{Second}{Third}

\end{document}

结果如下所示,可选参数的不同定义:
可选参数的不同定义

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.

% ----------------------------------
%! TEX program = XeLaTeX
% !TeX encoding = UTF-8
% Author:       Troy_Daniel 
% Email:        [email protected]
% ----------------------------------
\documentclass{article}
\usepackage{xcolor}

\newcommand{\Caption}[3]{%
    \textcolor[rgb]{0.36, 0.72, 0.80}{\Large #1}
    {\def\tmp{#3}
    \ifx\tmp\empty                                  % the third parameter is not provieded
        \textcolor[rgb]{0.96, 0.66, 0.35}{\small#2}
    \else                                           % non-empty third parameter
        \textcolor[rgb]{0.96, 0.66, 0.35}{\small[#3]#2}
    \fi}}
\begin{document}

\Caption{First}{Second}{}

\Caption{First}{Second}{Third}

\end{document}

And the result is shown below, different definitions for optional parameter(s):
Different definitions for optional parameter(s)

乄_柒ぐ汐 2024-09-10 03:23:26

特洛伊丹尼尔的解决方案对我来说非常有效。此自定义“示例”环境允许命名您的示例或不使用可选参数。

\newcounter{example}[section]
\newenvironment{examplex}[1][]{
\def\optpar{#1}
\refstepcounter{examplex}\par\medskip\noindent\textbf{
    Example~\thesection.\theexample
    \ifx\optpar\empty
    {.}
    \else
    { - }#1.
    \fi
    } 
    \rmfamily
    \begin{adjustwidth}{2em}{0em}}{
    \medskip
    \end{adjustwidth}
}

Troy Daniel's solution worked perfectly for me. This custom 'example' environment allows to name your examples or not using an optional parameter.

\newcounter{example}[section]
\newenvironment{examplex}[1][]{
\def\optpar{#1}
\refstepcounter{examplex}\par\medskip\noindent\textbf{
    Example~\thesection.\theexample
    \ifx\optpar\empty
    {.}
    \else
    { - }#1.
    \fi
    } 
    \rmfamily
    \begin{adjustwidth}{2em}{0em}}{
    \medskip
    \end{adjustwidth}
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文