
私の質問は、オプションを使用して「newcommand」を宣言することについてです。
私は定義しました
\newcommand{\bforall}[2]{\forall #1\, (#2)}
つまり、 と$\bforall{x}{x \in A}$
書きます$\forall x (x \in A)$
。
括弧をコントロールしたいのですが、具体的には
\bforall[s]{x}{x \in A}
角括弧を使用する
\bforall[c]{x}{x \in A}
中括弧を使用します。
これはどうすれば実現できるのでしょうか?
フォーラムを検索して1/を見つけましたオプション引数の有無による異なるコマンド定義 そして2/新しいコマンドのオプション引数? そしてどちらも私を助けてくれませんでした。
答え1
パッケージなしの方法がここにあります。
\documentclass{article}
\newcommand\bforall[3][r]{%
\ifx r#1\forall #2\, (#3)\else
\ifx s#1\forall #2\, [#3]\else
\ifx c#1\forall #2\, \{#3\}\else
\mathrm{Illegal~option}%
\fi\fi\fi
}
\begin{document}
$\bforall{x}{x \in A}$
$\bforall[r]{x}{x \in A}$
$\bforall[s]{x}{x \in A}$
$\bforall[c]{x}{x \in A}$
\end{document}
touhami が正しく指摘しているように、構造の単純さを使用することのペナルティ\ifx
は、 のような 2 文字のオプション引数を使用するなどの偶発的な構文エラーでは[rs]
エラー メッセージが生成されず、単に用語が誤って設定されることです。
答え2
これを実行する方法の1つは、xstring
パッケージの\IfStrEqCase
:
デフォルトのオプションは、r
丸括弧、c
四角括弧、およびs
丸括弧です。括弧のサイズを制御するために、オプションの 2 番目のパラメータを追加することもできます。
コード:
\documentclass{article}
\usepackage{xstring}
\newcommand{\bforall}[3][r]{%
\forall #2\,
\IfStrEqCase{#1}{%
{r}{(#3)}%
{c}{\{#3\}}%
{s}{{[#3]}}%
}[%
%% Issue error message here about unsuported bracket type
]%
}
\begin{document}
$\bforall{x}{x \in A}$
$\bforall[r]{x}{x \in A}$
$\bforall[c]{x}{x \in A}$
$\bforall[s]{x}{x \in A}$
\end{document}
コードResizing Brackets
:
\documentclass{article}
\usepackage{xstring}
\usepackage{xparse}
\usepackage{amsmath}
\NewDocumentCommand{\bforall}{%
O{r}% #1 = r, c or s (bracket shape)
O{}% #2 = optional size specifier
m% #3
m% #4
}{%
\forall #3\,
\IfStrEqCase{#1}{%
{r}{#2(#4#2)}%
{c}{#2\{#4#2\}}%
{s}{{#2[#4#2]}}%
}[%
%% Issue error message here about unsupported bracket type
\PackageError{tree}{Undefined option to tree: #1}{}
]%
}
\begin{document}
\begin{minipage}{0.25\linewidth}
$\bforall{x}{x \in A}$
$\bforall[r]{x}{x \in A}$
$\bforall[c]{x}{x \in A}$
$\bforall[s]{x}{x \in A}$
\end{minipage}
\hspace*{0.5cm}
\begin{minipage}{0.25\linewidth}
$\bforall[r][\Big]{x}{x \in A, x > \dfrac{1}{2}}$
$\bforall[c][\Big]{x}{x \in A, x > \dfrac{1}{2}}$
$\bforall[s][\bigg]{x}{x \in A, x > \dfrac{1}{2}}$
\end{minipage}
\end{document}
答え3
ケーススイッチマクロに最適なのは です。括弧には 、角括弧には 、中括弧には など、expl3
すでに一般的な文字を使用しました。amsmath
p
b
B
\documentclass{article}
\usepackage{amsmath}
\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand{\bforall}{O{p}mm}
{
\forall #2\,
\str_case:nnF { #1 }
{
{p}{(#3)}
{b}{[#3]}
{B}{\{#3\}}
}
{\@latex@error{Illegal~option~#1}{I~used~p}}
}
\ExplSyntaxOff
\begin{document}
$\bforall{x}{x \in A}$
$\bforall[p]{x}{x \in A}$
$\bforall[b]{x}{x \in A}$
$\bforall[B]{x}{x \in A}$
\end{document}
答え4
異なる構文を持つプレーンなTeX
\def\bforall#1{\forall#1\,\futurelet\tmptoken\dobforall}
\def\dobforall{\ifx\tmptoken\bgroup\expandafter\bforallbraces\fi
\ifx\tmptoken[\expandafter\bforallbrackets\fi
\ifx\tmptoken(\expandafter\bforallparenthesis\fi
\relax}
\def\bforallbraces#1\relax#2{\{#2\}}
\def\bforallbrackets#1\relax[#2]{[#2]}
\def\bforallparenthesis#1\relax(#2){(#2)}
$\bforall{x}(x \in A)$\par
$\bforall{x}{x \in A}$\par
$\bforall{x}[x \in A]$\par
\bye
これらの定義は、パッケージを必要とせずに LaTeX でも使用できます。構文は、 、 、 の方が簡単だと思います\bforall{x}(x\in A)
。\bforall{x}{x\in A}
いずれにしても、あなたのニーズでは、そのマクロはまったく必要なく、と の後に入力したものが続くだけ\bforall{x}[x\in A]
でしょうか?\def\bforall#1{\forall#1\,}
構文を使用できるように少し改良しました\bforall x {x \in A}
。(LaTeX では、 があるためより簡単になります\@ifnextchar
。)
\def\bforall#1{\forall#1\,\futurelet\tmptoken\dobforall}
\def\dobforall{\ifx\tmptoken\spacetoken\expandafter\bforallspace\fi
\ifx\tmptoken\bgroup\expandafter\bforallbraces\fi
\ifx\tmptoken[\expandafter\bforallbrackets\fi
\ifx\tmptoken(\expandafter\bforallparenthesis\fi
\relax}
\def\bforallbraces#1\relax#2{\{#2\}}
\def\bforallbrackets#1\relax[#2]{[#2]}
\def\bforallparenthesis#1\relax(#2){(#2)}
\def\bforallspace#1\relax{\dobforallspace}
\expandafter\def\expandafter\dobforallspace\space{\futurelet\tmptoken\dobforall}
\lowercase{\let\spacetoken= } %
$\bforall x (x \in A)$\par
$\bforall x {x \in A}$\par
$\bforall x [x \in A]$\par
\bye