如何在LaTeX中使用可选参数创建命令? 类似的东西:

\newcommand{\sec}[2][]{
    \section*{#1
        \ifsecondargument
            and #2
        \fi}
    }
}

然后,我可以称之为

\sec{Hello}
%Output: Hello
\sec{Hello}{Hi}
%Output: Hello and Hi
有帮助吗?

解决方案

来自指南的示例:

\newcommand{\example}[2][YYY]{Mandatory arg: #2;
                                 Optional arg: #1.}

This defines \example to be a command with two arguments, 
referred to as #1 and #2 in the {<definition>}--nothing new so far. 
But by adding a second optional argument to this \newcommand 
(the [YYY]) the first argument (#1) of the newly defined 
command \example is made optional with its default value being YYY.

Thus the usage of \example is either:

   \example{BBB}
which prints:
Mandatory arg: BBB; Optional arg: YYY.
or:
   \example[XXX]{AAA}
which prints:
Mandatory arg: AAA; Optional arg: XXX.

其他提示

创建“可选参数”背后的一般思路首先定义一个中间命令,该命令向前扫描以检测令牌流中接下来要出现的字符,然后插入相关的宏来处理适当的参数。使用通用TeX编程可能非常繁琐(尽管并不困难)。 LaTeX的 \ @ ifnextchar 对于这些事情非常有用。

您的问题的最佳答案是使用新的 xparse 包。它是LaTeX3编程套件的一部分,包含用于定义具有完全任意可选参数的命令的广泛功能。

在您的示例中,您有一个 \ sec 宏,它可以使用一个或两个支撑参数。这将使用 xparse 实现,其中包含以下内容:

\documentclass{article}
\usepackage{xparse}
\begin{document}
\DeclareDocumentCommand\sec{ m g }{%
    {#1%
        \IfNoValueF {#2} { and #2}%
    }%
}
(\sec{Hello})
(\sec{Hello}{Hi})
\end{document}

参数 {m g} 定义 \ sec ;的参数。 m 表示“强制性参数”。并且 g 是“可选的支撑参数”。然后可以使用 \ IfNoValue(T)(F)来检查第二个参数是否确实存在。请参阅文档以了解允许的其他类型的可选参数。

以上所有内容都很难说是可以在LaTeX中制作一个漂亮,灵活(或禁止过载)的功能! (TeX代码对我来说似乎是希腊语)

好吧,只是为了添加我最近的(尽管不是那么灵活)开发,这是我最近在论文doc中使用的内容,

\usepackage{ifthen}  % provides conditonals...

使用“可选”命令启动命令命令默认设置为空白:

\newcommand {\figHoriz} [4] []  {

然后我将宏设置为临时变量\ temp {},具体取决于可选参数是否为空。这可以扩展到任何传递的参数。

\ifthenelse { \equal {#1} {} }  %if short caption not specified, use long caption (no slant)
    { \def\temp {\caption[#4]{\textsl{#4}}} }   % if #1 == blank
    { \def\temp {\caption[#1]{\textsl{#4}}} }   % else (not blank)

然后我使用\ temp {}变量为这两种情况运行宏。 (如果没有用户指定,它只是将短标题设置为等于长标题。)

\begin{figure}[!]
    \begin{center}
        \includegraphics[width=350 pt]{#3}
        \temp   %see above for caption etc.
        \label{#2}
    \end{center}
\end{figure}
}

在这种情况下,我只检查单个,“可选”和\ newcommand {}提供的参数。如果你要设置它,比如说3“可选”的话。 args,你仍然需要发送3个空白的args ......例如。

\MyCommand {first arg} {} {} {}

这是非常愚蠢的,我知道,但这就是我要和LaTeX一起使用 - 一旦我开始查看TeX代码就没那么敏感......我确实喜欢Robertson先生的xparse方法,也许我会尝试一下......

您只需要以下内容:

\makeatletter
\def\sec#1{\def\tempa{#1}\futurelet\next\sec@i}% Save first argument
\def\sec@i{\ifx\next\bgroup\expandafter\sec@ii\else\expandafter\sec@end\fi}%Check brace
\def\sec@ii#1{\section*{\tempa\ and #1}}%Two args
\def\sec@end{\section*{\tempa}}%Single args
\makeatother

\sec{Hello}
%Output: Hello
\sec{Hello}{Hi}
%Output: Hello and Hi

我有类似的问题,当我想创建一个命令 \ dx ,缩写 \; \ mathrm {d} x (即增加一个空间)在积分的微分之前并且也具有“d”直立。但是我还想让它足够灵活,将集成变量作为可选参数包含在内。我在序言中加入了以下代码。

\usepackage{ifthen}

\newcommand{\dx}[1][]{%
   \ifthenelse{ \equal{#1}{} }
      {\ensuremath{\;\mathrm{d}x}}
      {\ensuremath{\;\mathrm{d}#1}}
}

然后

\begin{document}
   $\int x\dx$
   $\int t\dx[t]$
\end{document}

\ dx with optional argument

这是我的尝试,但它并不完全符合您的规格。没有经过充分测试,所以要小心。

\newcount\seccount

\def\sec{%
    \seccount0%
    \let\go\secnext\go
}

\def\secnext#1{%
    \def\last{#1}%
    \futurelet\next\secparse
}

\def\secparse{%
    \ifx\next\bgroup
        \let\go\secparseii
    \else
        \let\go\seclast
    \fi
    \go
}

\def\secparseii#1{%
    \ifnum\seccount>0, \fi
    \advance\seccount1\relax
    \last
    \def\last{#1}%
    \futurelet\next\secparse
}

\def\seclast{\ifnum\seccount>0{} and \fi\last}%

\sec{a}{b}{c}{d}{e}
% outputs "a, b, c, d and e"

\sec{a}
% outputs "a"

\sec{a}{b}
% outputs "a and b"
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top