
頻繁に使用するコマンド、複雑な式のエイリアスがあり、新しいエイリアスを導入せずにそれを否定したいと考えています。
単純な解決策は、デフォルト値を持つ 3 番目の引数を導入することですが、書き方が面倒なので\myCommand[\neq]{arg1}{arg2}
、 のように書く方\not\myCommand{arg1}{arg2}
がはるかに優れています。
例:
\newcommand{\divides}[2]{\ensuremath{#1\mid #2}}
両方の回答に感謝します。どこから調べればよいかがわかりました。
答え1
プレフィックスではなく * バリアントを使用した構文を提案します\not
。
\documentclass{article}
\usepackage{amsmath,amssymb}
\usepackage{xparse}
\NewDocumentCommand{\newrelation}{mmo}{%
% #1 is the command to define
% #2 is the relation to be used
% #3 (optional) is the alternative
\IfNoValueTF{#3}
{\NewDocumentCommand{#1}{smm}{%
\IfBooleanTF{##1}{##2\not#2##3}{##2#2##3}%
}%
}
{\NewDocumentCommand{#1}{smm}{%
\IfBooleanTF{##1}{##2#3##3}{##2#2##3}%
}%
}%
}
\newrelation{\EQ}{=}
\newrelation{\LESS}{<}
\newrelation{\divides}{\mid}[\nmid]
\begin{document}
$\EQ{a}{b}$ and $\EQ*{a}{b}$
$\LESS{a}{b}$ and $\LESS*{a}{b}$
$\divides{a}{b}$ and $\divides*{a}{b}$
\end{document}
言うことは\newrelation{\EQ}{=}
入力することと同じである
\NewDocumentCommand{\EQ}{smm}{%
\IfBooleanTF{#1}% true if * is present
{#2\not=#3}%
{#2=#3}%
}
whileは\newrelation{\divides}{\mid}[\nmid]
入力するようなものです
\NewDocumentCommand{\divides}{smm}{%
\IfBooleanTF{#1}% true if * is present
{#2\nmid#3}%
{#2\mid#3}%
}
例えば、は\not\in
間違っているので、次のようにすることができます。
\newrelation{\IN}{\in}[\notin]
結果は適切にタイプセットされます。同様に、\not\mid
悪い結果をもたらすので、\nmid
を優先する必要があります。
プレフィックスを使用する場合は、次のようになります (出力は同じです)。
\documentclass{article}
\usepackage{amsmath,amssymb}
\usepackage{xparse}
\NewDocumentCommand{\newrelation}{mmo}{%
% #1 is the command to define
% #2 is the relation to be used
% #3 (optional) is the alternative
\IfNoValueTF{#3}
{%
\NewDocumentCommand{#1}{mm}{##1#2##2}%
\expandafter\NewDocumentCommand\csname negate\string#1\endcsname{mm}{##1\not#2##2}%
}%
{%
\NewDocumentCommand{#1}{mm}{##1#2##2}%
\expandafter\NewDocumentCommand\csname negate\string#1\endcsname{mm}{##1#3##2}%
}%
}
\makeatletter
\NewDocumentCommand{\negate}{m}{%
\@ifundefined{negate\string#1}
{\@latex@error{Undefined relation}{}#1}{\@nameuse{negate\string#1}}%
}
\makeatother
\newrelation{\EQ}{=}
\newrelation{\LESS}{<}
\newrelation{\divides}{\mid}[\nmid]
\begin{document}
$\EQ{a}{b}$ and $\negate\EQ{a}{b}$
$\LESS{a}{b}$ and $\negate\LESS{a}{b}$
$\divides{a}{b}$ and $\negate\divides{a}{b}$
\end{document}
答え2
セットアップ作業は必要ですが、実行可能です。基本的には、さまざまなシンボルに対して目的の「A シンボル B」を実行するさまざまなマクロを定義し、次に、\negate
これらの各シンボルを順番に否定する方法を定義する必要があります。
\documentclass{article}
\usepackage{stackengine,xcolor,mathtools}
\def\eqsym{=}
\def\gtsymbol{>}
\let\svmid\mid
\let\svgtsymbol\gtsymbol
\newcommand{\divides}[2]{\ensuremath{#1\mid #2}}
\newcommand{\equals}[2]{\ensuremath{#1\eqsym #2}}
\newcommand\greaterthan[2]{\ensuremath{#1\gtsymbol #2}}
\newcommand\negate[1]{%
\ifx\divides#1\def\mid{\mathrlap{\,/}\svmid}\else%
\ifx\equals#1\let\eqsym\neq\else%
\ifx\greaterthan#1\def\gtsymbol{\mathrlap{\,\,/}\svgtsymbol}\else%
\fi\fi\fi%
#1%
}
\begin{document}
$\divides{1}{2} \quad \negate\divides{1}{2}$
$\equals{1}{2} \quad \negate\equals{1}{2}$
$\greaterthan{1}{2} \quad \negate\greaterthan{1}{2}$
$\divides{1}{2} \quad \equals{1}{2} \quad \greaterthan{1}{2}$
\end{document}
補遺:
egreg の定義の容易さと私のアプローチの構文を組み合わせる 1 つの方法は、彼の MWE を追加の定義とともに使用することです。
\def\negate#1{#1*}
そうであれば、
$\EQ{a}{b}$ and $\negate\EQ{a}{b}$
$\LESS{a}{b}$ and $\negate\LESS{a}{b}$
$\divides{a}{b}$ and $\negate\divides{a}{b}$
MWE と同じ結果が生成されます。