Ver*_*gen 121 latex optional-arguments
如何在LaTeX中使用可选参数创建命令?就像是:
\newcommand{\sec}[2][]{
\section*{#1
\ifsecondargument
and #2
\fi}
}
}
Run Code Online (Sandbox Code Playgroud)
然后,我可以称之为
\sec{Hello}
%Output: Hello
\sec{Hello}{Hi}
%Output: Hello and Hi
Run Code Online (Sandbox Code Playgroud)
mik*_*iku 160
指南中的示例:
\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.
Run Code Online (Sandbox Code Playgroud)
Wil*_*son 26
创建"可选参数"背后的一般思想是首先定义一个中间命令,该命令向前扫描以检测令牌流中接下来要出现的字符,然后插入相关的宏来处理适当的参数.使用通用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)然后可以用来检查第二个参数是否确实存在.有关允许的其他类型的可选参数,请参阅文档.
Dem*_*mis 23
所有上述内容都很难说,它可以在LaTeX中制作一个漂亮,灵活(或禁止过载)的功能!(TeX代码看起来像我的希腊)
好吧,只是为了添加我最近的(尽管不是那么灵活)开发,这是我最近在我的论文doc中使用的,
\usepackage{ifthen} % provides conditonals...
Run Code Online (Sandbox Code Playgroud)
启动命令,默认情况下将"optional"命令设置为空:
\newcommand {\figHoriz} [4] [] {
Run Code Online (Sandbox Code Playgroud)
然后,我将宏设置为临时变量\ 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)
Run Code Online (Sandbox Code Playgroud)
然后我使用\ temp {}变量为这两种情况运行宏.(这里只是将短标题设置为等于长标题,如果用户未指定的话).
\begin{figure}[!]
\begin{center}
\includegraphics[width=350 pt]{#3}
\temp %see above for caption etc.
\label{#2}
\end{center}
\end{figure}
}
Run Code Online (Sandbox Code Playgroud)
在这种情况下,我只检查\newcommand {}提供的单个"可选"参数.如果你要为3个"可选"的args设置它,你仍然需要发送3个空白的args ...例如.
\MyCommand {first arg} {} {} {}
Run Code Online (Sandbox Code Playgroud)
这是非常愚蠢的,我知道,但这就是我要和LaTeX一起使用 - 一旦我开始查看TeX代码就不那么敏感......我确实喜欢Robertson先生的xparse方法,也许我试试吧......
Ale*_*tov 13
您所需要的只是以下内容:
\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
Run Code Online (Sandbox Code Playgroud)
当我想创建一个\dx简化的命令时,我遇到了类似的问题\;\mathrm{d}x(即,在积分的微分之前放置一个额外的空间,同时也使“ d”直立)。但是后来我也想使其足够灵活,以将积分变量作为可选参数包括在内。我将以下代码放在序言中。
\usepackage{ifthen}
\newcommand{\dx}[1][]{%
\ifthenelse{ \equal{#1}{} }
{\ensuremath{\;\mathrm{d}x}}
{\ensuremath{\;\mathrm{d}#1}}
}
Run Code Online (Sandbox Code Playgroud)
然后
\begin{document}
$$\int x\dx$$
$$\int t\dx[t]$$
\end{document}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
93509 次 |
| 最近记录: |