
我的問題是關於聲明帶有選項的“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/new 指令的可選參數? 兩者都沒有幫助我。
答案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}
正如圖哈米正確指出的那樣,使用簡單結構的代價\ifx
是意外的語法錯誤(例如使用兩個字元的可選參數)[rs]
不會產生錯誤訊息,而只是錯誤地設定了術語。
答案2
這是一種使用以下方法來做到這一點的方法xstring
包的\IfStrEqCase
:
預設選項是r
圓括號、c
大括號和s
方括號。您也可以新增可選的第二個參數來控制大括號大小:
代碼:
\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