
Tenho um comando, alias de uma expressão complexa, que uso com frequência e gostaria de negá-lo sem introduzir um novo alias.
A solução simples introduz o terceiro argumento com valor padrão, mas é complicado escrever \myCommand[\neq]{arg1}{arg2}
, algo assim \not\myCommand{arg1}{arg2}
é muito melhor.
Exemplo:
\newcommand{\divides}[2]{\ensuremath{#1\mid #2}}
obrigado por ambas as respostas, agora vejo onde me aprofundar.
Responder1
Proponho uma sintaxe com uma variante * em vez do \not
prefixo.
\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}
Dizer \newrelation{\EQ}{=}
é equivalente a digitar
\NewDocumentCommand{\EQ}{smm}{%
\IfBooleanTF{#1}% true if * is present
{#2\not=#3}%
{#2=#3}%
}
while \newrelation{\divides}{\mid}[\nmid]
é como digitar
\NewDocumentCommand{\divides}{smm}{%
\IfBooleanTF{#1}% true if * is present
{#2\nmid#3}%
{#2\mid#3}%
}
Por exemplo, como \not\in
está errado, você poderia fazer
\newrelation{\IN}{\in}[\notin]
e os resultados serão adequadamente compostos. Da mesma forma, \not\mid
dá um resultado ruim e \nmid
deve ser preferido.
Se você preferir usar um prefixo, aqui está (mesma saída):
\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}
Responder2
O trabalho de configuração é necessário, mas pode ser feito. Em essência, você definiu várias macros que executam o desejado "Símbolo A B" para vários símbolos e, em seguida, deve definir \negate
para saber como negar cada um desses símbolos, por sua vez.
\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}
TERMO ADITIVO:
Uma maneira de combinar a possível facilidade das definições de egreg com a sintaxe da minha abordagem seria usar seu MWE, com a definição adicional:
\def\negate#1{#1*}
Dessa forma,
$\EQ{a}{b}$ and $\negate\EQ{a}{b}$
$\LESS{a}{b}$ and $\negate\LESS{a}{b}$
$\divides{a}{b}$ and $\negate\divides{a}{b}$
produziria o mesmo resultado que seu MWE.