質問
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プログラミングスイートの一部であり、非常に任意のオプション引数を使用してコマンドを定義するための広範な機能が含まれています。
この例では、1つまたは2つの中括弧引数を取る \ 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)
を使用して、2番目の引数が実際に存在したかどうかを確認できます。許可されている他のタイプのオプション引数については、ドキュメントを参照してください。
上記のすべては、LaTeXで素敵で柔軟な(またはオーバーロードを禁止する)関数を作成するのは難しいことです!!! (TeXコードはギリシャ語のように見えます)
まあ、最近の(それほど柔軟ではないにせよ)開発を追加するために、ここで最近私が論文で使用したものを以下に示します
\usepackage{ifthen} % provides conditonals...
&quot; optional&quot;でコマンドを開始しますコマンドはデフォルトで空白に設定されています:
\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)
次に、2つの場合に\ temp {}変数を使用してマクロを実行します。 (ここでは、ユーザーによって指定されていない場合は、短いキャプションを長いキャプションに等しく設定します)。
\begin{figure}[!]
\begin{center}
\includegraphics[width=350 pt]{#3}
\temp %see above for caption etc.
\label{#2}
\end{center}
\end{figure}
}
この場合、単一の「オプション」のみをチェックします。 \ newcommand {}が提供する引数。たとえば、「3」「オプション」に設定する場合引数、まだ3つの空の引数を送信する必要があります...例:
\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}
ここに私の試みがありますが、それはあなたの仕様に正確には従っていません。完全にテストされていないため、注意が必要です。
\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"