將 \not 與自訂命令一起使用

將 \not 與自訂命令一起使用

我有一個命令,一個複雜表達式的別名,我經常使用它,我想在不引入新別名的情況下否定它。

簡單的解決方案引入了帶有預設值的第三個參數,但是寫起來很混亂\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的定義的可能的簡易性與我的方法的語法結合起來的一種方法是使用他的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 相同的結果。

相關內容